Input
Base component to create custom inputs
Import
Polymorphic
Source
Docs
Package
Disclaimer
!important: In most cases, you should not use Input
in your application.
Input
is a base for other inputs and was not designed to be used directly.
Use Input
to create custom inputs, for other cases prefer TextInput
or other component.
Usage
Input
component is used as base for some other inputs (NativeSelect, TextInput, Textarea, etc.).
The purpose of the Input
is to provide shared styles and features to other inputs.
Left and right sections
Input
supports leftSection
and rightSection
props. These sections are rendered with absolute position inside the input wrapper. You can use them to display icons, input controls or any other elements.
You can use the following props to control sections styles and content:
rightSection
/leftSection
– React node to render on the corresponding side of inputrightSectionWidth
/leftSectionWidth
– controls width of the right section and padding on the corresponding side of the input. By default, it is controlled by componentsize
prop.rightSectionPointerEvents
/leftSectionPointerEvents
– controlspointer-events
property of the section. If you want to render a non-interactive element, set it tonone
to pass clicks through to the input.
Change input element
Input is a polymorphic component, the default root element is input
,
but it can be changed to any other element or component.
Example of using Input
as button
and select
:
Example of using react-imask with Input
:
Input.Wrapper component
Input.Wrapper
component is used in all other inputs
(TextInput, NativeSelect, Textarea, etc.)
under the hood, you do not need to wrap your inputs with it, as it is already included in all of them.
Use Input.Wrapper
only when you want to create custom inputs.
Input description
Input error
inputWrapperOrder
inputWrapperOrder
allows configuring the order of Input.Wrapper
parts.
It accepts an array of four elements: label
, input
, error
and description
.
Note that it is not required to include all of them, you can use only those that you need
– parts that are not included will not be rendered.
Description below the input
Error and description are
both below the input
inputContainer
With inputContainer
prop, you can enhance inputs that use Input.Wrapper
under the hood,
for example, you can add Tooltip to the TextInput when
the input is focused:
Tooltip will be relative to the input
required and withAsterisk props
All components that are based on Input.Wrapper
support required
and withAsterisk
props.
When set to true, both of these props will add a red asterisk to the end of the label.
The only difference is whether input element will have required
attribute, example with
TextInput component:
error prop
All inputs that use Input.Wrapper
under the hood support error
prop.
When set to true
, it will add a red border to the input. You can also pass a React node to display
an error message below the input. To only display error message without a red border, set error
prop
to React node and withErrorStyles={false}
:
Something went wrong
Something went wrong
Input.Label, Input.Description and Input.Error components
Input.Label
, Input.Error
and Input.Description
components can be used to create custom
form layouts if the default Input.Wrapper
layout does not meet your requirements.
Input description
Input error
Input.Placeholder component
Input.Placeholder
component can be used to add placeholder to Input
and InputBase
components that are based on button
element
or do not support placeholder property natively:
Default props on theme
You can add default props on theme
to Input
and Input.Wrapper
components. These default props will be inherited by all inputs
that use Input
and Input.Wrapper
under the hood (TextInput, NativeSelect, Textarea, etc.):
Description below the input
Description below the input
Styles on theme
Same as with default props, you can use Input
and Input.Wrapper
Styles API
on theme to add styles to all inputs:
Change focus styles
Use &:focus-within
selector to change inputs focus styles. You can apply these styles to
one component with classNames
prop or to all inputs with Styles API
on theme.
InputBase component
InputBase
component combines Input
and Input.Wrapper
components and supports component
prop:
Styles API
Input
and Input.Wrapper
components support Styles API –
you can customize styles of any inner element with classNames
and styles
props.
Input
Styles API selectors:
Component Styles API
Hover over selectors to highlight corresponding elements
Input.Wrapper
Styles API selectors:
Input description
Input error
Component Styles API
Hover over selectors to highlight corresponding elements
Get element ref
Accessibility
If you use Input
component without associated label element, set aria-label
:
When you use Input
with Input.Wrapper
it is required to set id
on both components
to connect label and other elements with the input:
You can use use-id to generate unique ids: