| 1 |
import { ChangeEvent, FocusEvent } from 'react'; |
| 2 |
import TextField, { TextFieldProps } from '@mui/material/TextField'; |
| 3 |
import { |
| 4 |
ariaDescribedByIds, |
| 5 |
BaseInputTemplateProps, |
| 6 |
examplesId, |
| 7 |
getInputProps, |
| 8 |
labelValue, |
| 9 |
FormContextType, |
| 10 |
RJSFSchema, |
| 11 |
StrictRJSFSchema, |
| 12 |
} from '@rjsf/utils'; |
| 13 |
|
| 14 |
const TYPES_THAT_SHRINK_LABEL = ['date', 'datetime-local', 'file', 'time']; |
| 15 |
|
| 16 |
/** The `BaseInputTemplate` is the template to use to render the basic `<input>` component for the `core` theme. |
| 17 |
* It is used as the template for rendering many of the <input> based widgets that differ by `type` and callbacks only. |
| 18 |
* It can be customized/overridden for other themes or individual implementations as needed. |
| 19 |
* |
| 20 |
* @param props - The `WidgetProps` for this template |
| 21 |
*/ |
| 22 |
export default function BaseInputTemplate< |
| 23 |
T = any, |
| 24 |
S extends StrictRJSFSchema = RJSFSchema, |
| 25 |
F extends FormContextType = any |
| 26 |
>(props: BaseInputTemplateProps<T, S, F>) { |
| 27 |
const { |
| 28 |
id, |
| 29 |
name, // remove this from textFieldProps |
| 30 |
placeholder, |
| 31 |
required, |
| 32 |
readonly, |
| 33 |
disabled, |
| 34 |
type, |
| 35 |
label, |
| 36 |
hideLabel, |
| 37 |
hideError, |
| 38 |
value, |
| 39 |
onChange, |
| 40 |
onChangeOverride, |
| 41 |
onBlur, |
| 42 |
onFocus, |
| 43 |
autofocus, |
| 44 |
options, |
| 45 |
schema, |
| 46 |
uiSchema, |
| 47 |
rawErrors = [], |
| 48 |
formContext, |
| 49 |
registry, |
| 50 |
InputLabelProps, |
| 51 |
...textFieldProps |
| 52 |
} = props; |
| 53 |
const inputProps = getInputProps<T, S, F>(schema, type, options); |
| 54 |
// Now we need to pull out the step, min, max into an inner `inputProps` for material-ui |
| 55 |
const { step, min, max, ...rest } = inputProps; |
| 56 |
const otherProps = { |
| 57 |
inputProps: { |
| 58 |
step, |
| 59 |
min, |
| 60 |
max, |
| 61 |
...(schema.examples ? { list: examplesId<T>(id) } : undefined), |
| 62 |
}, |
| 63 |
...rest, |
| 64 |
}; |
| 65 |
const _onChange = ({ target: { value } }: ChangeEvent<HTMLInputElement>) => |
| 66 |
onChange(value === '' ? options.emptyValue : value); |
| 67 |
const _onBlur = ({ target: { value } }: FocusEvent<HTMLInputElement>) => onBlur(id, value); |
| 68 |
const _onFocus = ({ target: { value } }: FocusEvent<HTMLInputElement>) => onFocus(id, value); |
| 69 |
const DisplayInputLabelProps = TYPES_THAT_SHRINK_LABEL.includes(type) |
| 70 |
? { |
| 71 |
...InputLabelProps, |
| 72 |
shrink: true, |
| 73 |
} |
| 74 |
: InputLabelProps; |
| 75 |
|
| 76 |
return ( |
| 77 |
<> |
| 78 |
<TextField |
| 79 |
id={id} |
| 80 |
name={id} |
| 81 |
placeholder={placeholder} |
| 82 |
label={labelValue(label || undefined, hideLabel, undefined)} |
| 83 |
autoFocus={autofocus} |
| 84 |
required={required} |
| 85 |
disabled={disabled || readonly} |
| 86 |
{...otherProps} |
| 87 |
value={value || value === 0 ? value : ''} |
| 88 |
error={rawErrors.length > 0} |
| 89 |
onChange={onChangeOverride || _onChange} |
| 90 |
onBlur={_onBlur} |
| 91 |
onFocus={_onFocus} |
| 92 |
InputLabelProps={DisplayInputLabelProps} |
| 93 |
{...(textFieldProps as TextFieldProps)} |
| 94 |
aria-describedby={ariaDescribedByIds<T>(id, !!schema.examples)} |
| 95 |
/> |
| 96 |
{Array.isArray(schema.examples) && ( |
| 97 |
<datalist id={examplesId<T>(id)}> |
| 98 |
{(schema.examples as string[]) |
| 99 |
.concat(schema.default && !schema.examples.includes(schema.default) ? ([schema.default] as string[]) : []) |
| 100 |
.map((example: any) => { |
| 101 |
return <option key={example} value={example} />; |
| 102 |
})} |
| 103 |
</datalist> |
| 104 |
)} |
| 105 |
</> |
| 106 |
); |
| 107 |
} |