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
React
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 />)
- 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
| Name | Type | Default | Description |
|---|---|---|---|
| position | "start" | "end" | start | Position of toggle inputs (radio, checkbox, switch) in field |
| asChild | boolean | false | 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
| Name | Type | Default | Description |
|---|---|---|---|
| over | string | '%d tegn for mye' | Label template for when `maxCount` is exceeded. Use `%d` to insert the number of characters. |
| under | string | '%d tegn igjen' | Label template for count. Use `%d` to insert the number of characters. |
| hint | string | - | @deprecated The hint attribute is deprecated. |
| limit | number | undefined | The maximum allowed characters. |
| asChild | boolean | false | 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.
React
Unable to parse html
const AffixEn = () => ( <Field> <Label>How many pounds does it cost per month?</Label> <Field.Affixes> <Field.Affix>GBP</Field.Affix> <Input /> <Field.Affix>per month</Field.Affix> </Field.Affixes> </Field> ); render(<AffixEn />)
Number of characters
Use Field.Counter to inform about the number of characters users can type in the field.
React
Unable to parse html
const CounterEn = () => ( <Field> <Label>Add a description</Label> <Textarea rows={2} /> <Field.Counter limit={10} /> </Field> ); render(<CounterEn />)
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.
React
Unable to parse html
const PositionEn = () => ( <> <Field position='end'> <Label>Airplane mode</Label> <Input type='checkbox' role='switch' /> </Field> </> ); render(<PositionEn />)
HTML
Add the class name ds-field to the <ds-field> element.
<ds-field> links together label, input and validation message.
Number of characters
To get a counter on input and textarea, add a <p> with the attribute data-field="counter" inside the ds-field.
You can control the text in the counter using the following attributes:
| attribute | type | default | required |
|---|---|---|---|
| data-limit | number | undefined | true |
| data-over | string | %d tegn for mye | false |
| data-under | string | %d tegn igjen | false |
Indeterminate
To make a checkbox indeterminate, add data-indeterminate="true" on the <input> element.
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
| Name | Value |
|---|---|
| --dsc-field-content-spacing | var(--ds-size-2) |
| --dsc-field-counter-over | "%d tegn for mye" |
| --dsc-field-counter-under | "%d tegn igjen" |
| --dsc-field-affix-border-width | var(--ds-border-width-default) |
| --dsc-field-affix-border-style | solid |
| --dsc-field-affix-border-color | var(--ds-color-neutral-border-default) |
| --dsc-field-affix-padding-inline | var(--ds-size-4) |
| Name | Value |
|---|---|
| data-field | description, counter |
| data-state | over |
| data-clickdelegatefor | |
| data-position | end |