Skip to content

Commit

Permalink
feat(autocomplete): pre select option (if any option matching the val…
Browse files Browse the repository at this point in the history
…ue was found)
  • Loading branch information
ruiaraujo012 committed Aug 10, 2024
1 parent b3af506 commit f5a4a14
Showing 1 changed file with 74 additions and 57 deletions.
131 changes: 74 additions & 57 deletions src/components/AutocompleteController.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Autocomplete, CircularProgress, TextField } from '@mui/material';
import { Controller } from 'react-hook-form';
import { Controller, useController } from 'react-hook-form';

Check warning on line 2 in src/components/AutocompleteController.tsx

View workflow job for this annotation

GitHub Actions / Publish

'Controller' is defined but never used

Check warning on line 2 in src/components/AutocompleteController.tsx

View workflow job for this annotation

GitHub Actions / Publish

'Controller' is defined but never used

Check warning on line 2 in src/components/AutocompleteController.tsx

View workflow job for this annotation

GitHub Actions / Publish

'Controller' is defined but never used

Check warning on line 2 in src/components/AutocompleteController.tsx

View workflow job for this annotation

GitHub Actions / Publish

'Controller' is defined but never used
import {
useAsyncFieldControllerLabels,
useFieldControllerLabels,
useFieldControllerWithOptionsLabels,
useOnErrorMessage,
} from '../hooks/index';
import { useState } from 'react';
import { useEffect, useState } from 'react';
import type {
AsyncFieldControllerProps,
FieldControllerProps,
Expand Down Expand Up @@ -87,67 +87,84 @@ export const AutocompleteController = <
FreeSolo
> | null>(initialValue ?? null);

const {
field: { onChange, value, ...other },
fieldState: { invalid, error },
} = useController({
name,
control,

Check warning on line 95 in src/components/AutocompleteController.tsx

View workflow job for this annotation

GitHub Actions / Publish

Expected object keys to be in natural ascending order. 'control' should be before 'name'

Check warning on line 95 in src/components/AutocompleteController.tsx

View workflow job for this annotation

GitHub Actions / Publish

Expected object keys to be in natural ascending order. 'control' should be before 'name'
});

/**
* Side effects
*/
useEffect(() => {
if (value) {
const optionFound = options.find((option) => optionValueAccessor(option) === value);
if (optionFound) {

Check failure on line 104 in src/components/AutocompleteController.tsx

View workflow job for this annotation

GitHub Actions / Publish

Expected blank line before this statement

Check failure on line 104 in src/components/AutocompleteController.tsx

View workflow job for this annotation

GitHub Actions / Publish

Expected blank line before this statement
setSelectedValue(optionFound as AutocompleteValue<Value, Multiple, DisableClearable, FreeSolo> | null);
}
}
}, [value]);

Check warning on line 108 in src/components/AutocompleteController.tsx

View workflow job for this annotation

GitHub Actions / Publish

React Hook useEffect has missing dependencies: 'optionValueAccessor' and 'options'. Either include them or remove the dependency array. If 'optionValueAccessor' changes too often, find the parent component that defines it and wrap that definition in useCallback

Check warning on line 108 in src/components/AutocompleteController.tsx

View workflow job for this annotation

GitHub Actions / Publish

React Hook useEffect has missing dependencies: 'optionValueAccessor' and 'options'. Either include them or remove the dependency array. If 'optionValueAccessor' changes too often, find the parent component that defines it and wrap that definition in useCallback

/**
* Render
*/
return (
<Controller
control={control}
name={name}
render={({ field: { onChange, ...other }, fieldState: { invalid, error } }) => (
<Autocomplete
{...other}
aria-required={optional ? 'false' : 'true'}
getOptionLabel={(value) => {
if (!value) {
return '';
} else if (getOptionLabel) {
return getOptionLabel(value);
}
<Autocomplete
{...other}
aria-required={optional ? 'false' : 'true'}
getOptionLabel={(value) => {
if (!value) {
return '';
} else if (getOptionLabel) {
return getOptionLabel(value);
}

return '';
}}
isOptionEqualToValue={(option, value) => {
if (!value) {
return false;
} else if (isOptionEqualToValue) {
return isOptionEqualToValue(option, value);
}
return '';
}}
isOptionEqualToValue={(option, value) => {
if (!value) {
return false;
} else if (isOptionEqualToValue) {
return isOptionEqualToValue(option, value);
}

return false;
}}
loading={loading}
loadingText={fieldControllerLoadingLabel}
noOptionsText={loadingError ? fieldControllerLoadingErrorLabel : fieldControllerNoOptionsLabel}
onChange={(_: unknown, newValue) => {
setSelectedValue(newValue ?? null);
onChange(optionValueAccessor(newValue));
return false;
}}
loading={loading}
loadingText={fieldControllerLoadingLabel}
noOptionsText={loadingError ? fieldControllerLoadingErrorLabel : fieldControllerNoOptionsLabel}
onChange={(_: unknown, newValue) => {
setSelectedValue(newValue ?? null);
onChange(optionValueAccessor(newValue));
}}
options={options ?? []}
renderInput={(params) => (
<TextField
{...params}
InputProps={{
...params.InputProps,
endAdornment: (
<>
{loading ? (
<CircularProgress
color='primary'
size={20}
/>
) : null}
{params.InputProps.endAdornment}
</>
),
}}
options={options ?? []}
renderInput={(params) => (
<TextField
{...params}
InputProps={{
...params.InputProps,
endAdornment: (
<>
{loading ? (
<CircularProgress
color='primary'
size={20}
/>
) : null}
{params.InputProps.endAdornment}
</>
),
}}
{...muiProps?.textField}
error={invalid}
helperText={error?.message ? fieldOnErrorMessage(error?.message) : null}
label={fieldControllerLabel}
/>
)}
value={selectedValue as AutocompleteValue<Value, Multiple, DisableClearable, FreeSolo>}
{...autocompleteProps}
{...muiProps?.textField}
error={invalid}
helperText={error?.message ? fieldOnErrorMessage(error?.message) : null}
label={fieldControllerLabel}
/>
)}
value={selectedValue as AutocompleteValue<Value, Multiple, DisableClearable, FreeSolo>}
{...autocompleteProps}
/>
);
};

0 comments on commit f5a4a14

Please sign in to comment.