import { Grid, IconButton, InputBase, ListItem, Typography } from '@material-ui/core' import Collapse from '@material-ui/core/Collapse' import { createStyles, makeStyles, Theme } from '@material-ui/core/styles' import { ChangeEvent, ReactElement, useState } from 'react' import { Edit, Minus, Search, X } from 'react-feather' import ExpandableListItemActions from './ExpandableListItemActions' import ExpandableListItemNote from './ExpandableListItemNote' import { SwarmButton } from './SwarmButton' const useStyles = makeStyles((theme: Theme) => createStyles({ header: { backgroundColor: theme.palette.background.paper, marginBottom: theme.spacing(0.25), borderLeft: `${theme.spacing(0.25)}px solid rgba(0,0,0,0)`, wordBreak: 'break-word', }, headerOpen: { borderLeft: `${theme.spacing(0.25)}px solid ${theme.palette.primary.main}`, }, copyValue: { cursor: 'pointer', padding: theme.spacing(1), borderRadius: 0, '&:hover': { backgroundColor: '#fcf2e8', color: theme.palette.primary.main, }, }, content: { marginTop: theme.spacing(1), marginBottom: theme.spacing(1), }, keyMargin: { marginRight: theme.spacing(1), }, unselectableLabel: { cursor: 'default', userSelect: 'none', // Many browsers don't support yet the general user-select css property WebkitUserSelect: 'none', MozUserSelect: 'none', msUserSelect: 'none', }, }), ) interface Props { label: string value?: string placeholder?: string helperText?: string expandedOnly?: boolean confirmLabel?: string confirmLabelDisabled?: boolean loading?: boolean onChange?: (value: string) => void onConfirm?: (value: string) => void mapperFn?: (value: string) => string locked?: boolean } export default function ExpandableListItemKey({ label, value, onConfirm, onChange, confirmLabel, confirmLabelDisabled, expandedOnly, helperText, placeholder, loading, mapperFn, locked, }: Props): ReactElement | null { const classes = useStyles() const [open, setOpen] = useState(Boolean(expandedOnly)) const [inputValue, setInputValue] = useState(value || '') const toggleOpen = () => setOpen(!open) const handleChange = (e: ChangeEvent) => { if (mapperFn) { e.target.value = mapperFn(e.target.value) } setInputValue(e.target.value) if (onChange) onChange(e.target.value) } return ( <> {label && ( {label} )}
{!open && value} {!expandedOnly && !locked && ( {open ? ( ) : ( )} )}
{helperText && {helperText}} { if (onConfirm) onConfirm(inputValue) }} > {confirmLabel || 'Save'} setInputValue(value || '')} cancel > Cancel ) }