import { Field, FieldProps, FieldAttributes, FormikProps } from "formik"; import React, { LabelHTMLAttributes } from "react"; import styles from "./Input.module.css"; import classnames from "classnames"; type CustomInputProps = { customRender?: (fieldProps: FieldProps) => React.ReactNode; customOnChange?: ( event: React.ChangeEvent, form: FormikProps ) => void; }; export function Label({ children, ...props }: LabelHTMLAttributes) { return ( ); } export function Hint({ children, ...props }: LabelHTMLAttributes) { return ( ); } export default function Input( props: CustomInputProps & FieldAttributes<{ hint?: string; label?: string; disabled?: boolean }> ) { const generatedId = React.useId(); return (
{props.label ? : null} {(fieldProps: FieldProps) => { let { field, meta, form } = fieldProps; let textAfterField = meta.touched && meta.error ? (

{meta.error}

) : ( props.hint &&

{props.hint}

); // in React is always uncontrolled, so we have to hardcode // the value to "" if it's a file picker const inputFields = props.type === "file" ? (() => { let clonedField = Object.assign({}, field); delete clonedField.value; return clonedField; })() : field; return ( <> {props.customRender == null ? ( { console.log(event); if (props.customOnChange) { console.log("using custom on change"); props.customOnChange(event, form); } else { form.setFieldValue(field.name, event.currentTarget.value); } }} /> ) : ( props.customRender(fieldProps) )} {textAfterField} ); }}
); }