FormInput component

The FormInput component is a reusable input field designed for integration with useFormik composable. It handles field value updates, validation errors, and accessibility attributes automatically. With support for customization through props, it ensures consistent styling and behavior across forms while simplifying form logic.

Props:

NameTypeRequiredDescription
formikobjectfalseThe Formik instance manages form state, validation, and field values. Using Vue’s provide/inject, the Formik instance can be passed to child components. If no props are provided, the component searches for a parent Formik instance and throws an error if none is found.
namestringtrueThe name of the form field, used to identify the field in the Formik instance.
labelstringfalseAn optional label for the input field. If provided, it is displayed as a field label.
placeholderstringfalseAn optional placeholder for the input field. It is displayed inside the input when no value is present.
typestringfalseThe type of the input field (e.g., 'text', 'password', 'email'). Defaults to 'text' if not provided.
readonlybooleanfalseDetermines if the input field is read-only. Defaults to false.
disabledbooleanfalseDetermines if the input field is disabled. Defaults to false.
requiredbooleanfalseDetermines if the input field is required. Defaults to false.
readonlybooleanfalseDetermines if the input field is read-only. Defaults to false.
inputPropsobjectfalseAdditional props to pass to the input field, such as attributes like 'min', 'max', 'step', etc.

Slots

NameDescription
defaultSlot for rendering additional content or components inside the input container, below the input field.
prependSlot for rendering content or components before the input field, such as an icon or label.
appendSlot for rendering content or components after the input field, such as an action button or icon.
FormInput Sketch

Usage:

<FormInput
  :formik="formik"
  name="name"
  label="Name:"
  type="text"
  placeholder="Enter your name"
/>

If your field has array of values then you can use the name like:

<FormInput
  :formik="formik"
  :name="`contacts[${index}].code`"
  type="tel"
  :placeholder="`Enter contact number ${index + 1}`"
/>

Class Naming Style

  1. Base Class: The base class for this component is vf-field. It serves as the primary identifier for the component and provides the foundational styles.
  2. Dynamic Type-Specific Classes: The component applies a type-specific class dynamically using the type prop.
    For specific types (e.g., password, email), the classes will be vf-password-field or vf-email-field, respectively.
  3. Modifier Classes: These classes modify the appearance or behavior of the base component:
    • vf-field--error: Applied when the field has a validation error, determined by formik.hasFieldError(name).
    • vf-field--disabled: Applied when the field is in a disabled state, determined by the disabled prop.
    • vf-field--readonly: Applied when the field is set to read-only, determined by the readonly prop.
  4. Input Element Classes: Classes applied specifically to the input element within the component:
    • vf-input--error: Indicates an error in the input field, applied when formik.hasFieldError(name) is true.
    • vf-input--disabled: Marks the input as disabled, determined by the disabled prop.
    • vf-input--readonly: Marks the input as read-only, determined by the readonly prop.
  5. Error Message Class:
    • vf-error: Used for displaying error messages below the input field. The error message is rendered when formik.hasFieldError(name) is true.
  6. Dynamic Behavior: Classes and attributes are applied conditionally based on the component's props and state:
    • The vf-field--error and vf-input--error classes are applied when formik.hasFieldError(name) is true.
    • The vf-field--disabled and vf-input--disabled classes are applied when the disabled prop is true.
    • The vf-field--readonly and vf-input--readonly classes are applied when the readonly prop is true.
    • ARIA attributes like aria-labelledby, aria-describedby, aria-invalid, aria-required, aria-readonly, and aria-disabled are applied dynamically to enhance accessibility.