Checkbox

Examples

Basic

Basic usage of checkbox.

<Checkbox bind:checked on:change="{onChange}">Checkbox</Checkbox>

<script>
  import { Checkbox } from "svant";

  let checked = false;

  function onChange() {
    console.log(`checked = ${checked}`);
  }
</script>

Disabled

Disabled checkbox.


<div>
  <Checkbox disabled />
  <br />
  <Checkbox bind:checked disabled />
</div>

<script>
  import { Checkbox } from "svant";

  let checked = true;
</script>

Controlled Checkbox

Communicated with other components.

<div>
  <p style="{{ marginBottom: '20px' }}">
    <Checkbox bind:checked {disabled} on:change="{onChange}">{label}</Checkbox>
  </p>
  <p>
    <Button type="primary" size="small" on:click="{toggleChecked}">
      {!checked ? 'Check' : 'Uncheck'}
    </Button>
    <Button
      style="{{ margin: '0 10px' }}"
      type="primary"
      size="small"
      on:click="{toggleDisable}">
      {!disabled ? 'Disable' : 'Enable'}
    </Button>
  </p>
</div>

<script>
  import { Checkbox } from "svant";
  import { Button } from "svant";
  let checked = true;
  let disabled = false;
  let label;
  $: label = `${checked ? "Checked" : "Unchecked"}-${
    disabled ? "Disabled" : "Enabled"
  }`;
  function toggleChecked() {
    checked = !checked;
  }

  function toggleDisable() {
    disabled = !disabled;
  }

  function onChange() {
    console.log("checked = ", checked);
  }
</script>

Checkbox Group

Generate a group of checkboxes from an array.





<div>
  <CheckboxGroup
    options="{plainOptions}"
    bind:value="{value1}"
    on:change="{onChange}" />
  <br />
  <br />
  <CheckboxGroup {options} bind:value="{value2}" on:change="{onChange}" />
  <br />
  <br />
  <CheckboxGroup
    options="{optionsWithDisabled}"
    disabled
    bind:value="{value3}"
    on:change="{onChange}" />
</div>

<script>
  import { CheckboxGroup } from "svant";

  let value1 = ["Apple"];
  let value2 = ["Pear"];
  let value3 = ["Apple"];

  const plainOptions = ["Apple", "Pear", "Orange"];
  const options = [
    { label: "Apple", value: "Apple" },
    { label: "Pear", value: "Pear" },
    { label: "Orange", value: "Orange", disabled: true }
  ];
  const optionsWithDisabled = [
    { label: "Apple", value: "Apple" },
    { label: "Pear", value: "Pear" },
    { label: "Orange", value: "Orange" }
  ];

  function onChange({ detail: checkedValues }) {
    console.log("checked = ", checkedValues);
  }
</script>

Check all

The indeterminate property can help you to achieve a 'check all' effect.


<div>
  <div className="site-checkbox-all-wrapper">
    <Checkbox
      {indeterminate}
      on:change="{onCheckAllChange}"
      checked="{checkAll}">
      Check all
    </Checkbox>
  </div>
  <br />
  <CheckboxGroup
    options="{plainOptions}"
    bind:value="{checkedList}"
    on:change="{onChange}" />
</div>

<script>
  import { CheckboxGroup, Checkbox } from "svant";

  const plainOptions = ["Apple", "Pear", "Orange"];
  let checkedList = ["Apple", "Orange"];
  let indeterminate = true;
  let checkAll = false;

  function onChange({ detail }) {
    indeterminate =
      checkedList.length && checkedList.length < plainOptions.length;
    checkAll = checkedList.length === plainOptions.length;
  }

  function onCheckAllChange({ detail }) {
    console.log(detail.target.checked);
    checkedList = detail.target.checked ? plainOptions : [];
    indeterminate = false;
    checkAll = detail.target.checked;
  }
</script>

Checkbox Group With Slot

Instead of using the options attribute you can just provide the checkboxes in the slot

<CheckboxGroup bind:value style="{{ width: '100%' }}" on:change="{onChange}">
  <Checkbox value="A">A</Checkbox>
  <Checkbox value="B">B</Checkbox>
  <Checkbox value="C">C</Checkbox>
  <Checkbox value="D">D</Checkbox>
  <Checkbox value="E">E</Checkbox>
</CheckboxGroup>

<script>
  import { CheckboxGroup, Checkbox } from "svant";
  let value = [];
  function onChange() {
    console.log("checked = ", value);
  }
</script>

API

Checkbox Attributes

PropertyDescriptionTypeDefault
autoFocusSet focus when component mounted.Booleanfalse
checkedSpecifies whether the checkbox is selected. Can be used for 2 way binding.Booleanfalse
disabledDisable checkbox.Booleanfalse
indeterminateIndeterminate checked state of checkbox.Booleanfalse

CheckboxGroup Attributes

PropertyDescriptionTypeDefault
disabledDisable all checkboxes.Booleanfalse
nameThe name property of all input[type="checkbox"] children.String-
optionsSpecifies options.String[] | [{label:"",value:"",disabled:false}][]
valueUsed for setting the currently selected value.String[][]

Checkbox Events

NameDescription
changeTriggers when the checkbox checked state changes.

CheckboxGroup Events

NameDescription
changeTriggers when the checkbox group value changes.

Checkbox Bind

NameDescription
focusLet's you bind to the input focus method.
blurLet's you bind to the input blur method.