mirror of https://github.com/ctk-hq/ctk
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
54 lines
1.3 KiB
TypeScript
54 lines
1.3 KiB
TypeScript
import { FunctionComponent, ReactElement } from "react";
|
|
import { styled } from "@mui/joy";
|
|
import lodash from "lodash";
|
|
import { useFormikContext } from "formik";
|
|
|
|
export interface ITextFieldProps {
|
|
name: string;
|
|
help?: string;
|
|
[key: string]: any;
|
|
}
|
|
|
|
const Root = styled("div")``;
|
|
|
|
const TextField: FunctionComponent<ITextFieldProps> = (
|
|
props: ITextFieldProps
|
|
): ReactElement => {
|
|
const { label, name, help, required, ...otherProps } = props;
|
|
const formik = useFormikContext();
|
|
const error =
|
|
lodash.get(formik.touched, name) && lodash.get(formik.errors, name);
|
|
|
|
return (
|
|
<Root>
|
|
<div>
|
|
{label && (
|
|
<label htmlFor={name} className="lbl-util">
|
|
{label + (required ? "*" : "")}
|
|
</label>
|
|
)}
|
|
|
|
<input
|
|
id={name}
|
|
name={name}
|
|
type="text"
|
|
autoComplete="off"
|
|
className="input-util mb-2 sm:mb-0"
|
|
required={required}
|
|
onBlur={formik.handleBlur}
|
|
onChange={formik.handleChange}
|
|
value={lodash.get(formik.values, name)}
|
|
{...otherProps}
|
|
/>
|
|
|
|
<div className="text-xs text-red-600 mt-1">
|
|
{error && <span className="caption">{error}</span>}
|
|
{!error && help}
|
|
</div>
|
|
</div>
|
|
</Root>
|
|
);
|
|
};
|
|
|
|
export default TextField;
|