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

fix(element-search): remove freesolo and improve typing #456

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
74 changes: 21 additions & 53 deletions src/components/ElementSearchDialog/element-search-dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,28 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

import { ReactNode, useCallback, useMemo } from 'react';
import {
Autocomplete,
Dialog,
DialogContent,
TextField,
AutocompleteInputChangeReason,
AutocompleteChangeReason,
} from '@mui/material';
import { HTMLAttributes, ReactNode, useCallback, useMemo } from 'react';
import { Autocomplete, Dialog, DialogContent, TextField } from '@mui/material';
import { Search, SearchOff } from '@mui/icons-material';
import { useIntl } from 'react-intl';
import { EquipmentInfos } from '../../index';
import * as React from 'react';

interface ElementSearchDialogProps {
export type RenderElementProps = HTMLAttributes<HTMLLIElement> & {
element: EquipmentInfos;
inputValue: string;
onClose: () => void;
};

export interface ElementSearchDialogProps {
open: boolean;
onClose: () => void;
searchingLabel?: string;
searchTerm: string;
onSearchTermChange: (searchTerm: string) => void;
onSelectionChange: (selection: EquipmentInfos) => void;
elementsFound: EquipmentInfos[];
renderElement: (props: any) => ReactNode;
renderElement: (props: RenderElementProps) => ReactNode;
searchTermDisabled?: boolean;
searchTermDisableReason?: string;
isLoading: boolean;
Expand All @@ -54,8 +53,8 @@ const ElementSearchDialog = (props: ElementSearchDialogProps) => {

const displayedValue = useMemo(() => {
return searchTermDisabled || searchTermDisableReason
? searchTermDisableReason
: searchTerm;
? searchTermDisableReason ?? 'search disabled'
Copy link
Contributor

Choose a reason for hiding this comment

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

manage translation for this default value

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

: searchTerm ?? '';
}, [searchTerm, searchTermDisabled, searchTermDisableReason]);

const handleClose = useCallback(() => {
Expand All @@ -77,60 +76,29 @@ const ElementSearchDialog = (props: ElementSearchDialogProps) => {
id="element-search"
forcePopupIcon={false}
fullWidth
freeSolo
onInputChange={(
_event: React.SyntheticEvent,
value: string,
reason: AutocompleteInputChangeReason
) => {
// if reason is equal to "reset", it means it programmatically changed (by selecting a result)
// we don't want to change "searchTerm" when clicking a result
if (!searchTermDisabled && reason !== 'reset') {
onInputChange={(_event, value) => {
if (!searchTermDisabled) {
Copy link
Contributor

Choose a reason for hiding this comment

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

must restore reason to avoid calling onSearchTermChange when selecting an option. Otherwise it will fetch serach one more time

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

onSearchTermChange(value);
}
}}
onChange={(
_event: React.SyntheticEvent,
newValue: string | EquipmentInfos | null,
reason?: AutocompleteChangeReason
) => {
onChange={(_event, newValue, reason) => {
// when calling this method with reason == "selectOption", newValue can't be null or of type "string", since an option has been clicked on
if (
newValue != null &&
typeof newValue !== 'string' &&
reason === 'selectOption'
) {
if (newValue !== null && reason === 'selectOption') {
onSelectionChange(newValue);
}
}}
getOptionLabel={(option: EquipmentInfos | string) =>
typeof option === 'string' ? option : option.label
getOptionLabel={(option) => option.label}
isOptionEqualToValue={(option, value) =>
option.id === value.id
}
isOptionEqualToValue={(
option: EquipmentInfos | string,
value: EquipmentInfos | string
) => {
if (
typeof option === 'string' ||
typeof value === 'string'
) {
return option === value;
} else {
return option.id === value.id;
}
}}
options={isLoading ? [] : elementsFound}
loading={isLoading}
loadingText={loadingText}
autoHighlight={true}
noOptionsText={intl.formatMessage({
id: 'element_search/noResult',
})}
renderOption={(
optionProps: any,
element: EquipmentInfos | string,
{ inputValue }: { inputValue: string }
) =>
renderOption={(optionProps, element, { inputValue }) =>
renderElement({
...optionProps,
element,
Expand Down Expand Up @@ -161,7 +129,7 @@ const ElementSearchDialog = (props: ElementSearchDialogProps) => {
</>
),
}}
value={displayedValue ?? ''}
value={displayedValue}
/>
)}
disabled={searchTermDisabled}
Expand Down
Loading