47 lines
1.3 KiB
JavaScript
47 lines
1.3 KiB
JavaScript
|
|
import * as React from 'react';
|
||
|
|
import Select from '@mui/joy/Select';
|
||
|
|
import Option from '@mui/joy/Option';
|
||
|
|
import IconButton from '@mui/joy/IconButton';
|
||
|
|
import CloseRounded from '@mui/icons-material/CloseRounded';
|
||
|
|
|
||
|
|
export default function SelectClearable() {
|
||
|
|
const [value, setValue] = React.useState('dog');
|
||
|
|
const action = React.useRef(null);
|
||
|
|
return (
|
||
|
|
<Select
|
||
|
|
action={action}
|
||
|
|
value={value}
|
||
|
|
placeholder="Favorite pet…"
|
||
|
|
onChange={(event, newValue) => setValue(newValue)}
|
||
|
|
{...(value && {
|
||
|
|
// display the button and remove select indicator
|
||
|
|
// when user has selected a value
|
||
|
|
endDecorator: (
|
||
|
|
<IconButton
|
||
|
|
size="sm"
|
||
|
|
variant="plain"
|
||
|
|
color="neutral"
|
||
|
|
onMouseDown={(event) => {
|
||
|
|
// don't open the popup when clicking on this button
|
||
|
|
event.stopPropagation();
|
||
|
|
}}
|
||
|
|
onClick={() => {
|
||
|
|
setValue(null);
|
||
|
|
action.current?.focusVisible();
|
||
|
|
}}
|
||
|
|
>
|
||
|
|
<CloseRounded />
|
||
|
|
</IconButton>
|
||
|
|
),
|
||
|
|
indicator: null,
|
||
|
|
})}
|
||
|
|
sx={{ minWidth: 160 }}
|
||
|
|
>
|
||
|
|
<Option value="dog">Dog</Option>
|
||
|
|
<Option value="cat">Cat</Option>
|
||
|
|
<Option value="fish">Fish</Option>
|
||
|
|
<Option value="bird">Bird</Option>
|
||
|
|
</Select>
|
||
|
|
);
|
||
|
|
}
|