以下组件包含在内
复选框
名称 | 类型 | 默认 | 描述 |
---|---|---|---|
checked | 布尔值 | false | 定义复选框输入是否选中 |
colors | 对象 | 包含 Tailwind CSS 颜色类别的对象 | |
colors.bgCheckedIos | 字符串 | 'bg-primary' | |
colors.bgCheckedMaterial | 字符串 | 'bg-md-light-primary dark:bg-md-dark-primary' | |
colors.borderCheckedIos | 字符串 | 'border-primary' | |
colors.borderCheckedMaterial | 字符串 | 'border-md-light-primary dark:border-md-dark-primary' | |
colors.borderIos | 字符串 | 'border-black border-opacity-30 dark:border-white dark:border-opacity-30' | |
colors.borderMaterial | 字符串 | 'border-md-light-outline dark:border-md-dark-outline' | |
component | 字符串 | 'label' | 组件的 HTML 元素 |
defaultChecked | 布尔值 | false | 定义复选框输入是否选中,用于非受控组件的情况 |
disabled | 布尔值 | false | 定义复选框输入是否禁用 |
indeterminate | 布尔值 | false | 定义复选框输入是否处于不确定状态 |
name | 字符串 | 复选框输入名称 | |
readOnly | 布尔值 | false | 定义复选框输入是否只读 |
touchRipple | 布尔值 | true | 在 Material 主题中启用触摸涟漪效果 |
value | 任何 | 复选框输入值 | |
onChange | function(e) | 当复选框状态改变时会触发事件 |
复选框列表不是一个单独的组件,而只是使用 <List>
和 <ListItem>
组件的一种特殊情况。
<List>
{/* Additional "label" prop on ListItem */}
<ListItem
label
title="Books"
media={
<>
{/* Pass Checkbox to list item media */}
<Checkbox component="div" name="my-checkbox" />
</>
}
/>
<ListItem
label
title="Movies"
media={
<>
{/* Pass Checkbox to list item media */}
<Checkbox component="div" name="my-checkbox" />
</>
}
/>
</List>
import React, { useState } from 'react';import {Page,Navbar,NavbarBackLink,Checkbox,BlockTitle,Block,List,ListItem,} from 'konsta/react';export default function CheckboxPage() {const [checked1, setChecked1] = useState(false);const [checked2, setChecked2] = useState(true);// Groupconst [group, setGroup] = useState(['Books']);const toggleGroupValue = (value) => {if (group.includes(value)) group.splice(group.indexOf(value), 1);else group.push(value);setGroup([...group]);};// Indeterminateconst [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([]);}};// Mediaconst [media, setMedia] = useState(['Item 1']);const toggleMediaValue = (value) => {if (media.includes(value)) media.splice(media.indexOf(value), 1);else media.push(value);setMedia([...media]);};return (<Page><Navbartitle="Checkbox"/><BlockTitle>Inline</BlockTitle><Block strongIos outlineIos><p>Lorem{' '}<Checkboxname="checkbox-1"checked={checked1}onChange={(e) => setChecked1(e.target.checked)}/>{' '}ipsum dolor sit amet, consectetur adipisicing elit. Alias beatae illonihil aut eius commodi sint eveniet aliquid eligendi{' '}<Checkboxname="checkbox-2"checked={checked2}onChange={(e) => setChecked2(e.target.checked)}/>{' '}ad delectus impedit tempore nemo, enim vel praesentium consequaturnulla mollitia!</p></Block><BlockTitle>Checkbox Group</BlockTitle><List strongIos outlineIos><ListItemlabeltitle="Books"media={<Checkboxcomponent="div"name="demo-checkbox"checked={group.includes('Books')}onChange={() => toggleGroupValue('Books')}/>}/><ListItemlabeltitle="Movies"media={<Checkboxcomponent="div"name="demo-checkbox"checked={group.includes('Movies')}onChange={() => toggleGroupValue('Movies')}/>}/><ListItemlabeltitle="Food"media={<Checkboxcomponent="div"name="demo-checkbox"checked={group.includes('Food')}onChange={() => toggleGroupValue('Food')}/>}/><ListItemlabeltitle="Drinks"media={<Checkboxcomponent="div"name="demo-checkbox"checked={group.includes('Drinks')}onChange={() => toggleGroupValue('Drinks')}/>}/></List><BlockTitle>Indeterminate State</BlockTitle><List strongIos outlineIos><ListItemlabeltitle="Movies"name="demo-checkbox"media={<Checkboxchecked={movies.length === 2}indeterminate={movies.length === 1}onChange={onMoviesChange}/>}><ul className="ps-12"><ListItemlabeltitle="Movie 1"media={<Checkboxname="demo-checkbox"value="Movie 1"checked={movies.indexOf('Movie 1') >= 0}onChange={onMovieChange}/>}/><ListItemlabeltitle="Movie 2"media={<Checkboxname="demo-checkbox"value="Movie 2"checked={movies.indexOf('Movie 2') >= 0}onChange={onMovieChange}/>}/></ul></ListItem></List><BlockTitle>With Media Lists</BlockTitle><List strongIos outlineIos><ListItemlabeltitle="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."media={<Checkboxcomponent="div"name="demo-media-checkbox"checked={media.includes('Item 1')}onChange={() => toggleMediaValue('Item 1')}/>}/><ListItemlabeltitle="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."media={<Checkboxcomponent="div"name="demo-media-checkbox"checked={media.includes('Item 2')}onChange={() => toggleMediaValue('Item 2')}/>}/></List></Page>);}