-
Notifications
You must be signed in to change notification settings - Fork 4
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
TheMaskedTurtle
wants to merge
7
commits into
main
Choose a base branch
from
jorism/remove-free-solo
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5490226
fix(element-search): remove freesolo and improve typing
TheMaskedTurtle df1b73c
Merge branch 'main' into jorism/remove-free-solo
TheMaskedTurtle 8dbf8a5
fix: simplify unnecessary generic typing
TheMaskedTurtle 3b5d46f
Merge branch 'jorism/remove-free-solo' of github.com:gridsuite/common…
TheMaskedTurtle 42ce2a3
fix: set back selectionOption filter
TheMaskedTurtle 1b0d542
Merge remote-tracking branch 'origin/main' into jorism/remove-free-solo
TheMaskedTurtle ef23da5
fix: add translation for search disabled
TheMaskedTurtle File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -54,8 +53,8 @@ const ElementSearchDialog = (props: ElementSearchDialogProps) => { | |
|
||
const displayedValue = useMemo(() => { | ||
return searchTermDisabled || searchTermDisableReason | ||
? searchTermDisableReason | ||
: searchTerm; | ||
? searchTermDisableReason ?? 'search disabled' | ||
: searchTerm ?? ''; | ||
}, [searchTerm, searchTermDisabled, searchTermDisableReason]); | ||
|
||
const handleClose = useCallback(() => { | ||
|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. must restore There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
|
@@ -161,7 +129,7 @@ const ElementSearchDialog = (props: ElementSearchDialogProps) => { | |
</> | ||
), | ||
}} | ||
value={displayedValue ?? ''} | ||
value={displayedValue} | ||
/> | ||
)} | ||
disabled={searchTermDisabled} | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done