import React, { useState, useCallback, useMemo } from 'react';
import { storiesOf } from '@storybook/react'; // import { withKnobs, text, boolean } from '@storybook/addon-knobs';
import Button from '../../button';
import Popover from '../../popover';
import Checkbox from '../index';
import CheckboxGroup from '../checkboxGroup';
import { Input, Row, Col } from '../../index';
import { IconClose } from '@douyinfe/semi-icons';
const stories = storiesOf('Checkbox', module); // stories.addDecorator(withKnobs);;
stories.add('checkbox default', () => {
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
);
});
stories.add('checkbox without text', () => {
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'}
);
}
}
stories.add('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
抖音
火山
今日头条
西瓜视频
>
);
}
}
stories.add('checkbox group', () => );
stories.add('checkbox group with options ', () => {
function onChange(checkedValues) {
console.log('checked = ', checkedValues);
}
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',
disabled: false,
},
];
return (
default
受控
最后一个disabled
全体disabled, 优先父级disabled,次选子级disabled
);
});
stories.add('checkboxGroup-直接后代是其他类型Node', () => {
return (
Apple
Pear
Orange
);
});
stories.add('checkbox 主文本+副文本', () => {
let options = [
{
label: 'Apple',
value: 'Apple',
extra: '苹果',
},
{
label: 'Pear',
value: 'Pear',
extra: '梨',
},
{
label: 'Orange',
value: 'Orange',
disabled: true,
extra: '橙子',
},
];
return (
checkbox
console.log(e)}
extra="我是副文本,这是辅助的文本,辅助文本会更长一些,甚至还可能换行"
>
我是主文本
console.log(e)}
extra="我是副文本,这是辅助的文本,辅助文本会更长一些,甚至还可能换行"
>
我是主文本
checkboxGroup
Apple
Pear
Orange
checkboxGroup with options
);
});
stories.add('checkbox + grid', () => {
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
);
});
const plainOptions = ['Apple', 'Pear', 'Orange'];
const defaultCheckedList = ['Apple', 'Orange'];
class IndeterminateDemo extends React.Component {
state = {
checkedList: defaultCheckedList,
indeterminate: true,
checkAll: false,
};
onChange = checkedList => {
this.setState({
checkedList,
indeterminate: !!checkedList.length && checkedList.length < plainOptions.length,
checkAll: checkedList.length === plainOptions.length,
});
};
onCheckAllChange = e => {
this.setState({
checkedList: e.target.checked ? plainOptions : [],
indeterminate: false,
checkAll: e.target.checked,
});
};
render() {
return (
);
}
}
stories.add('checkbox inderterminate联动', () => );
stories.add('checkbox render in div', () => (
<>
{
console.log('clicked checkbox outer: ', ...args);
}}
>
{
console.log('clicked checkbox: ', ...args);
}}
/>
>
));
stories.add(`checkbox in popover`, () => (
}
>
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
>
};
stories.add('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)}>
);
})
) : (
暂无内容,可从左侧勾选
)}
);
};
stories.add(`bugDemo`, () => );
stories.add(`checkboxGroup card style`, () => (
<>
常见情况
多选框标题
多选框标题
多选框标题
radio disabled
多选框标题
多选框标题
多选框标题
checkboxGroup disabled
多选框标题
多选框标题
多选框标题
文字很长,并且没有设置宽度,因此换行显示
多选框标题
多选框标题
多选框标题
设置了width=180
多选框标题
多选框标题
多选框标题
没有extra,width=180
多选框标题
多选框标题
多选框标题
没有标题,width=380
下面是垂直的情况:
常见情况
多选框标题
多选框标题
多选框标题
没有设置宽度
多选框标题
多选框标题
多选框标题
设置了width=380
多选框标题
多选框标题
多选框标题
>
));
stories.add(`checkboxGroup pureCard style`, () => (
<>
常见情况
多选框标题
多选框标题
多选框标题
radio disabled
多选框标题
多选框标题
多选框标题
checkboxGroup disabled
多选框标题
多选框标题
多选框标题
文字很长,并且没有设置宽度,因此换行显示
多选框标题
多选框标题
多选框标题
设置了width=180
多选框标题
多选框标题
多选框标题
没有extra,width=180
多选框标题
多选框标题
多选框标题
没有标题,width=380
下面是垂直的情况:
常见情况
多选框标题
多选框标题
多选框标题
没有设置宽度
多选框标题
多选框标题
多选框标题
设置了width=380
多选框标题
多选框标题
多选框标题
>
));