refactor(web): remove typescript
This commit is contained in:
@@ -1,39 +1,19 @@
|
||||
import * as React from 'react';
|
||||
import { Controller, useFormContext } from 'react-hook-form';
|
||||
import FormHelperText from '@mui/material/FormHelperText';
|
||||
import Autocomplete, {
|
||||
AutocompleteProps,
|
||||
createFilterOptions,
|
||||
} from '@mui/material/Autocomplete';
|
||||
import Autocomplete, { createFilterOptions } from '@mui/material/Autocomplete';
|
||||
import Typography from '@mui/material/Typography';
|
||||
import type { IFieldDropdownOption } from 'types';
|
||||
|
||||
interface ControlledAutocompleteProps
|
||||
extends AutocompleteProps<IFieldDropdownOption, boolean, boolean, boolean> {
|
||||
shouldUnregister?: boolean;
|
||||
name: string;
|
||||
required?: boolean;
|
||||
showOptionValue?: boolean;
|
||||
description?: string;
|
||||
dependsOn?: string[];
|
||||
}
|
||||
|
||||
const getOption = (options: readonly IFieldDropdownOption[], value: string) =>
|
||||
const getOption = (options, value) =>
|
||||
options.find((option) => option.value === value) || null;
|
||||
|
||||
// Enables filtering by value in autocomplete dropdown
|
||||
const filterOptions = createFilterOptions<IFieldDropdownOption>({
|
||||
const filterOptions = createFilterOptions({
|
||||
stringify: ({ label, value }) => `
|
||||
${label}
|
||||
${value}
|
||||
`,
|
||||
});
|
||||
|
||||
function ControlledAutocomplete(
|
||||
props: ControlledAutocompleteProps
|
||||
): React.ReactElement {
|
||||
function ControlledAutocomplete(props) {
|
||||
const { control, watch, setValue, resetField } = useFormContext();
|
||||
|
||||
const {
|
||||
required = false,
|
||||
name,
|
||||
@@ -47,23 +27,19 @@ function ControlledAutocomplete(
|
||||
showOptionValue,
|
||||
...autocompleteProps
|
||||
} = props;
|
||||
|
||||
let dependsOnValues: unknown[] = [];
|
||||
let dependsOnValues = [];
|
||||
if (dependsOn?.length) {
|
||||
dependsOnValues = watch(dependsOn);
|
||||
}
|
||||
|
||||
React.useEffect(() => {
|
||||
const hasDependencies = dependsOnValues.length;
|
||||
const allDepsSatisfied = dependsOnValues.every(Boolean);
|
||||
|
||||
if (hasDependencies && !allDepsSatisfied) {
|
||||
// Reset the field if any dependency is not satisfied
|
||||
setValue(name, null);
|
||||
resetField(name);
|
||||
}
|
||||
}, dependsOnValues);
|
||||
|
||||
return (
|
||||
<Controller
|
||||
rules={{ required }}
|
||||
@@ -89,20 +65,18 @@ function ControlledAutocomplete(
|
||||
filterOptions={filterOptions}
|
||||
value={getOption(options, field.value)}
|
||||
onChange={(event, selectedOption, reason, details) => {
|
||||
const typedSelectedOption =
|
||||
selectedOption as IFieldDropdownOption;
|
||||
const typedSelectedOption = selectedOption;
|
||||
if (
|
||||
typedSelectedOption !== null &&
|
||||
Object.prototype.hasOwnProperty.call(
|
||||
typedSelectedOption,
|
||||
'value'
|
||||
'value',
|
||||
)
|
||||
) {
|
||||
controllerOnChange(typedSelectedOption.value);
|
||||
} else {
|
||||
controllerOnChange(typedSelectedOption);
|
||||
}
|
||||
|
||||
onChange?.(event, selectedOption, reason, details);
|
||||
}}
|
||||
onBlur={(...args) => {
|
||||
@@ -139,5 +113,4 @@ function ControlledAutocomplete(
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
export default ControlledAutocomplete;
|
Reference in New Issue
Block a user