import React from 'react'; import { useIntl } from 'react-intl'; import AddIcon from '@mui/icons-material/Add'; import TextField from '@mui/material/TextField'; import { Label } from '../../../../classes/client'; import { StyledButton, NewLabelContainer, NewLabelColor, CreateLabel } from './styled'; import { Tooltip } from '@mui/material'; const labelColors = [ '#00b327', '#0565ff', '#2d2dd6', '#6a00ba', '#ad1599', '#ff1e35', '#ff6600', '#ffff47', ]; type AddLabelFormProps = { onAdd: (newLabel: Label) => void; }; const AddLabelDialog = ({ onAdd }: AddLabelFormProps): React.ReactElement => { const intl = useIntl(); const [createLabelColorIndex, setCreateLabelColorIndex] = React.useState( Math.floor(Math.random() * labelColors.length) ); const [newLabelTitle, setNewLabelTitle] = React.useState(''); const newLabelColor = labelColors[createLabelColorIndex]; const setNextLabelColorIndex = () => { const nextIndex = labelColors[createLabelColorIndex + 1] ? createLabelColorIndex + 1 : 0; setCreateLabelColorIndex(nextIndex); }; const handleSubmitNew = () => { onAdd({ title: newLabelTitle, color: newLabelColor, id: 0, }); setNewLabelTitle(''); setNextLabelColorIndex(); }; return ( { e.stopPropagation(); setNextLabelColorIndex(); }} /> setNewLabelTitle(e.target.value)} onKeyPress={(e) => { if (e.key === 'Enter') { handleSubmitNew(); } }} value={newLabelTitle} /> handleSubmitNew()} disabled={!newLabelTitle.length} aria-label={intl.formatMessage({ id: 'label.add-button', defaultMessage: 'Add label', })} > ); } export default AddLabelDialog;