FormSelectField component

The FormSelectField component is a reusable select 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.
optionsArray<{ label: string; value: string | number }>falseAn array of objects representing the selectable options. Each object should have a 'label' and 'value' property.
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 data attributes, 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.
FormSelect Sketch

Usage:

<FormSelectField
  :formik="formik"
  name="name"
  label="Name:"
  :options="[
    { label: 'Option 1', value: 'option1' },
    { label: 'Option 2', value: 'option2' },
    { label: 'Option 3', value: 'option3' },
  ]"
  placeholder="Select an option"
/>

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

<FormSelectField
  :formik="formik"
  :name="`contacts[${index}].code`"
  :options="[
    { label: 'Option 1', value: 'option1' },
    { label: 'Option 2', value: 'option2' },
    { label: 'Option 3', value: 'option3' },
  ]"
  placeholder="Select an option"
/>

Class Naming Style

  1. Base Classes: The foundational classes for this component are:
    • vf-field: The primary class for styling the field container.
    • vf-select-field: A base-specific class indicating the field is a select input.
  2. Input Wrapper Class: The vf-input class is applied to the wrapper element surrounding the select input. It serves as the container for the select element and optional prepend/append slots.
  3. Dynamic Classes: Classes applied conditionally based on the component's state and props:
    • vf-field--error: Applied when the field has a validation error, determined by formik.hasFieldError(name).
    • vf-input--error: Applied to the select element when formik.hasFieldError(name) is true, indicating an error in the field.
    • vf-input--disabled: Applied to the select element when the disabled prop is true, marking the field as disabled.