React components for creating data-driven forms.
npm install @stackup/inputsA set of helpful input components that take the burden out of building forms with React.
This library depends on @stackup/form to manage the state
of form values.
- Components
- Checkbox
- CheckboxItem
- FieldSet
- FileInput
- FileListInput
- Input
- Radio
- Select
- Switch
- TextArea
- ThemeProvider
- Utilities
- concat
- createTheme
- formatDateTime
- formatTime
- formatNumber
- parseDateTime
- parseTime
- parseNumber
Manages the state of a boolean value using a checkbox.
In addition to the props listed below, this component accepts
all props for an HTML input.
#### Example
``jsx`
help="Did you read all 7,000 lines?"
field={useField(form, "confirmation")}
/>
#### Props
| Name | Type | Required | Description |
| -------------- | ------------------------------------- | -------- | ------------------------------------------------------ |
| field | FormField | ✓ | See @stackup/form |ReactNode
| label | | ✓ | Content to appear in the label |ReactNode
| help | | ✗ | Extra help info that will be rendered within the label |string \| boolean \| FieldVariant[]
| variant | | ✗ | Variant class name to append to all elements |ReactNode
| prepend | | ✗ | Content to render before the input |ReactNode
| append | | ✗ | Content to render after the input |string
| className | | ✗ | An additional class name for the field |string
| inputClassName | | ✗ | An additional class name for the input |string
| labelClassName | | ✗ | An additional class name for the label |string
| helpClassName | | ✗ | An additional class name for the help |string
| errorClassName | | ✗ | An additional class name for the error |Ref
| innerRef | | ✗ | A ref to the input element |
Manages an array of values, represented by checkboxes.
In addition to the props listed below, this component accepts
all props for an HTML input.
Note that this component will not render error messages. For that, you'll
want to wrap your checkboxes in a FieldSet.
#### Example
`jsx`
#### Props
| Name | Type | Required | Description |
| -------------- | ------------------------------------- | -------- | ------------------------------------------------------ |
| value | Value | ✓ | Toggle the inclusion of this value in the array. |FormField
| field | | ✓ | See @stackup/form |ReactNode
| label | | ✓ | Content to appear in the label |ReactNode
| help | | ✗ | Extra help info that will be rendered within the label |string \| boolean \| FieldVariant[]
| variant | | ✗ | Variant class name to append to all elements |ReactNode
| prepend | | ✗ | Content to render before the input |ReactNode
| append | | ✗ | Content to render after the input |string
| className | | ✗ | An additional class name for the field |string
| inputClassName | | ✗ | An additional class name for the input |string
| labelClassName | | ✗ | An additional class name for the label |string
| helpClassName | | ✗ | An additional class name for the help |string
| errorClassName | | ✗ | An additional class name for the error |Ref
| innerRef | | ✗ | A ref to the input element |
Fields that are composed of multiple components (e.g. Radio and CheckboxItem)
should always be rendered within a fieldset.
If an error is associated with the field, it will be rendered within the
fieldset, rather than on a Radio or CheckboxItem.
#### Example
`jsx`
#### Props
| Name | Type | Required | Description |
| --------------- | ------------------------------------- | -------- | -------------------------------------------------- |
| field | FormField | ✓ | See @stackup/form |ReactNode
| legend | | ✓ | Content to appear in the legend |ReactNode
| help | | ✗ | Extra help info |ReactNode
| children | | ✗ | Content that should appear inside the fieldset |string \| boolean \| FieldVariant[]
| variant | | ✗ | Variant class name to append to all elements |string
| className | | ✗ | An additional class name for the fieldset |string
| legendClassName | | ✗ | An additional class name for the legend |string
| helpClassName | | ✗ | An additional class name for the help |string
| errorClassName | | ✗ | An additional class name for the error |
Renders an .
In addition to the props listed below, this component accepts
all props for an HTML input.
#### Example
`jsx`
#### Props
| Name | Type | Required | Description |
| -------------- | ------------------------------------- | -------- | ------------------------------------------------------ |
| field | FormField | ✓ | See @stackup/form |ReactNode
| label | | ✓ | Content to appear in the label |ReactNode
| help | | ✗ | Extra help info that will be rendered within the label |string \| boolean \| FieldVariant[]
| variant | | ✗ | Variant class name to append to all elements |ReactNode
| prepend | | ✗ | Content to render before the input |ReactNode
| append | | ✗ | Content to render after the input |string
| className | | ✗ | An additional class name for the field |string
| inputClassName | | ✗ | An additional class name for the input |string
| labelClassName | | ✗ | An additional class name for the label |string
| helpClassName | | ✗ | An additional class name for the help |string
| errorClassName | | ✗ | An additional class name for the error |Ref
| innerRef | | ✗ | A ref to the input element |
Renders an .
In addition to the props listed below, this component accepts
all props for an HTML input.
#### Example
`jsx`
#### Props
| Name | Type | Required | Description |
| -------------- | ------------------------------------- | -------- | ------------------------------------------------------ |
| field | FormField | ✓ | See @stackup/form |ReactNode
| label | | ✓ | Content to appear in the label |ReactNode
| help | | ✗ | Extra help info that will be rendered within the label |string \| boolean \| FieldVariant[]
| variant | | ✗ | Variant class name to append to all elements |ReactNode
| prepend | | ✗ | Content to render before the input |ReactNode
| append | | ✗ | Content to render after the input |string
| className | | ✗ | An additional class name for the field |string
| inputClassName | | ✗ | An additional class name for the input |string
| labelClassName | | ✗ | An additional class name for the label |string
| helpClassName | | ✗ | An additional class name for the help |string
| errorClassName | | ✗ | An additional class name for the error |Ref
| innerRef | | ✗ | A ref to the input element |
Renders an , which whatever type you provide.
The value of this input will always be a string, but you should use a
validation library such as @stackup/validate
to parse and validate the entry before submission.
In addition to the props listed below, this component accepts
all props for an HTML input.
#### Example
`jsx`
#### Props
| Name | Type | Required | Description |
| -------------- | ------------------------------------- | -------- | ------------------------------------------------------ |
| type | InputType | ✗ | |FormField
| field | | ✓ | See @stackup/form |ReactNode
| label | | ✓ | Content to appear in the label |ReactNode
| help | | ✗ | Extra help info that will be rendered within the label |string \| boolean \| FieldVariant[]
| variant | | ✗ | Variant class name to append to all elements |ReactNode
| prepend | | ✗ | Content to render before the input |ReactNode
| append | | ✗ | Content to render after the input |string
| className | | ✗ | An additional class name for the field |string
| inputClassName | | ✗ | An additional class name for the input |string
| labelClassName | | ✗ | An additional class name for the label |string
| helpClassName | | ✗ | An additional class name for the help |string
| errorClassName | | ✗ | An additional class name for the error |Ref
| innerRef | | ✗ | A ref to the input element |
Renders an and manages the state of any type of value.
Note that this component will not render error messages. For that, you'll
want to wrap your radio options in a FieldSet.
In addition to the props listed below, this component accepts
all props for an HTML input.
#### Example
`jsx`
#### Props
| Name | Type | Required | Description |
| -------------- | ------------------------------------- | -------- | ------------------------------------------------------ |
| value | Value | ✓ | The value of the option to be selected |FormField
| field | | ✓ | See @stackup/form |ReactNode
| label | | ✓ | Content to appear in the label |ReactNode
| help | | ✗ | Extra help info that will be rendered within the label |string \| boolean \| FieldVariant[]
| variant | | ✗ | Variant class name to append to all elements |ReactNode
| prepend | | ✗ | Content to render before the input |ReactNode
| append | | ✗ | Content to render after the input |string
| className | | ✗ | An additional class name for the field |string
| inputClassName | | ✗ | An additional class name for the input |string
| labelClassName | | ✗ | An additional class name for the label |string
| helpClassName | | ✗ | An additional class name for the help |string
| errorClassName | | ✗ | An additional class name for the error |Ref
| innerRef | | ✗ | A ref to the input element |
Renders a tag with whatever options you provide.
You can provide a placeholder, which will render a disabled option
with a blank value.
In addition to the props listed below, this component accepts
all props for an HTML select.
#### Example
`jsx`
label="Sport"
placeholder="Choose a sport"
field={useField(form, "sport")}
>
#### Props
| Name | Type | Required | Description |
| -------------- | ------------------------------------- | -------- | ------------------------------------------------------ |
| placeholder | string | ✗ | |FormField
| field | | ✓ | See @stackup/form |ReactNode
| label | | ✓ | Content to appear in the label |ReactNode
| help | | ✗ | Extra help info that will be rendered within the label |string \| boolean \| FieldVariant[]
| variant | | ✗ | Variant class name to append to all elements |ReactNode
| prepend | | ✗ | Content to render before the input |ReactNode
| append | | ✗ | Content to render after the input |string
| className | | ✗ | An additional class name for the field |string
| inputClassName | | ✗ | An additional class name for the input |string
| labelClassName | | ✗ | An additional class name for the label |string
| helpClassName | | ✗ | An additional class name for the help |string
| errorClassName | | ✗ | An additional class name for the error |Ref
| innerRef | | ✗ | A ref to the input element |
Renders a to toggle the state of a boolean value.
You can style this element with CSS by selecting the [aria-checked]
value.
In addition to the props listed below, this component accepts
all props for an HTML button.
#### Example
`jsx`
#### Props
| Name | Type | Required | Description |
| -------------- | ------------------------------------- | -------- | ------------------------------------------------------ |
| field | FormField | ✓ | See @stackup/form |ReactNode
| label | | ✓ | Content to appear in the label |ReactNode
| help | | ✗ | Extra help info that will be rendered within the label |string \| boolean \| FieldVariant[]
| variant | | ✗ | Variant class name to append to all elements |ReactNode
| prepend | | ✗ | Content to render before the input |ReactNode
| append | | ✗ | Content to render after the input |string
| className | | ✗ | An additional class name for the field |string
| inputClassName | | ✗ | An additional class name for the input |string
| labelClassName | | ✗ | An additional class name for the label |string
| helpClassName | | ✗ | An additional class name for the help |string
| errorClassName | | ✗ | An additional class name for the error |Ref
| innerRef | | ✗ | A ref to the input element |
Renders a .
In addition to the props listed below, this component accepts
all props for an HTML input.
#### Example
`jsx`
#### Props
| Name | Type | Required | Description |
| -------------- | ------------------------------------- | -------- | ------------------------------------------------------ |
| field | FormField | ✓ | See @stackup/form |ReactNode
| label | | ✓ | Content to appear in the label |ReactNode
| help | | ✗ | Extra help info that will be rendered within the label |string \| boolean \| FieldVariant[]
| variant | | ✗ | Variant class name to append to all elements |ReactNode
| prepend | | ✗ | Content to render before the input |ReactNode
| append | | ✗ | Content to render after the input |string
| className | | ✗ | An additional class name for the field |string
| inputClassName | | ✗ | An additional class name for the input |string
| labelClassName | | ✗ | An additional class name for the label |string
| helpClassName | | ✗ | An additional class name for the help |string
| errorClassName | | ✗ | An additional class name for the error |Ref
| innerRef | | ✗ | A ref to the input element |
Render a ThemeProvider at the top of your component tree
to provide a global theme.
See createTheme for an example of how to define your theme.
#### Example
`jsx`
Concatenate multiple class names.
`javascript`
concat("field", touched && error && "field--invalid");
Create a new theme. This can be used in combination with the
ThemeProvider to apply class names to fields.
`javascriptfield--${type}
export const theme = createTheme({
field: props => {
return concat(
"field",
props.touched && props.error && "field--invalid",
...props.types.map(type => ),field--${variant}
...props.variants.map(variant => variant && )`
);
}
});
Convert ISO-8601 to input[type=datetime-local]. This can be used to populate
the initial value for your inputs.
When given an invalid value, this function will throw an error.
`javascript`
formatDateTime("2020-07-08T16:30:00.000Z");
Convert ISO-8601 to input[type=time]. This can be used to populate
the initial value for your inputs.
When given an invalid value, this function will throw an error.
`javascript`
formatTime("08:28:00Z");
Convert a number to input[type=number]. This can be used to populate
the initial value for your inputs.
When given an invalid value, this function will throw an error.
`javascript`
formatNumber(0);
Convert input[type=datetime-local] to ISO-8601. This can be used to parse and validate
information entered by the user.
When given an invalid value, this function will return undefined.
`javascript`
parseDateTime("2020-07-08T12:30");
Convert input[type=time] to ISO-8601. This can be used to parse and validate
information entered by the user.
When given an invalid value, this function will return undefined.
`javascript`
parseTime("04:28");
Convert input[type=number] to a number. This can be used to parse and validate
information entered by the user.
When given an invalid value, this function will return undefined.
`javascript``
parseNumber("1");