import React, { useState } from 'react'; import { makeStyles, createStyles, Button, Dialog, Grow, DialogActions, DialogContent, DialogTitle, TextField, } from '@material-ui/core'; import { TransitionProps } from '@material-ui/core/transitions'; import AddCircleIcon from '@material-ui/icons/AddCircle'; import AddBoxIcon from '@material-ui/icons/AddBoxSharp'; import { AddItemProps } from './props'; const Transition = React.forwardRef(function Transition( props: TransitionProps & { children?: React.ReactElement }, ref: React.Ref ) { return ; }); const useStyles = makeStyles(theme => createStyles({ root: { margin: theme.spacing(0.75) }, startIcon: { marginLeft: -1, marginRight: theme.spacing(2), }, iconSizeMedium: { '& > *:first-child': { fontSize: 24 }, }, }) ); export default function AddItem({ multiple, value, onChange, AddButtonProps, AddDialogProps, disabled = false, }: AddItemProps) { const classes = useStyles(); const [open, setOpen] = useState(false); const handleClose = () => setOpen(false); const [toAdd, setToAdd] = useState(''); const handleAdd = () => { if (multiple) onChange( {} as any, [ ...((Array.isArray(value) ? value : []) as any), { value: toAdd, label: toAdd }, ] as any, 'create-option' ); else onChange( {} as any, { value: toAdd, label: toAdd } as any, 'create-option' ); setToAdd(''); setTimeout(() => setOpen(false)); }; return ( <> {AddDialogProps?.title || 'Add Item to List'} setToAdd(e.target.value)} onKeyDown={e => { if (e.key === 'Enter') handleAdd(); }} /> ); }