2024-03-12 10:56:10 +01:00
|
|
|
import Add from '@mui/icons-material/Add';
|
2022-10-13 12:34:47 +02:00
|
|
|
import { Button, Chip, Stack, styled } from '@mui/material';
|
|
|
|
import Input from 'component/common/Input/Input';
|
|
|
|
import { useState } from 'react';
|
|
|
|
|
|
|
|
const StyledItemListAdd = styled('div')(({ theme }) => ({
|
|
|
|
display: 'flex',
|
|
|
|
marginBottom: theme.spacing(1),
|
|
|
|
'& > div:first-of-type': {
|
|
|
|
width: '100%',
|
|
|
|
marginRight: theme.spacing(1),
|
|
|
|
'& > div:first-of-type': {
|
|
|
|
width: '100%',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}));
|
|
|
|
|
|
|
|
interface IItemListProps {
|
|
|
|
label: string;
|
|
|
|
value: string[];
|
|
|
|
onChange: React.Dispatch<React.SetStateAction<string[]>>;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const ItemList = ({
|
|
|
|
label,
|
|
|
|
value,
|
|
|
|
onChange,
|
|
|
|
...props
|
|
|
|
}: IItemListProps) => {
|
|
|
|
const [inputValue, setInputValue] = useState('');
|
|
|
|
|
|
|
|
const addItem = () => {
|
2023-10-02 14:25:46 +02:00
|
|
|
onChange((prev) => [...prev, inputValue]);
|
2022-10-13 12:34:47 +02:00
|
|
|
setInputValue('');
|
|
|
|
};
|
|
|
|
|
|
|
|
const removeItem = (value: string) => {
|
2023-10-02 14:25:46 +02:00
|
|
|
onChange((prev) => prev.filter((item) => item !== value));
|
2022-10-13 12:34:47 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div {...props}>
|
|
|
|
<StyledItemListAdd>
|
|
|
|
<Input
|
|
|
|
label={label}
|
|
|
|
value={inputValue}
|
2023-10-02 14:25:46 +02:00
|
|
|
onChange={(e) => setInputValue(e.target.value)}
|
|
|
|
onKeyPress={(e) => {
|
2022-10-13 12:34:47 +02:00
|
|
|
if (e.key === 'Enter') {
|
|
|
|
addItem();
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
<Button
|
|
|
|
startIcon={<Add />}
|
|
|
|
onClick={addItem}
|
2023-10-02 14:25:46 +02:00
|
|
|
variant='outlined'
|
|
|
|
color='primary'
|
2022-10-13 12:34:47 +02:00
|
|
|
disabled={!inputValue.trim() || value.includes(inputValue)}
|
|
|
|
>
|
|
|
|
Add
|
|
|
|
</Button>
|
|
|
|
</StyledItemListAdd>
|
2023-10-02 14:25:46 +02:00
|
|
|
<Stack flexDirection='row' flexWrap={'wrap'} gap={1}>
|
2022-10-13 12:34:47 +02:00
|
|
|
{value?.map((item, index) => (
|
|
|
|
<Chip
|
|
|
|
key={index}
|
|
|
|
label={item}
|
|
|
|
onDelete={() => removeItem(item)}
|
|
|
|
/>
|
|
|
|
))}
|
|
|
|
</Stack>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|