1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-09-28 17:55:15 +02:00
unleash.unleash/frontend/src/component/common/ItemList/ItemList.tsx
Nuno Góis 4167a60588
feat: biome lint frontend (#4903)
Follows up on https://github.com/Unleash/unleash/pull/4853 to add Biome
to the frontend as well.


![image](https://github.com/Unleash/unleash/assets/14320932/1906faf1-fc29-4172-a4d4-b2716d72cd65)

Added a few `biome-ignore` to speed up the process but we may want to
check and fix them in the future.
2023-10-02 13:25:46 +01:00

77 lines
2.2 KiB
TypeScript

import { Add } from '@mui/icons-material';
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 = () => {
onChange((prev) => [...prev, inputValue]);
setInputValue('');
};
const removeItem = (value: string) => {
onChange((prev) => prev.filter((item) => item !== value));
};
return (
<div {...props}>
<StyledItemListAdd>
<Input
label={label}
value={inputValue}
onChange={(e) => setInputValue(e.target.value)}
onKeyPress={(e) => {
if (e.key === 'Enter') {
addItem();
}
}}
/>
<Button
startIcon={<Add />}
onClick={addItem}
variant='outlined'
color='primary'
disabled={!inputValue.trim() || value.includes(inputValue)}
>
Add
</Button>
</StyledItemListAdd>
<Stack flexDirection='row' flexWrap={'wrap'} gap={1}>
{value?.map((item, index) => (
<Chip
// biome-ignore lint/suspicious/noArrayIndexKey: <explanation>
key={index}
label={item}
onDelete={() => removeItem(item)}
/>
))}
</Stack>
</div>
);
};