generated from voxgig/model-vue
-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add BasicEntityAutocompleteField component #113
Closed
Closed
Changes from 6 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
838769a
Add BasicEntityAutocompleteField component
zackatbrightbeam aea72fb
Refactor BasicEntityAutocompleteField component
zackatbrightbeam 8216588
Remove unnecessary code from BasicEntityAutocompleteField
zackatbrightbeam 2375bb5
adjust gubu shape for BasicEntityAutocompleteField
zackatbrightbeam 9ade7be
change variable name to avoid confusion with other BasicEntityField c…
zackatbrightbeam d80ae75
add comment to resolveOptions in BasicEntityAutocompleteField
zackatbrightbeam 94e0f03
update gubu shape on BasicEntityAutocompleteField
zackatbrightbeam fd7ef67
Tie getOptionLabel to model
zackatbrightbeam 13bdb3e
Add key prop to Controller cmp
zackatbrightbeam f64e350
Refactor BasicEntityAutocompleteField component
zackatbrightbeam 149e511
Remove unnecessary prop in Gubu shape
zackatbrightbeam 865cb9b
Refactor BasicEntityAutocompleteField component
zackatbrightbeam File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
declare function BasicEntityAutocompleteField(props: any): import("react/jsx-runtime").JSX.Element; | ||
export { BasicEntityAutocompleteField }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
declare function BasicEntityCheckBoxField(props: any): import("react/jsx-runtime").JSX.Element; | ||
export { BasicEntityCheckBoxField }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
declare function BasicEntityRadioGroupField(props: any): import("react/jsx-runtime").JSX.Element; | ||
export { BasicEntityRadioGroupField }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
declare function BasicEntitySliderField(props: any): import("react/jsx-runtime").JSX.Element; | ||
export { BasicEntitySliderField }; |
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import React, { useEffect, forwardRef } from "react"; | ||
|
||
import { TextField, Autocomplete } from "@mui/material"; | ||
import { Controller } from "react-hook-form"; | ||
|
||
import type { Spec } from './basic-types' | ||
|
||
import { Default, Gubu } from 'gubu' | ||
const CMPNAME = 'BasicEntityAutocompleteField' | ||
|
||
const { Open } = Gubu | ||
const BasicEntityAutocompleteFieldSpecShape = Gubu(Open({ | ||
field: Open({ | ||
id: String, | ||
name: String, | ||
kind: String, | ||
label: Default('', String), | ||
options: Open({ | ||
default: Open({}), | ||
label: { field: Default('label', String) }, | ||
multiple: Default(false, Boolean), | ||
ents: Open({}) | ||
}), | ||
ux: Open({ | ||
kind: Default('Text', String), | ||
edit: Default(true, Boolean), | ||
}) | ||
}), | ||
errors: Open({}), | ||
}), {name: CMPNAME}) | ||
|
||
function BasicEntityAutocompleteField(props: any) { | ||
const { spec } = props | ||
|
||
// console.log('BasicEntityAutocompleteField', spec) | ||
|
||
const basicEntityAutocompleteField: Spec = BasicEntityAutocompleteFieldSpecShape(spec) | ||
const { control, field } = basicEntityAutocompleteField | ||
const { resolvedOptions, resolvedDefault } = resolveOptions(field.options); | ||
|
||
return ( | ||
<Controller | ||
name={field.name} | ||
control={control} | ||
defaultValue={resolvedDefault} | ||
render={({ field: { onChange, value } }) => ( | ||
<Autocomplete | ||
key={field.id} | ||
freeSolo | ||
forcePopupIcon | ||
multiple={field.options.multiple} | ||
options={resolvedOptions} | ||
isOptionEqualToValue={(opt: any, val: any) => | ||
opt === val || | ||
(opt?.id != null && val?.id != null && opt.id === val.id) || | ||
(opt?.value != null && val?.value != null && opt.value === val.value) | ||
} | ||
getOptionLabel={(option: any) => option.label} | ||
value={value} | ||
onChange={(_, nweValue: any) => onChange(nweValue)} | ||
renderInput={(params: any) => <TextField {...params} label={field.label} />} | ||
/> | ||
)} | ||
/> | ||
) | ||
} | ||
|
||
// Returns array of options and default value(s) based on the options object | ||
function resolveOptions(options: any) { | ||
// Array of options | ||
const resolvedOptions = Object.keys(options.ents).map(key => ({ | ||
label: options.ents[key].label, | ||
value: key | ||
})) | ||
|
||
// Array of default values (or single value if multiple is false) | ||
const resolvedDefault = options.multiple === false ? ( | ||
Object.keys(options.default).length > 0 | ||
? { value: Object.keys(options.default)[0], label: options.default[Object.keys(options.default)[0]].label } | ||
: null | ||
) : ( | ||
Object.keys(options.default).map(key => ({ | ||
label: options.default[key].label, | ||
value: key | ||
})) | ||
) | ||
|
||
return { | ||
resolvedOptions, | ||
resolvedDefault | ||
} | ||
} | ||
|
||
export { BasicEntityAutocompleteField } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Make it Exact('Autocomplete')