优化select 多选代码

This commit is contained in:
catchonme 2019-05-27 12:22:01 +08:00
parent 0a2ca2a0cd
commit 1487a0c897
3 changed files with 43 additions and 62 deletions

View File

@ -156,13 +156,17 @@
user-select: none;
}
&-checkall {
&-checkAll {
padding: (
$Form-select-menu-height - $Form-input-lineHeight *
$Form-input-fontSize - px2rem(2px)
)/2 $Form-select-paddingX;
border-bottom: px2rem(1px) solid $Form-select-checkall-bottomBorder;
min-width: px2rem(100px);
label {
display: block;
}
}
&-option {
@ -187,10 +191,6 @@
background-color: $Form-select-menu-onDisabled-bg;
}
&.is-checkAll {
border-bottom: px2rem(1px) solid $Form-select-checkall-bottomBorder;
}
&--placeholder {
color: $Form-input-placeholderColor;
}

View File

@ -1,4 +1,3 @@
/**
* @file Checkbox
* @author fex
@ -25,7 +24,6 @@ interface CheckboxProps {
label?: string;
className?: string;
onChange?: (value: any) => void;
onClick?: (e:any, checked:boolean) => void;
value?: any;
containerClass?: string;
inline?: boolean;
@ -58,25 +56,6 @@ export class Checkbox extends React.Component<CheckboxProps, any> {
onChange(e.currentTarget.checked ? trueValue : falseValue);
}
@autobind
handleClick(e:any) {
const {
checked,
value,
trueValue,
onClick,
disabled
} = this.props;
const isChecked:boolean = !!(typeof checked !== 'undefined'
? checked
: typeof value === 'undefined'
? value
: value == trueValue);
disabled || onClick && onClick(e, isChecked);
}
render() {
let {
size,
@ -105,7 +84,6 @@ export class Checkbox extends React.Component<CheckboxProps, any> {
},
className
)}
onClick={this.handleClick}
>
<input
type={type}

View File

@ -188,6 +188,7 @@ export class Select extends React.Component<SelectProps, SelectState> {
input: HTMLInputElement;
target: HTMLElement;
menu: React.RefObject<HTMLDivElement> = React.createRef();
constructor(props: SelectProps) {
super(props);
@ -252,7 +253,11 @@ export class Select extends React.Component<SelectProps, SelectState> {
});
}
toggle() {
toggle(e?: React.MouseEvent<HTMLDivElement>) {
if (e && this.menu.current && this.menu.current.contains(e.target as HTMLElement)) {
return;
}
this.props.disabled ||
this.setState({
isOpen: !this.state.isOpen,
@ -336,7 +341,7 @@ export class Select extends React.Component<SelectProps, SelectState> {
}
handleChange(selectItem: any) {
const {onChange, multiple, onNewOptionClick, checkAll} = this.props;
const {onChange, multiple, onNewOptionClick} = this.props;
let {selection} = this.state;
if (selectItem.isNew) {
@ -345,9 +350,6 @@ export class Select extends React.Component<SelectProps, SelectState> {
}
if (multiple) {
if (checkAll && selectItem.__all) {
this.toggleCheckAll();
} else {
selection = selection.concat();
const idx = selection.indexOf(selectItem);
if (~idx) {
@ -356,7 +358,6 @@ export class Select extends React.Component<SelectProps, SelectState> {
selection.push(selectItem);
}
onChange(selection);
}
} else {
onChange(selectItem);
}
@ -471,7 +472,6 @@ export class Select extends React.Component<SelectProps, SelectState> {
if (multiple) {
if (checkAll) {
filtedOptions.unshift({label: checkAllLabel, value: 'all', __all: true});
const optionsValues = options.map(option => option.value);
const selectionValues = selection.map(select => select.value);
checkedAll = optionsValues.every(option => selectionValues.indexOf(option) > -1);
@ -499,7 +499,18 @@ export class Select extends React.Component<SelectProps, SelectState> {
}
const menu = (
<div className={cx('Select-menu')}>
<div ref={this.menu} className={cx('Select-menu')}>
{multiple && checkAll ? (
<div className={cx('Select-checkAll')}>
<Checkbox
checked={checkedPartial}
partial={checkedPartial && !checkedAll}
onChange={this.toggleCheckAll}
>
{checkAllLabel}
</Checkbox>
</div>
) : null}
{filtedOptions.length ? (
filtedOptions.map((item, index) => {
const checked = checkAll ? selection.some((o:Option) => o.value == item.value) : false;
@ -515,24 +526,16 @@ export class Select extends React.Component<SelectProps, SelectState> {
className={cx(`Select-option`, {
'is-disabled': item.disabled,
'is-highlight': highlightedIndex === index,
'is-checkAll': checkAll && index === 0,
'is-active':
selectedItem === item ||
(Array.isArray(selectedItem) && ~selectedItem.indexOf(item)),
})}
>
{checkAll ?
index === 0 ? (
<Checkbox
checked={checkedPartial}
partial={checkedPartial && !checkedAll}
>
{checkAllLabel}
</Checkbox>
) : (
{checkAll ? (
<Checkbox
checked={checked}
trueValue={item.value}
onChange={() => this.handleChange(item)}
>
{item.isNew
? promptTextCreator(item.label as string)