Components
Input
Input is a form element used to collect user data. It offers basic functionality and is ideal when you need full control over the component's layout and validation, making it suitable for building custom elements.
<input class="ds-input" type="text" aria-label="input"/>const Preview = () => { return <Input aria-label='input' />; }; render(<Preview />)
Use input when
- you need a basic input field without additional functionality
- you are building custom fields or composite components
- you want to control your own logic for error handling and description
Avoid input when
- you need a complete form field with label, description and validation message, use textfield instead
- the field requires additional logic already provided by higher-level components such as textfield
Example
This component is used as a building block for other components, such as textfield. For that reason, this component will not have many examples or detailed guidance.
With label
<ds-field class="ds-field">
<label class="ds-label">Fødselsnummer</label>
<input class="ds-input" type="text"/>
</ds-field>const WithLabel = () => { return ( <Field> <Label>Fødselsnummer</Label> <Input /> </Field> ); }; render(<WithLabel />)
With error
<ds-field class="ds-field">
<label class="ds-label">Fødselsnummer</label>
<input class="ds-input" type="text"/>
<p class="ds-validation-message" data-field="validation">Vennligst fyll ut fødselsnummer</p>
</ds-field>const WithError = () => { return ( <Field> <Label>Fødselsnummer</Label> <Input /> <ValidationMessage>Vennligst fyll ut fødselsnummer</ValidationMessage> </Field> ); }; render(<WithError />)
Disabled
Avoid using disabled where possible. Consider using readOnly instead.
<ds-field class="ds-field">
<label class="ds-label">Fødselsnummer</label>
<input
class="ds-input"
type="text"
disabled=""
value="12345678901"
/>
</ds-field>const Disabled = () => { return ( <Field> <Label>Fødselsnummer</Label> <Input disabled value='12345678901' /> </Field> ); }; render(<Disabled />)
ReadOnly
<ds-field class="ds-field">
<label class="ds-label">Fødselsnummer</label>
<input
class="ds-input"
type="text"
readOnly=""
value="12345678901"
/>
</ds-field>const ReadOnly = () => { return ( <Field> <Label>Fødselsnummer</Label> <Input readOnly value='12345678901' /> </Field> ); }; render(<ReadOnly />)
Guidelines
See the guidelines for textfield.
Placement of the label
The label and any description should always be placed above the text field. This makes them easier to see on small screens and prevents them from being hidden by validation messages.
In special cases, the label may be visually hidden by not using label. This may be appropriate in tables, for example, when the field receives its label from the table header. Even when the label is visually hidden, it must still be written in a meaningful way, as it will be announced by screen readers. This can be done using aria-label or aria-labelledby.