import React, { useCallback, useMemo, useState } from 'react';
import Button from '../../button';
import Popover from '../../popover';
import Tag from '../../tag';
import Cascader from '../../cascader';
import Checkbox from '../index';
import CheckboxGroup from '../checkboxGroup';
import { Col, Input, Row } from '../../index';
import { IconClose } from '@douyinfe/semi-icons';
import { getUuidShort } from '@douyinfe/semi-foundation/utils/uuid';
export default {
title: 'Checkbox',
}
export const CheckboxDefault = () => {
return (
console.log(e)} value={1} onChange={v => console.log(v)}>
hello
这是一个受控的checked=true的checkbox,没有配onChange
这是一个不受控的defaultChecked=true的checkbox
这是一个受控的disabled=true的checkbox
既checked又disabled
indeterminate
console.log(e)} value={1} onChange={v => console.log(v)}>
);
};
export const CheckboxWithoutText = () => {
return (
console.log(e)} />
);
};
class CheckboxControl extends React.Component {
state = {
checked: true,
disabled: false,
};
toggleChecked = () => {
this.setState({
checked: !this.state.checked,
});
};
toggleDisable = () => {
this.setState({
disabled: !this.state.disabled,
});
};
onChange = e => {
console.log('checked = ', e.target.checked);
this.setState({
checked: e.target.checked,
});
};
render() {
const label = `${this.state.checked ? 'Checked' : 'Unchecked'}-${
this.state.disabled ? 'Disabled' : 'Enabled'
}`;
return (
{label}
{!this.state.checked ? 'Check' : 'Uncheck'}
{!this.state.disabled ? 'Disable' : 'Enable'}
);
}
}
export const CheckboxControlledDisabledChecked = () => ;
CheckboxControlledDisabledChecked.story = {
name: 'checkbox controlled disabled & checked',
};
class GroupDemo extends React.Component {
constructor() {
super();
this.state = {
value: [],
};
this.onChange = this.onChange.bind(this);
}
onChange(value) {
console.log(value);
this.setState({
value: value,
});
}
render() {
let { value } = this.state;
return (
<>
水平Group
console.log(v)}>
抖音
火山
今日头条
西瓜视频
垂直Group
console.log(v)}>
抖音
火山
今日头条
西瓜视频
默认Group
抖音
火山
今日头条
西瓜视频
受控Group
抖音
火山
今日头条
西瓜视频
受控Group+onChange
抖音
火山
今日头条
西瓜视频
disabled
抖音
火山
今日头条
西瓜视频
>
);
}
}
export const CheckboxGroupDemo = () => ;
export const CheckboxGroupWithOptions = () => {
function onChange(checkedValues) {
console.log('checked = ', checkedValues);
}
const plainOptions = ['green', 'red', 'pink'];
const options = [
{
label: 'green',
value: 'green',
},
{
label: 'red',
value: 'red',
},
{
label: 'pink',
value: 'pink',
disabled: true,
},
];
const optionsWithDisabled = [
{
label: 'green',
value: 'green',
},
{
label: 'red',
value: 'red',
},
{
label: 'pink',
value: 'pink',
disabled: false,
},
];
return (
default
受控
最后一个disabled
全体disabled, 优先父级disabled,次选子级disabled
);
};
export const CheckboxGroupWithOtherTypeChild = () => {
return (
green
red
pink
);
};
CheckboxGroupWithOtherTypeChild.story = {
name: 'checkboxGroup-直接后代是其他类型Node',
};
export const CheckboxExtra = () => {
let options = [
{
label: 'green',
value: 'green',
extra: '苹果',
},
{
label: 'red',
value: 'red',
extra: '梨',
},
{
label: 'pink',
value: 'pink',
disabled: true,
extra: '橙子',
},
];
return (
checkbox
console.log(e)}
extra="我是副文本,这是辅助的文本,辅助文本会更长一些,甚至还可能换行"
>
我是主文本
console.log(e)}
extra="我是副文本,这是辅助的文本,辅助文本会更长一些,甚至还可能换行"
>
我是主文本
checkboxGroup
green
red
pink
checkboxGroup with options
);
};
export const CheckboxGrid = () => {
return (
console.log(v)}
>
无限长的一串字The Storybook webapp UI can be customised with this addon. It can be used
to change the header, show/hide various UI elements and to enable full-screen mode by
default.
B
C
D
E
);
};
CheckboxGrid.story = {
name: 'checkbox + grid',
};
export const IndeterminateDemo = () => {
const options = ['yellow', 'green', 'red'];
const defaultCheckedColors = ['yellow', 'red'];
const [checkedList, setCheckList] = useState(defaultCheckedColors);
const [indeterminate, setIndeterminate] = useState(true);
const [checkAll, setCheckAll] = useState(false);
const onCheckListChange = checkedList => {
setCheckList([...checkedList]);
setIndeterminate(!!checkedList.length && checkedList.length < options.length);
setCheckAll(checkedList.length === options.length);
};
const onCheckAllChange = e => {
setCheckList([...(e.target.checked ? options : [])]);
setIndeterminate(false);
setCheckAll(e.target.checked);
};
return (
);
};
export const CheckboxRenderInDiv = () => (
<>
{
console.log('clicked checkbox outer: ', ...args);
}}
>
{
console.log('clicked checkbox: ', ...args);
}}
/>
>
);
export const CheckboxInPopover = () => (
}
>
click me
)
const SwitchCheckedFromTrue2Undefined = () => {
const [props, setProps] = useState();
const [flag, setFlag] = useState(0);
const change = () => {
if (flag === 0) {
setFlag(1);
setProps({ checked: true });
} else {
setFlag(0);
setProps({ checked: false });
}
};
return (
<>
123
change()}>switch
>
);
};
export const CheckboxSwitchCheckedTrueUndefined = () => ;
CheckboxSwitchCheckedTrueUndefined.story = {
name: 'checkbox switch checked: true => undefined',
};
const TransformSelect = props => {
const { onChange, value, options = [], defaultValue = [], placeholder } = props;
const [currentValue, setCurrentValue] = useState([]);
const [inputValue, setInputValue] = useState(''); // 变化
const onSelectChange = useCallback(() => {
setCurrentValue(currentValue);
onChange && onChange(currentValue);
}, []); // 选择某一个
const removeValue = useCallback(
currentIndex => {
currentValue.splice(currentIndex, 1);
onSelectChange([...currentValue]);
},
[currentValue]
); // 选择所有
const selectAllValue = useCallback(() => {
const value = options.map(option => option.value);
onSelectChange(value);
}, [options]);
const viewsOptions = useMemo(() => {
if (inputValue) {
const newOptions = options.filter(option => option.label.indexOf(inputValue) !== -1);
return newOptions;
}
return options;
}, [options, inputValue]);
return (
setInputValue(value)}
placeholder={placeholder}
/>
{`共 ${options.length} 项`}
selectAllValue()}>
全选
{`已选 ${currentValue.length} 项`}
onSelectChange([])}
>
清空
{currentValue.length > 0 ? (
currentValue.map((value, idx) => {
// 不存在不需要展示
const option = options.find(option => option.value === value);
return (
{option.label}
removeValue(idx)}>
);
})
) : (
暂无内容,可从左侧勾选
)}
);
};
export const BugDemo = () => ;
export const CheckboxGroupCardStyle = () => (
<>
常见情况
多选框标题
多选框标题
多选框标题
Checkbox disabled
多选框标题
多选框标题
多选框标题
checkboxGroup disabled
多选框标题
多选框标题
多选框标题
文字很长,并且没有设置宽度,因此换行显示
多选框标题
多选框标题
多选框标题
设置了width=180
多选框标题
多选框标题
多选框标题
没有extra,width=180
多选框标题
多选框标题
多选框标题
没有标题,width=380
下面是垂直的情况:
常见情况
多选框标题
多选框标题
多选框标题
没有设置宽度
多选框标题
多选框标题
多选框标题
设置了width=380
多选框标题
多选框标题
多选框标题
>
);
export const CheckboxGroupPureCardStyle = () => (
<>
常见情况
多选框标题
多选框标题
多选框标题
Checkbox disabled
多选框标题
多选框标题
多选框标题
checkboxGroup disabled
多选框标题
多选框标题
多选框标题
文字很长,并且没有设置宽度,因此换行显示
多选框标题
多选框标题
多选框标题
设置了width=180
多选框标题
多选框标题
多选框标题
没有extra,width=180
多选框标题
多选框标题
多选框标题
没有标题,width=380
下面是垂直的情况:
常见情况
多选框标题
多选框标题
多选框标题
没有设置宽度
多选框标题
多选框标题
多选框标题
设置了width=380
多选框标题
多选框标题
多选框标题
>
);
export const CheckboxOnChangeEvent = () => (
查看 onChange 入参
console.log(e)}>
Apple
Popover 内套 Popover, 且 content 为 checkbox
console.log('onClickOutSide')}
content={
{
console.log('checkbox onChange', e);
e.stopPropagation();
e.nativeEvent && e.nativeEvent.stopImmediatePropagation();
}}
>
Semi Design
}
>
trigger
}
>
点击此处
Popover 内套 Cascader 多选
}
>
点击此处
);
CheckboxOnChangeEvent.story = {
name: 'checkbox onChange event',
};