Skip to content
Snippets Groups Projects
Verified Commit ab7d7b94 authored by Defendi Alberto's avatar Defendi Alberto
Browse files

Install controller.

parent 3b9d3003
No related branches found
No related tags found
2 merge requests!60New component to search senior (see #12). Enhance responsiveness and solve #10 and #11,!58Camelize data and insert react-hook-form in autocomplete.
...@@ -4,50 +4,68 @@ import TextField from '@material-ui/core/TextField'; ...@@ -4,50 +4,68 @@ import TextField from '@material-ui/core/TextField';
import Autocomplete from '@material-ui/lab/Autocomplete'; import Autocomplete from '@material-ui/lab/Autocomplete';
import { getSeniorList } from 'api/getSeniorList'; import { getSeniorList } from 'api/getSeniorList';
import { ResponseProps } from 'api/ResponseProps'; import { ResponseProps } from 'api/ResponseProps';
import { Control, Controller } from 'react-hook-form';
import { ReservationProps } from './ReservationProps';
export const SeniorSearch: FC = () => { type SeniorSearchProps = {
control: Control<ReservationProps>;
};
export const SeniorSearch: FC<SeniorSearchProps> = ({
control,
}: SeniorSearchProps) => {
const [value, setValue] = useState<ResponseProps | null>(null); const [value, setValue] = useState<ResponseProps | null>(null);
const [seniors, setSeniors] = useState<ResponseProps[]>([]); const [seniors, setSeniors] = useState<ResponseProps[]>([]);
const [query, setQuery] = useState<string>('');
// FIX: Input closing when the data is changed.
// The issue is caused by the stopped re-rendering of react hook form. Consider
// attaching the value manually or to isolate this component.
const handleChange = (newValue: string): void => { const handleChange = (newValue: string): void => {
if (newValue !== undefined) { const MIN_SEARCH_LENGTH = 4;
const MIN_SEARCH_LENGTH = 3; if (newValue.length >= MIN_SEARCH_LENGTH && value == null) {
if (newValue.length >= MIN_SEARCH_LENGTH) getSeniorList(newValue).then((list) => {
getSeniorList(newValue).then((list) => { if (list.length !== 0) {
setSeniors(list); setSeniors(list);
}); } else {
setSeniors([]);
}
});
} }
}; };
return ( return (
<Autocomplete <Controller
value={value} control={control}
id="senior-searcher" name="senior"
onInputChange={(event, newValue) => handleChange(newValue)} as={({ onChange }) => (
onChange={(event, newValue) => { <Autocomplete
setValue(newValue); value={value}
}} id="senior-searcher"
options={seniors} onInputChange={(_, newValue) => handleChange(newValue)}
getOptionSelected={(option, val) => option.id === val.id} onChange={(_, val) => {
getOptionLabel={(option) => { onChange(val?.id);
if (Object.prototype.hasOwnProperty.call({ user: true }, 'user')) { setValue(val);
return option.user.lastName;
}
return '';
}}
renderOption={(option) => (
<>
{option.user.firstName} {option.user.lastName} {option.user.username}
</>
)}
renderInput={(params) => (
<TextField
{...params}
label="Search a senior"
variant="outlined"
InputProps={{
...params.InputProps,
}} }}
options={seniors}
getOptionSelected={(option, val) => option.id === val.id}
getOptionLabel={(option) => option.user.lastName}
renderOption={(option) => (
<>
{option.user.firstName} {option.user.lastName}{' '}
{option.user.username} {option.memberCardNumber}
</>
)}
renderInput={(params) => (
<TextField
{...params}
label="Search a senior"
variant="outlined"
InputProps={{
...params.InputProps,
}}
/>
)}
/> />
)} )}
/> />
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment