Skip to main content

What are you looking for?

Try searching for…

Components

Field

Field is a helper component to automatically associate a field with label, description, validation message and character count.

Press Enter to start editing
Unable to parse html
const PreviewEn = () => {
  return (
    <Field>
      <Label>Last name</Label>
      <Field.Description>Last name cannot contain spaces</Field.Description>
      <Input defaultValue='Smith Washington' />
      <ValidationMessage>
        You cannot have spaces in your last name
      </ValidationMessage>
    </Field>
  );
};

render(<PreviewEn />)

Usage

Field groups a label, an optional description, a form element (such as input, textarea, or select), and a validation message.

Add the class name ds-field to the <ds-field> element. <ds-field> automatically adds accessibility attributes and support for optional character count. <ds-field> also links the validation message across fields if it is placed inside a fieldset.

Field or fieldset?

Field is used for individual form elements, while fieldset is used to group multiple fields together. A good rule of thumb is that a fieldset is a set of fields.

Note: The React component textfield already has field built-in and should therefore not be placed inside a field.

Examples

Prefix/Suffix

Prefixes and suffixes are useful for showing units, currency or other types of information relevant to the field. You should not use these on their own, as screen readers do not announce them.

Character count

Add a <p> with the attribute data-field="counter" inside the ds-field to get a counter on input and textarea.

You can control the text in the counter using the following attributes:

attributetypedefaultrequired
data-limitnumberundefinedtrue
data-overstring%d tegn for myefalse
data-understring%d tegn igjenfalse

Placement

Use data-position="end" on <ds-field> when you want to place, for example, a switch to the right of the label. The default is data-position="start".

Indeterminate

To make a checkbox indeterminate, add data-indeterminate="true" on the <input> element.

CSS variables and data attributes

Sizes are controlled with data-size and colors with data-color. The component will inherit from the closest parent that has these set.

CSS Variables
NameValue
--dsc-field-affix-backgroundvar(--ds-color-neutral-surface-tinted)
--dsc-field-affix-border-colorvar(--ds-color-neutral-border-default)
--dsc-field-affix-border-stylesolid
--dsc-field-affix-border-widthvar(--ds-border-width-default)
--dsc-field-affix-colorvar(--ds-color-neutral-text-subtle)
--dsc-field-affix-padding-inlinevar(--ds-size-4)
--dsc-field-border-colorvar(--ds-color-neutral-border-default)
--dsc-field-border-color--checkedvar(--ds-color-border-default)
--dsc-field-border-color--invalidvar(--ds-color-danger-border-default)
--dsc-field-border-color--readonlyvar(--ds-color-neutral-border-subtle)
--dsc-field-border-radiusvar(--ds-border-radius-lg)
--dsc-field-border-stylesolid
--dsc-field-border-widthvar(--ds-border-width-default)
--dsc-field-border-width--activemax(2px, .125rem)
--dsc-field-content-spacingvar(--ds-size-2)
--dsc-field-counter-over"%d tegn for mye"
--dsc-field-counter-under"%d tegn igjen"
--dsc-field-description-colorinherit
--dsc-field-description-font-size--togglevar(--ds-font-size-minus-1)
--dsc-field-paddingvar(--ds-size-4)
Data Attributes
NameValue
data-clickdelegatefor
data-fielddescription, counter, validation
data-positionend
data-variantoutline

React

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. @deprecated This is not supported anymore, as the element needs to be `ds-field`

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. @deprecated This is not supported anymore, as the element needs to be `ds-field`

FieldCounter

over
Description

Label template for when `maxCount` is exceeded. Use `%d` to insert the number of characters.

Type
string
Default
'%d tegn for mye'
under
Description

Label template for count. Use `%d` to insert the number of characters.

Type
string
Default
'%d tegn igjen'
hint
Description

@deprecated The hint attribute is deprecated.

Type
string
limit
Description

The maximum allowed characters.

Type
number
Default
undefined
asChild
Description

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

Type
boolean
Default
false
NameTypeDefaultDescription
overstring'%d tegn for mye'

Label template for when `maxCount` is exceeded. Use `%d` to insert the number of characters.

understring'%d tegn igjen'

Label template for count. Use `%d` to insert the number of characters.

hintstring-

@deprecated The hint attribute is deprecated.

limitnumberundefined

The maximum allowed characters.

asChildbooleanfalse

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

Edit this page on github.com (opens in a new tab)