Komponenter
Input
Input er et skjemaelement for å samle inn brukerdata. Det tilbyr grunnleggende funksjonalitet og er ideell når du trenger full kontroll over komponentens oppsett og validering, noe som gjør den ideell for bygging av spesialtilpassede elementer.
React
Unable to parse html
const Preview = () => { return <Input aria-label='input' />; }; render(<Preview />)
- type
- Description
Supported `input` types
- Type
"number" | "hidden" | "color" | "checkbox" | "date" | "datetime-local" | "email" | "file" | "month" | "password" | "radio" | "search" | "tel" | "text" | "time" | "url" | "week"- Default
text
- size
- Description
Defines the width of `Input` in count of characters.
- Type
number
- disabled
- Description
Disables element @note Avoid using if possible for accessibility purposes
- Type
boolean
- readOnly
- Description
Toggle `readOnly`
- Type
boolean
- role
- Description
Set role, i.e. `switch` when `checkbox` or `radio`
- Type
AriaRole
| Name | Type | Default | Description |
|---|---|---|---|
| type | "number" | "hidden" | "color" | "checkbox" | "date" | "datetime-local" | "email" | "file" | "month" | "password" | "radio" | "search" | "tel" | "text" | "time" | "url" | "week" | text | Supported `input` types |
| size | number | - | Defines the width of `Input` in count of characters. |
| disabled | boolean | - | Disables element @note Avoid using if possible for accessibility purposes |
| readOnly | boolean | - | Toggle `readOnly` |
| role | AriaRole | - | Set role, i.e. `switch` when `checkbox` or `radio` |
Bruk
Input er er styla native <input>-element, så dokumentasjonen er lite utfylt.
Du kan bruke denne komponenten for å style de fleste type-attributtene til <input>.
Du kan lese om The HTML Input element på MDN (mozilla.com).
Eksempel
Med label
React
Unable to parse html
const WithLabel = () => { return ( <Field> <Label>Fødselsnummer</Label> <Input /> </Field> ); }; render(<WithLabel />)
Med feil
React
Unable to parse html
const WithError = () => { return ( <Field> <Label>Fødselsnummer</Label> <Input aria-invalid /> </Field> ); }; render(<WithError />)
Disabled
React
Unable to parse html
const Disabled = () => { return ( <Field> <Label>Fødselsnummer</Label> <Input disabled value='12345678901' /> </Field> ); }; render(<Disabled />)
ReadOnly
React
Unable to parse html
const ReadOnly = () => { return ( <Field> <Label>Fødselsnummer</Label> <Input readOnly value='12345678901' /> </Field> ); }; render(<ReadOnly />)
type="checkbox" og type="radio" er alltid :read-only, og har ikkje støtte for dette på samme måte som input typer du kan skrive i. Dersom du setter readOnly på checkbox eller radio passe vi på at den oppfører som om den kun har lesetilgang, ved å stoppe onClick og onChange.
React
Unable to parse html
const ReadOnlyCheckAndRadio = () => { return ( <> <Fieldset> <Fieldset.Legend>Kjønn</Fieldset.Legend> <Field> <Input readOnly value='male' name='gender' type='radio' defaultChecked /> <Label>Mann</Label> </Field> <Field> <Input readOnly value='female' name='gender' type='radio' /> <Label>Kvinne</Label> </Field> <Field> <Input readOnly value='other' name='gender' type='radio' /> <Label>Annet</Label> </Field> </Fieldset> <Divider /> <Field> <Input readOnly type='checkbox' defaultChecked name='checkbox' /> <Label>Jeg samtykker</Label> </Field> </> ); }; render(<ReadOnlyCheckAndRadio />)
HTML
Legg klassenavnet på <input>-elementet for å få designet fra designsystemet.
Flere typer av input er støttet, se propstabellen på toppen for full liste.
Dersom du skal ha readonly på checkbox eller radio, må du selv stoppe onClick og onChange for å hindre endring av tilstanden.
CSS variabler og data-attributter
Klassen ds-input kan bli brukt for å style Select og Textarea også.
| Navn | Verdi |
|---|---|
| --dsc-input-padding-block | var(--ds-size-2) |
| --dsc-input-padding-inline | var(--ds-size-3) |
| --dsc-input-padding | var(--dsc-input-padding-block) var(--dsc-input-padding-inline) |
| --dsc-input-size--toggle | var(--ds-size-6) |
| --dsc-input-size | var(--ds-size-12) |
| --dsc-input-background--readonly | var(--ds-color-neutral-surface-tinted) |
| --dsc-input-background | var(--ds-color-neutral-surface-default) |
| --dsc-input-border-color--readonly | var(--ds-color-neutral-border-subtle) |
| --dsc-input-border-color | var(--ds-color-neutral-border-default) |
| --dsc-input-border-style | solid |
| --dsc-input-border-width--toggle | max(var(--ds-border-width-default),calc(var(--ds-size-1)/2)) |
| --dsc-input-border-width | var(--ds-border-width-default) |
| --dsc-input-outline-color--hover | var(--ds-color-neutral-border-default) |
| --dsc-input-outline-color--toggle--hover | var(--dsc-input-accent-color) |
| --dsc-input-outline-width--hover | var(--ds-border-width-default) |
| --dsc-input-outline-style--hover | solid |
| --dsc-input-color--readonly | var(--ds-color-neutral-text-default) |
| --dsc-input-color | var(--ds-color-neutral-text-default) |
| --dsc-input-stroke-color | var(--ds-color-base-contrast-default) |
| --dsc-input-stroke-color--invalid | var(--ds-color-danger-base-contrast-default) |
| --dsc-input-stroke-width | 0.05em |
| --dsc-input-accent-color | var(--ds-color-base-default) |
| --dsc-input-accent-color--invalid | var(--ds-color-danger-text-subtle) |
| --dsc-input-line-height | var(--ds-line-height-md) |
| Navn | Verdi |
|---|---|
| data-width | auto |