Skip to main content

Components

Field

Field is a helper component to automatically associate a field with Label, Field.Description, ValidationMessage and Field.Counter.

It is common to get confused between Fieldset and Field. A good rule of thumb is that Fieldset is a set of Field

position
Description

Position of toggle inputs (radio, checkbox, switch) in field

Type
"start" | "end"
Default
start
asChild
Description

Change the default rendered element for the one passed as a child, merging their props and behavior.

Type
boolean
Default
false
NameTypeDefaultDescription
position"start" | "end"start

Position of toggle inputs (radio, checkbox, switch) in field

asChildbooleanfalse

Change the default rendered element for the one passed as a child, merging their props and behavior.

Usage

Field can be used with Input, Textarea and Select. NOTE: Textfield already has Field built in, and therefore cannot be used in a Field.

Examples

Prefix/Suffix

Prefixes and suffixes are useful for displaying units, currency or other types of information relevant to the field. You should not use these on their own, as screen readers do not read them out. It is important that the same information displayed in the prefix or suffix is ​​also included in the prompt.

Number of characters

Use Field.Counter to inform about the number of characters users can type in the field.

Position

Use position="end" when you want to place, for example, a Switch to the right of Label.

You can change the text by submitting props, which you can see at the top of the page. The language is standard Norwegian Bokmål.

HTML

You can get styling from Field with the class name ds-field. On the description you must set data-field="description" to get correct styling.

Also, there is some javascript that Field runs internally, so this must be connected yourself.

The code we use is not react-specific, so you can copy our code until we have a better alternative outside of React.

fieldObserver (github.com)

Make sure the form field has aria-describedby which refers to both description and validation message.. Error should be placed first in aria-describedby so that screen readers read the error message first.

data-field="validation" is only needed if you connect fieldObserver.

Position

To change the position of elements in Field, such as a Switch, you can add data-position="end|start" to the same element as ds-field.

CSS variables and data attributes

CSS Variables
NameValue
--dsc-field-content-spacingvar(--ds-size-2)
--dsc-field-affix-border-widthvar(--ds-border-width-default)
--dsc-field-affix-border-stylesolid
--dsc-field-affix-border-colorvar(--ds-color-neutral-border-default)
--dsc-field-affix-padding-inlinevar(--ds-size-4)
Data Attributes
NameValue
data-fielddescription
data-positionend
Edit this page on github.com (opens in a new tab)