Input components
Both the naming and the way the components work are based on being data-driven.
The word "input" in an input component here, has nothing to do with whether it places an <input>
tag or something else on the screen. It is about receiving input from the user, which will end up in the data that the components convey via callbacks.
Standardized properties
All input component has a fixed set of props that make it possible to build more complex standardized functionality around them. The most important ones here are value
and onChange
. Value expects values in the given data type, so for example DataInput.Number
expects a value
of the type number
, and will give a type error in Typescript if it e.g. receives a number in a string
. The callback function submitted to onChange
will always receive the value of the corresponding type as the first argument.
It is deliberate that onChange
sends out the value from the field, and not the event object that comes from the actual HTML tag into which the user enters data. This is to create a less tight coupling between application code that uses the components, and the internal implementation in the input components. In addition, this makes the surrounding logic simpler by not having to extract, for example, e.target.value
everywhere.
The basic components have a number of properties that make it possible to control how they function in the interface, such as multiline
on DataInput.String
, which chooses whether to get one line of text (input tag) or several lines (textarea tag) . In addition, they have a number of validation props, such as minLength
and required
.
Controlled & Uncontrolled
In React, it's important to be aware of where the states of a given set of data "lives". This can be an entire object that represents an entity the user is going to make changes to (e.g. a user or a bank account), but it also applies to the individual value a form makes changes to. A form field can be controlled or uncontrolled. The components in this package make it possible to work in both ways.
If the functionality is designed so that the state of the data will live outside the form components, you give the components a value
and an onChange
, and ensure that all changes that are sent out via onChange
are fed back via value
so that it functions as a controlled component. The internal logic in the components will then ensure that the value is kept the same via the changes it receives from the outside.
If you want the state of the value to live inside the input component, do not send the updated value in via value
. The logic will then keep the internal value with the changes continuously, and still send the latest version with all the changes the user has made, even if they are not received continuously via value
, as a basic <input>
tag in React expects.
Creating custom input components
The useInput
hook that is used in all existing input components is exported to make it possible to create custom input components that have the same properties and follow the same flow as the standard components, without the need to recreate all the basic state handling features.
Basic input components
Data input consists of basic components that are designed to receive user input that will end up in the form of primitive data types, such as string
, number
and boolean
:
String
DataInput.String is the base component for receiving user input where the target data is of type `string`.
Number
DataInput.Number is the base component for receiving user input where the target data is of type number.
Boolean
DataInput.Boolean is the base component for receiving user input where the target data is of type `boolean`.
Select
DataInput.Select is a wrapper component for selecting between options using a dropdown or similar user experiences.
Option
Feature-specific input components
In addition to the basic input components, the package offers a number of input components that are more closely tailored to the business logic we need in our applications.
Although these mostly reuse the basic components internally, they prevent you from having to provide a number of boilerplate props such as syntax rules, field labels and validation error messages. All props can still be overridden individually if necessary.
The following input components exist:
Currency
DataInput.Currency is a wrapper component for the input of numbers, with user experience tailored for currency values.
Date
DataInput.Date is a wrapper component for the input of strings, with user experience tailored for date values.
DataInput.Email is a wrapper component for the input of strings, with user experience tailored for email values.
FirstName
DataInput.FirstName is a wrapper component for the input of strings, with user experience tailored for first name values.
LastName
DataInput.LastName is a wrapper component for the input of strings, with user experience tailored for last name values.
NationalIdentityNumber
DataInput.NationalIdentityNumber is a wrapper component for the input of strings, with user experience tailored for national identity number values.
PhoneNumber
DataInput.PhoneNumber is a wrapper component for the input of strings, with user experience tailored for phone number values.