Import
import { Input } from '@dnb/eufemia'
Description
The input component is an umbrella component for all inputs which share the same style as the classic text
input field.
Formatted input fields (masked values)
You may consider to use InputMasked for formatted strings and Eufemia Forms fields like Number and Currency for formatted numbers:
Accessibility
Please avoid using the maxlength
attribute when possible, as it may lower good accessibility. You can instead, use the TextCounter component.
But you may also consider to use a multiline input with a characterCounter
:
40 av 40 tegn gjenstår.
<Field.String label="Label text" placeholder="Enter your text" multiline rows={1} characterCounter={40} />
Demos
Placeholder text
<Input label="Label" placeholder="Placeholder text" />
Search text placeholder
<Input label="Search" type="search" placeholder="Search text placeholder" on_change={({ value }) => { console.log('on_change', value) }} on_submit={({ value }) => { console.log('Submit:', value) }} />
Medium and stretched search input
<Input size="medium" type="search" stretch={true} value="Medium search value" on_change={({ value }) => { console.log('on_change', value) }} />
Input with icon
With left / right aligned text
<Input label="Input with icon" placeholder="Input" label_direction="vertical" icon="check" bottom /> <Input label="Input with icon" label_sr_only placeholder="Input with a placeholder" icon_position="right" icon="check" align="right" />
Disabled input
<Input disabled label="Disabled input" placeholder="Disabled Input with a placeholder" />
With FormStatus
<Provider formElement={{ label_direction: 'vertical', }} > <Flex.Vertical> <div> <Input label="With FormStatus" status="You have to fill in this field" value="Input value with error" /> </div> <div> <Input label="With button" status="You have to fill in this field" value="Input value with error" type="search" /> </div> </Flex.Vertical> </Provider>
Input with suffix and custom label component
<Input label={<Span className="dnb-p--lead">Fødselsnummer</Span>} label_direction="vertical" autocomplete="on" placeholder="Placeholder text" suffix={ <HelpButton title="Info" size="large"> Some content </HelpButton> } on_change={({ value }) => { console.log('on_change', value) }} />
Input
in horizontal flex and a long label
Stretched <Provider formElement={{ label_direction: 'vertical', }} > <FieldBlock label="Long label labwl Adipiscing mauris dis proin nec" forId="input-id" > <Input id="input-id" value="I stretch ..." stretch status="Status message" status_state="warn" /> </FieldBlock> </Provider>
Numbers are using DNB Mono (monospace)
Also, this example manipulates the value during typing.
<Input label="Label" autocomplete="on" placeholder="Placeholder text" status="Numbers are using DNB Mono (monospace)" status_state="info" value="1234567890" on_change={({ value }) => { console.log('on_change', value) return String(value).toUpperCase() }} />
Submit Form with Input
Pressing the enter key will trigger a submit.
<Form.Handler onSubmit={(event) => { console.log(event) }} > <FormLabel for_id="search">Label</FormLabel> <Flex.Horizontal align="baseline"> <Input id="search" type="search" value="Input ..." selectall={true} on_submit={(event) => { console.log('Input.on_submit', event) }} on_change={({ value }) => { console.log('on_change:', value) }} /> <Form.SubmitButton /> </Flex.Horizontal> </Form.Handler>
Input with clear button
Pushing the clear button will clear the input.
<Flex.Vertical> <Input clear={true} value="Value ..." size="medium" /> <Input clear={true} value="Value ..." type="search" /> <Input clear={true} value="Value ..." icon="loupe" type="search" /> </Flex.Vertical>
Input password type
The password component has to ensure that there is still room for password managers to inject the input with their UX functionality.
In order to get the show/hide button, you may have to import the component like so:
import InputPassword from '@dnb/eufemia/components/input/InputPassword'
<InputPassword label="Label" placeholder="A placeholder text" on_change={({ value }) => { console.log('on_change:', value) }} on_show_password={() => { console.log('on_show_password') }} on_hide_password={() => { console.log('on_hide_password') }} />