Forms
The below section will outline some standards we want all forms to utilise. Since a major part of Luna is to collect data we need to make sure our forms follow a set of standards to ensure consistency, usability and robustnes.
Forms need to be free of clutter, their main purpose is collecting data and the main priority is having a clear usable form that is easy to understand, it has relevant markers to indicate required fields and validation is triggered both inline as well as on submit.
Standardising form structure
Every form that collects data and posts it to an API should be using Formik and apply the below structure. This structure utilises the Formik and Form components that allow us to simplify the data flow in the form and help handle values and errors.
To review the structure of the initialValues and validationSchema objects as well as the desired folder structure please refer to the FormModel folder in the code sandbox included at the bottom of this page.
<Formik
initialValues={initialValues}
validationSchema={validationSchema}
onSubmit={_submitForm}
>
{({ isSubmitting }) => (
<Form id={formId}>
<Grid container>
<Grid item xs={12}>
<TextInput formField={description} fullWidth />
<Grid container justifyContent="flex-end">
<Button
color="primary"
type="submit"
disabled={isSubmitting}
data-testid="super-awesome-submit-button"
>
{isSubmitting ? <CircularProgress size={24} /> : "Save"}
</Button>
</Grid>
</Grid>
</Grid>
</Form>
)}
</Formik>Formik
Thwe Formik wrapper component accepts the forms initialValues which must be set respective to the value we expect of each field, the validationSchema for which we use the Yup library and an onSubmit handler that will automatically have access to Formiks actions and values objects upon submit.
This wrapper also enables us to use props from Formik that can help manage form state like error and loading much easier. Formik docs
The Form component from formik passes down the relevant handlers/triggers to the submit button within the form. Making it simpler and cleaner to write up the markup of the form.
Formik Enabled Inputs
In order to bring consistency and speed up form development we have defined some reusable Input fields that take standard unstyled MUI inputs like a TextField or Select and serve as a proxy component wrapping these with Formik hooks such as useField and FormikContext directly hooking the fields up to our validation schema and initial values.
☝️ The above example for the reusable SelectInput assumes the correct setup of the Formik wrapper with initialValues, ValdationSchema and the FormModel
Prefer luna-components
Prefer the @bhag-business-platform/luna-components form fields where they exist over the local ones described below, and migrate towards them as you touch a form. The FormFields below remain the fallback set for fields not yet available in luna-components.
You will find these reusable fields in luna-front-end under components/Form/FormFields. The list of available components will grow over time but for now we have
TextInputwhich can also be used as a textareaSelectInputwhich has a list of available custom props/options like helperText, AlertText etc.DateInputto be used for date only inputs and will set the received value as a luxon date objectDateAndTimeInputwhich allows the selection of time using the new MUI dateTime pickerAutocomplete/Andi/AndiAutocompleteatm only set up to load active employees, but can easily be extendedAutocomplete/Organisation/OrganisationAutocompleteallows searching for an organisation
☝️ all of these inputs accept the standard MUI props as documented in the MUI docs We will aim to add inputs for checkbox and switch and any other custom autocomplete ones so that all forms are able to utilise this approach.
👇 feel free to have a look at the sandbox below, expand the editor and see a basic example of a form using the Select and TextInput fields with initialValues, formModel and validationSchema all set up.
