import { FC, useState } from 'react'; import { FormControl as MUIFormControl, FormControlProps as MUIFormControlProps, IconButton as MUIIconButton, InputAdornment as MUIInputAdornment, } from '@mui/material'; import { QuestionMark as MUIQuestionMarkIcon } from '@mui/icons-material'; import { GREY } from '../lib/consts/DEFAULT_THEME'; import InputMessageBox from './InputMessageBox'; import { MessageBoxProps } from './MessageBox'; import OutlinedInput, { OutlinedInputProps } from './OutlinedInput'; import OutlinedInputLabel, { OutlinedInputLabelProps, } from './OutlinedInputLabel'; type OutlinedInputWithLabelOptionalProps = { formControlProps?: Partial; helpMessageBoxProps?: Partial; id?: string; inputProps?: Partial; inputLabelProps?: Partial; messageBoxProps?: Partial; onChange?: OutlinedInputProps['onChange']; value?: OutlinedInputProps['value']; }; type OutlinedInputWithLabelProps = { label: string; } & OutlinedInputWithLabelOptionalProps; const OUTLINED_INPUT_WITH_LABEL_DEFAULT_PROPS: Required< Omit > & Pick = { formControlProps: {}, helpMessageBoxProps: {}, id: '', inputProps: {}, inputLabelProps: {}, messageBoxProps: {}, onChange: undefined, value: '', }; const OutlinedInputWithLabel: FC = ({ formControlProps = OUTLINED_INPUT_WITH_LABEL_DEFAULT_PROPS.formControlProps, helpMessageBoxProps = OUTLINED_INPUT_WITH_LABEL_DEFAULT_PROPS.helpMessageBoxProps, id = OUTLINED_INPUT_WITH_LABEL_DEFAULT_PROPS.id, inputProps: { endAdornment, ...restInputProps } = OUTLINED_INPUT_WITH_LABEL_DEFAULT_PROPS.inputProps, inputLabelProps = OUTLINED_INPUT_WITH_LABEL_DEFAULT_PROPS.inputLabelProps, label, messageBoxProps = OUTLINED_INPUT_WITH_LABEL_DEFAULT_PROPS.messageBoxProps, onChange, value = OUTLINED_INPUT_WITH_LABEL_DEFAULT_PROPS.value, }) => { const { text: helpText } = helpMessageBoxProps; const [isShowHelp, setIsShowHelp] = useState(false); return ( {label} :not(:first-child)': { marginLeft: '.3em', }, }} > {endAdornment} {helpText && ( { setIsShowHelp(true); }} sx={{ color: GREY, padding: '.1em', }} tabIndex={-1} > )} ), fullWidth: formControlProps.fullWidth, id, label, onChange, value, ...restInputProps, }} /> {isShowHelp && ( { setIsShowHelp(false); }, ...helpMessageBoxProps, }} /> )} ); }; OutlinedInputWithLabel.defaultProps = OUTLINED_INPUT_WITH_LABEL_DEFAULT_PROPS; export type { OutlinedInputWithLabelProps }; export default OutlinedInputWithLabel;