useField
Usage
import { forwardRef } from "react";
import { TextField, TextFieldProps } from "@mui/material";
import { useField } from "@olenbetong/appframe-react";
export type BoundTextFieldProps = TextFieldProps & {
field: string;
};
export const BoundTextField = forwardRef<HTMLInputElement, BoundTextFieldProps>(
function BoundTextField({ field, ...props }, ref) {
let { error, value, onChange, onKeyDown } = useField<any, any>(field);
if (props.type === "date" && value instanceof Date) {
value = value.toISOString().split("T")[0];
} else if (
["datetime", "datetime-local"].includes(props.type ?? "") &&
value instanceof Date
) {
value = value.toISOString();
}
return (
<TextField
error={!!error}
fullWidth
{...props}
helperText={error ?? props.helperText}
value={value ?? ""}
onChange={onChange}
onKeyDown={onKeyDown}
inputRef={ref}
InputLabelProps={{
...props.InputLabelProps,
shrink: !!props.value ? true : props.InputLabelProps?.shrink,
}}
/>
);
}
);API
Last updated
Was this helpful?