Skip to content
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
wants to merge 12 commits into from
Closed
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions dist/BasicEntityAutocompleteField.d.ts
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 };
2 changes: 2 additions & 0 deletions dist/BasicEntityCheckBoxField.d.ts
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 };
2 changes: 2 additions & 0 deletions dist/BasicEntityRadioGroupField.d.ts
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 };
2 changes: 2 additions & 0 deletions dist/BasicEntitySliderField.d.ts
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 };
301 changes: 129 additions & 172 deletions dist/voxgig-model-react.es.js

Large diffs are not rendered by default.

299 changes: 128 additions & 171 deletions dist/voxgig-model-react.umd.js

Large diffs are not rendered by default.

94 changes: 94 additions & 0 deletions src/lib/BasicEntityAutocompleteField.tsx
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),
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make it Exact('Autocomplete')

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 }
2 changes: 2 additions & 0 deletions src/lib/BasicEntityEdit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ function BasicEntityEdit (props: any) {
handleSubmit,
getValues,
reset,
control,
formState: { errors },
} = useForm({
mode: 'onChange',
Expand Down Expand Up @@ -155,6 +156,7 @@ function BasicEntityEdit (props: any) {
field,
register,
getValues,
control,
errors,
}} />
</Grid>
Expand Down
2 changes: 2 additions & 0 deletions src/lib/BasicEntityField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
import type { Spec } from './basic-types'

import { Gubu } from 'gubu'
import { BasicEntityAutocompleteField } from './BasicEntityAutocompleteField'

const CMPNAME = 'BasicEntityField'

Expand All @@ -26,6 +27,7 @@ const fieldMap: any = {
Date: BasicEntityDateField,
DateTime: BasicEntityDateTimeField,
Time: BasicEntityTimeField,
Autocomplete: BasicEntityAutocompleteField
}


Expand Down
Loading