import { Box, IconButton, InputBase, ListItemButton, Typography } from '@mui/material' import Collapse from '@mui/material/Collapse' import React, { ChangeEvent, ReactElement, useState } from 'react' import type { RemixiconReactIconProps } from 'remixicon-react' import Check from 'remixicon-react/CheckLineIcon' import X from 'remixicon-react/CloseLineIcon' import Edit from 'remixicon-react/PencilLineIcon' import Minus from 'remixicon-react/SubtractLineIcon' import { makeStyles } from 'tss-react/mui' import ExpandableListItemActions from './ExpandableListItemActions' import ExpandableListItemNote from './ExpandableListItemNote' import { SwarmButton } from './SwarmButton' const useStyles = makeStyles()(theme => ({ header: { backgroundColor: theme.palette.background.paper, marginBottom: theme.spacing(0.25), borderLeft: `${theme.spacing(0.25)} solid rgba(0,0,0,0)`, wordBreak: 'break-word', '&:hover': { backgroundColor: theme.palette.background.paper, }, '&:focus-within': { backgroundColor: theme.palette.background.paper, }, }, headerOpen: { borderLeft: `${theme.spacing(0.25)} 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 confirmIcon?: React.ComponentType loading?: boolean onChange?: (value: string) => void onConfirm?: (value: string) => void mapperFn?: (value: string) => string locked?: boolean } export default function ExpandableListItemInput({ label, value = '', placeholder, helperText, expandedOnly, confirmLabel, confirmLabelDisabled, confirmIcon, loading, onChange, onConfirm, 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) => { let newValue = e.target.value if (mapperFn) { newValue = mapperFn(newValue) } setInputValue(newValue) if (onChange) onChange(newValue) } return ( <> {label && ( {label} )} {!open && value && ( {value} )} {!expandedOnly && !locked && ( {open ? : } )} {helperText && {helperText}} { onConfirm?.(inputValue.trim()) }} > {confirmLabel || 'Save'} setInputValue(value || '')} cancel > Cancel ) }