toggleDropdown for chip to open and close, limiting tag chars
This commit is contained in:
@@ -28,6 +28,13 @@ function FilterAutocomplete({
|
|||||||
const [open, setOpen] = React.useState(false);
|
const [open, setOpen] = React.useState(false);
|
||||||
const [frozenValue, setFrozenValue] = React.useState<string[]>(value);
|
const [frozenValue, setFrozenValue] = React.useState<string[]>(value);
|
||||||
|
|
||||||
|
const toggleDropdown = () => {
|
||||||
|
setOpen(prev => {
|
||||||
|
const next = !prev;
|
||||||
|
setFrozenValue(value);
|
||||||
|
return next;
|
||||||
|
});
|
||||||
|
};
|
||||||
const sortedOptions = React.useMemo(() => {
|
const sortedOptions = React.useMemo(() => {
|
||||||
const sel = new Set(frozenValue);
|
const sel = new Set(frozenValue);
|
||||||
const picked: string[] = [];
|
const picked: string[] = [];
|
||||||
@@ -45,8 +52,8 @@ function FilterAutocomplete({
|
|||||||
freeSolo
|
freeSolo
|
||||||
disableCloseOnSelect
|
disableCloseOnSelect
|
||||||
open={open}
|
open={open}
|
||||||
onOpen={() => { setOpen(true); setFrozenValue(value); }}
|
onOpen={toggleDropdown}
|
||||||
onClose={() => { setOpen(false); setFrozenValue(value); }}
|
onClose={toggleDropdown}
|
||||||
options={sortedOptions}
|
options={sortedOptions}
|
||||||
value={value}
|
value={value}
|
||||||
getOptionKey={(option) => option}
|
getOptionKey={(option) => option}
|
||||||
@@ -73,9 +80,9 @@ function FilterAutocomplete({
|
|||||||
return <Chip
|
return <Chip
|
||||||
key={key}
|
key={key}
|
||||||
{...tagProps}
|
{...tagProps}
|
||||||
label={tag}
|
label={tag.length > 10 ? `${tag.slice(0, 8)}..` : tag}
|
||||||
size="small"
|
size="small"
|
||||||
onClick={() => setOpen(true)}
|
onClick={toggleDropdown}
|
||||||
sx={{ cursor: 'pointer' }}
|
sx={{ cursor: 'pointer' }}
|
||||||
/>;
|
/>;
|
||||||
})}
|
})}
|
||||||
@@ -83,7 +90,7 @@ function FilterAutocomplete({
|
|||||||
<Chip
|
<Chip
|
||||||
label={`+${tagValue.length - maxChips}`}
|
label={`+${tagValue.length - maxChips}`}
|
||||||
size="small"
|
size="small"
|
||||||
onClick={() => setOpen(true)}
|
onClick={toggleDropdown}
|
||||||
sx={{ cursor: 'pointer' }}
|
sx={{ cursor: 'pointer' }}
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
|
|||||||
Reference in New Issue
Block a user