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 all 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 };
335 changes: 163 additions & 172 deletions dist/voxgig-model-react.es.js

Large diffs are not rendered by default.

333 changes: 162 additions & 171 deletions dist/voxgig-model-react.umd.js

Large diffs are not rendered by default.

117 changes: 117 additions & 0 deletions src/lib/BasicEntityAutocompleteField.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
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, Exact, Gubu } from 'gubu'
const CMPNAME = 'BasicEntityAutocompleteField'

const { Open } = Gubu
const BasicEntityAutocompleteFieldSpecShape = Gubu(Open({
field: Open({
id: String,
name: String,
kind: String,
label: Default(''),
options: Open({
label: { field: Default('label') },
value: { field: Default('value') },
multiple: Default(false),
default: Open({}),
ents: Open({})
}),
ux: Open({
kind: Exact('Autocomplete'),
edit: Default(true),
})
})
}), {name: CMPNAME})

function BasicEntityAutocompleteField(props: any) {
const { spec } = props

const basicEntityAutocompleteField: Spec = BasicEntityAutocompleteFieldSpecShape(spec)
const { control, field } = basicEntityAutocompleteField
const { resolvedOptions, resolvedDefault } = resolveOptions(field.options);

return (
<Controller
key={`${field.id}-controller`}
name={field.name}
control={control}
defaultValue={resolvedDefault}
render={({ field: { onChange, value } }) => (
<Autocomplete
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[field.options.label.field]}
value={resolveValue(field.options, value)}
onChange={(_, newVal: any) => onChange(newVal)}
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) {
const { multiple, ents, label, value, default: defaultValues } = options;
const labelField = label?.field;
const valueField = value?.field;

// Array of options
const resolvedOptions = Object.keys(ents).map(key => ({
[labelField]: ents?.[key]?.[labelField],
[valueField]: key
}));

let resolvedDefault;
if (multiple === false) {
if (Object.keys(defaultValues).length > 0) {
const firstKey = Object.keys(defaultValues)[0];
resolvedDefault = { value: firstKey, label: defaultValues[firstKey][labelField] };
} else {
resolvedDefault = null;
}
} else {
resolvedDefault = Object.keys(defaultValues).map(key => ({
label: defaultValues[key].label,
value: key
}));
}

return {
resolvedOptions,
resolvedDefault
};
}

function resolveValue(options: any, val: any) {
const { multiple, ents, label, value } = options;
const labelField = label?.field;
const valueField = value?.field;

const getValue = (val: string) => ents?.[val]?.[labelField] ? { [valueField]: val, [labelField]: ents[val][labelField] || val } : undefined;

if (!multiple) {
return typeof val === 'string' ? getValue(val) : (val || undefined);
} else {
if (typeof val === 'string') {
const resolvedValue = getValue(val);
return resolvedValue ? [resolvedValue] : [];
}
return val || [];
}
}

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