Checkbox React Component

Checkbox React component represents Checkbox component.

Checkbox Components

There are following components included:

  • Checkbox

Checkbox Properties

PropTypeDefaultDescription
<Checkbox> properties
checkedbooleanDefines whether the checkbox input is checked or not
defaultCheckedbooleanDefines whether the checkbox input is checked or not, for the case if it is uncontrolled component
indeterminatebooleanDefines whether the checkbox input is in indeterminate state or not
namestring
number
Checkbox input name
valuestring
number
boolean
Checkbox input value
disabledbooleanDefines whether the checkbox input is disabled
readonlybooleanDefines whether the checkbox input is readonly

Checkbox Events

EventDescription
<Checkbox> events
changeEvent will be triggered when checkbox state changed

Checkboxes List

Checkboxes List is not a separate component, but just a particular case of using <List>, <ListItem>.

<List>
  <!-- Additional "checkbox" prop to enable checkbox list item -->
  <ListItem checkbox value="check_1" checked title="Checkbox 1"></ListItem>
  <ListItem checkbox value="check_2" title="Checkbox 2"></ListItem>
</List>

Examples

import React, { useState } from 'react';
import {
  Page,
  Navbar,
  BlockTitle,
  Block,
  Checkbox,
  List,
  ListItem,
} from 'framework7-react';

export default () => {
  const [movies, setMovies] = useState(['Movie 1']);
  const onMovieChange = (e) => {
    const value = e.target.value;
    if (e.target.checked) {
      movies.push(value);
    } else {
      movies.splice(movies.indexOf(value), 1);
    }
    setMovies([...movies]);
  };
  const onMoviesChange = () => {
    if (movies.length === 1 || movies.length === 0) {
      setMovies(['Movie 1', 'Movie 2']);
    } else if (movies.length === 2) {
      setMovies([]);
    }
  };
  return (
    
      
    <Page>
      <Navbar title="Checkbox" />
      <BlockTitle>Inline</BlockTitle>
      <Block strong>
        <p>
          Lorem <Checkbox name="checkbox-1"></Checkbox> ipsum dolor sit amet, consectetur
          adipisicing elit. Alias beatae illo nihil aut eius commodi sint eveniet aliquid
          eligendi <Checkbox name="checkbox-2" defaultChecked></Checkbox> ad delectus impedit
          tempore nemo, enim vel praesentium consequatur nulla mollitia!
        </p>
      </Block>

      <BlockTitle>Checkbox Group</BlockTitle>
      <List>
        <ListItem checkbox title="Books" name="demo-checkbox" defaultChecked />
        <ListItem checkbox title="Movies" name="demo-checkbox" />
        <ListItem checkbox title="Food" name="demo-checkbox" />
        <ListItem checkbox title="Drinks" name="demo-checkbox" />
      </List>

      <BlockTitle>Indeterminate State</BlockTitle>
      <List>
        <ListItem
          checkbox
          title="Movies"
          name="demo-checkbox"
          checked={movies.length === 2}
          indeterminate={movies.length === 1}
          onChange={(e) => onMoviesChange(e)}
        >
          <ul slot="root">
            <ListItem
              checkbox
              title="Movie 1"
              name="demo-checkbox"
              value="Movie 1"
              checked={movies.indexOf('Movie 1') >= 0}
              onChange={(e) => onMovieChange(e)}
            />
            <ListItem
              checkbox
              title="Movie 2"
              name="demo-checkbox"
              value="Movie 2"
              checked={movies.indexOf('Movie 2') >= 0}
              onChange={(e) => onMovieChange(e)}
            />
          </ul>
        </ListItem>
      </List>

      <BlockTitle>With Media Lists</BlockTitle>
      <List mediaList>
        <ListItem
          checkbox
          defaultChecked
          name="demo-media-checkbox"
          title="Facebook"
          after="17:14"
          subtitle="New messages from John Doe"
          text="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla sagittis tellus ut turpis condimentum, ut dignissim lacus tincidunt. Cras dolor metus, ultrices condimentum sodales sit amet, pharetra sodales eros. Phasellus vel felis tellus. Mauris rutrum ligula nec dapibus feugiat. In vel dui laoreet, commodo augue id, pulvinar lacus."
        />
        <ListItem
          checkbox
          name="demo-media-checkbox"
          title="John Doe (via Twitter)"
          after="17:11"
          subtitle="John Doe (@_johndoe) mentioned you on Twitter!"
          text="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla sagittis tellus ut turpis condimentum, ut dignissim lacus tincidunt. Cras dolor metus, ultrices condimentum sodales sit amet, pharetra sodales eros. Phasellus vel felis tellus. Mauris rutrum ligula nec dapibus feugiat. In vel dui laoreet, commodo augue id, pulvinar lacus."
        />
        <ListItem
          checkbox
          name="demo-media-checkbox"
          title="Facebook"
          after="16:48"
          subtitle="New messages from John Doe"
          text="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla sagittis tellus ut turpis condimentum, ut dignissim lacus tincidunt. Cras dolor metus, ultrices condimentum sodales sit amet, pharetra sodales eros. Phasellus vel felis tellus. Mauris rutrum ligula nec dapibus feugiat. In vel dui laoreet, commodo augue id, pulvinar lacus."
        />
        <ListItem
          checkbox
          name="demo-media-checkbox"
          title="John Doe (via Twitter)"
          after="15:32"
          subtitle="John Doe (@_johndoe) mentioned you on Twitter!"
          text="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla sagittis tellus ut turpis condimentum, ut dignissim lacus tincidunt. Cras dolor metus, ultrices condimentum sodales sit amet, pharetra sodales eros. Phasellus vel felis tellus. Mauris rutrum ligula nec dapibus feugiat. In vel dui laoreet, commodo augue id, pulvinar lacus."
        />
      </List>
    </Page>
      
    
  );
};