import * as React from 'react'; import { Controller, useFormContext } from 'react-hook-form'; import MuiTextField, { TextFieldProps as MuiTextFieldProps, } from '@mui/material/TextField'; import IconButton from '@mui/material/IconButton'; import InputAdornment from '@mui/material/InputAdornment'; import ContentCopyIcon from '@mui/icons-material/ContentCopy'; import copyInputValue from 'helpers/copyInputValue'; type TextFieldProps = { shouldUnregister?: boolean; name: string; clickToCopy?: boolean; readOnly?: boolean; } & MuiTextFieldProps; const createCopyAdornment = ( ref: React.RefObject ): React.ReactElement => { return ( copyInputValue(ref.current as HTMLInputElement)} edge="end" > ); }; export default function TextField(props: TextFieldProps): React.ReactElement { const { control } = useFormContext(); const inputRef = React.useRef(null); const { required, name, defaultValue, shouldUnregister = true, clickToCopy = false, readOnly = false, disabled = false, onBlur, onChange, ...textFieldProps } = props; return ( ( { controllerOnChange(...args); onChange?.(...args); }} onBlur={(...args) => { controllerOnBlur(); onBlur?.(...args); }} inputRef={(element) => { inputRef.current = element; ref(element); }} InputProps={{ readOnly, endAdornment: clickToCopy ? createCopyAdornment(inputRef) : null, }} /> )} /> ); }