Skip to content

useField + type safety for name? #245

Closed
@PascalLuginbuehl

Description

I work at Siemens and we are also using Material-UI with Formik. For that reason, we have developed our own internal library.

We would like to contribute to your project.

Our requirements:

  • standalone components with useFields()
  • type safety for the name field via. generic

Currently this library does not make use of these requirements. Are you interested to change this?

We would provide pull requests similar to this.

(Stripped library code)

import React from "react"
import MuiTextField, { TextFieldProps as MuiTextFieldProps } from "@material-ui/core/TextField"
import { useFormikContext, useField, FieldValidator } from "formik"

export interface FormikTextFieldProps<FormValues> extends Omit<MuiTextFieldProps, "error" | "onChange" | "name" | "value"> {
    validate?: FieldValidator

    name: keyof FormValues & string
}

export default function FormikTextField<FormValues>(props: FormikTextFieldProps<FormValues>) {
    const {
        disabled,
        helperText,

        /* eslint-disable @typescript-eslint/no-unused-vars */
        // removed from otherProps, only useField
        validate,
        /* eslint-enable @typescript-eslint/no-unused-vars */

        children,

        // Same as Autocomplete renderInput(), mostly used with fullWidth
        fullWidth = true,

        ...otherProps
    } = props

    const [field, meta] = useField<string>({
        name: props.name,
        validate: validate,
    })

    const { isSubmitting } = useFormikContext()

    const fieldError = meta.error
    const showError = meta.touched && !!fieldError

    return <MuiTextField
        disabled={disabled != undefined ? disabled : isSubmitting}
        error={showError}
        helperText={showError ? fieldError : helperText}
        fullWidth={fullWidth}

        {...otherProps}
        {...field}
    >
        {children}
    </MuiTextField>
}

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions