Hoppa till huvudinnehåll

Checkbox

Inmatningsfält för att låta användaren välja inget, ett eller flera av ett antal förvalda alternativ.

import { Checkbox } from '@midas-ds/components'
<Checkbox>Jag godkänner villkoren</Checkbox>

Varianter​

Ensam checkbox​

<Checkbox>Jag godkänner villkoren</Checkbox>

Förvalt alternativ (okontrollerat)​

Använd attributet defaultSelected för att en checkbox ska vara förvald.

<Checkbox defaultSelected />

Kontrollerat val​

Använd attributet isSelected och eventet onChange för att använda controlled mode på Checkbox.

import React from 'react'

const [isSelected, setIsSelected] = React.useState(false)

return (
<Checkbox
isSelected={isSelected}
onChange={setIsSelected}
>
Jag godkänner villkoren
</Checkbox>
)
Du har inte godkänt villkoren

Varianter för flera checkboxar​

Använd <CheckboxGroup> för att gruppera flera checkboxar som hör samman.

<CheckboxGroup
label='Godkänner du våra villkor?'
description='Läs texten först'
>
<Checkbox value='1'>Jag har läst texten</Checkbox>
<Checkbox value='2'>Jag godkänner villkoren</Checkbox>
</CheckboxGroup>
Läs texten först

Välj alla​

Ibland kan det vara lämpligt att ge användaren möjlighet att snabbt kryssa i alla kryssrutor. Denna funktion finns inbyggt i <CheckboxGroup> via egenskapen showSelectAll. Om detta används på en sida med paginerat innehåll så skall endast de kryssrutor som finns på den aktuella sidan väljas.

<CheckboxGroup
label='Godkänner du våra villkor'
description='Läs texten först'
showSelectAll
>
<Checkbox value='1'>Jag har läst texten</Checkbox>
<Checkbox value='2'>Jag godkänner villkoren</Checkbox>
</CheckboxGroup>
Läs texten först

Förvalda alternativ (okontrollerat)​

Använd attributet defaultValue för att en eller flera kryssrutor ska vara förvalda.

<CheckboxGroup
label='Favoritfrukt'
defaultValue={['banan', 'melon']}
>
<Checkbox value='banan'>Banan</Checkbox>
<Checkbox value='melon'>Melon</Checkbox>
<Checkbox value='kiwi'>Kiwi</Checkbox>
<Checkbox value='satsumas'>Satsumas</Checkbox>
</CheckboxGroup>

Kontrollerat val​

Använd attributet value och eventet onChange för att använda controlled mode på CheckboxGroup.

import React from 'react'

const [value, setValue] = React.useState<string[]>()

return (
<CheckboxGroup
label='Favoritfrukt'
value={value}
onChange={setValue}
>
<Checkbox value='banan'>Banan</Checkbox>
<Checkbox value='melon'>Melon</Checkbox>
<Checkbox value='kiwi'>Kiwi</Checkbox>
<Checkbox value='satsumas'>Satsumas</Checkbox>
</CheckboxGroup>
)
Valda alternativ: 

Riktlinjer​

  • Om det är många alternativ så bör Select användas istället.
  • Använd inte kryssruta om användaren väntar sig att valet ska få effekt direkt
  • Fältetikett ska inledas med en stor bokstav och inte följas av punkt.

API​

Checkbox​

NameTypeDefaultDescription
descriptionstring-
errorMessage-
errorPositiontop
className'react-aria-CheckboxField'

The CSS className for the element. A function may be provided to compute the class based on component state.

inputRef-

A ref for the HTML input element.

isDisabled-

Whether the input is disabled.

autoFocus-

Whether the element should receive focus on render.

valuestring-

The value of the input element, used when submitting an HTML form. See MDN.

isReadOnly-

Whether the input can be selected but not changed by the user.

isInvalid-

Whether the input value is invalid.

isRequired-

Whether user input is required on the input before form submission.

validate-

A function that returns an error message if a given value is invalid. Validation errors are displayed to the user when the form is submitted if validationBehavior="native". For realtime validation, use the isInvalid prop instead.

isIndeterminate-

Indeterminism is presentational only. The indeterminate visual representation remains regardless of user interaction.

defaultSelected-

Whether the element should be selected (uncontrolled).

isSelected-

Whether the element should be selected (controlled).

validationBehavior'native'

Whether to use native HTML form validation to prevent form submission when the value is missing or invalid, or mark the field as required or invalid via ARIA.

children-

The children of the component. A function may be provided to alter the children based on component state.

style-

The inline style for the element. A function may be provided to compute the style based on component state.

renderDOMRenderFunction<"div", CheckboxFieldRenderProps>-

Overrides the default DOM element with a custom render function. This allows rendering existing components with built-in styles and behaviors such as router links, animation libraries, and pre-styled components.

Requirements:

  • You must render the expected element type (e.g. if <button> is expected, you cannot render an <a>).
  • Only a single root DOM element can be rendered (no fragments).
  • You must pass through props and ref to the underlying DOM element, merging with your own prop as appropriate.
slot-

A slot name for the component. Slots allow the component to receive props from a parent component. An explicit null value indicates that the local props completely override all props received from a parent.

CheckboxGroup​