checkboxes 添加增删改功能

This commit is contained in:
2betop 2020-03-26 11:36:41 +08:00
parent d211582169
commit dee9010253
2 changed files with 96 additions and 5 deletions

View File

@ -25,6 +25,12 @@
margin-left: $Checkbox-gap; margin-left: $Checkbox-gap;
cursor: pointer; cursor: pointer;
> a {
float: right;
margin-left: px2rem(5px);
display: none;
}
&:empty { &:empty {
display: none; display: none;
} }
@ -44,6 +50,10 @@
} }
} }
&:hover > i + span > a {
display: inline-block;
}
&--checkbox { &--checkbox {
padding-left: $Checkbox-size; padding-left: $Checkbox-size;
@ -262,3 +272,20 @@
.#{$ns}CheckboxesControl-groupLabel { .#{$ns}CheckboxesControl-groupLabel {
display: block; display: block;
} }
.#{$ns}Checkboxes {
&-addBtn {
display: block;
cursor: pointer;
&:hover {
text-decoration: none;
}
> svg {
width: px2rem(14px);
height: px2rem(14px);
margin-right: $Checkbox-gap;
}
}
}

View File

@ -3,22 +3,33 @@ import {OptionsControl, OptionsControlProps, Option} from './Options';
import cx from 'classnames'; import cx from 'classnames';
import Checkbox from '../../components/Checkbox'; import Checkbox from '../../components/Checkbox';
import chunk from 'lodash/chunk'; import chunk from 'lodash/chunk';
import {Icon} from '../../components/icons';
import {Api} from '../../types';
import {autobind} from '../../utils/helper';
export interface CheckboxesProps extends OptionsControlProps { export interface CheckboxesProps extends OptionsControlProps {
placeholder?: any; placeholder?: any;
itemClassName?: string; itemClassName?: string;
columnsCount?: number; columnsCount?: number;
labelClassName?: string; labelClassName?: string;
onAdd?: () => void;
addApi?: Api;
creatable: boolean;
createBtnLabel: string;
editable?: boolean;
removable?: boolean;
} }
export default class CheckboxesControl extends React.Component< export default class CheckboxesControl extends React.Component<
CheckboxesProps, CheckboxesProps,
any any
> { > {
static defaultProps: Partial<CheckboxesProps> = { static defaultProps = {
columnsCount: 1, columnsCount: 1,
multiple: true, multiple: true,
placeholder: '暂无选项' placeholder: '暂无选项',
creatable: false,
createBtnLabel: '新增选项'
}; };
componentDidMount() { componentDidMount() {
@ -41,6 +52,28 @@ export default class CheckboxesControl extends React.Component<
reload && reload(); reload && reload();
} }
@autobind
handleAddClick() {
const {onAdd} = this.props;
onAdd && onAdd();
}
@autobind
handleEditClick(e: Event, item: any) {
const {onEdit} = this.props;
e.preventDefault();
e.stopPropagation();
onEdit && onEdit(item);
}
@autobind
handleDeleteClick(e: Event, item: any) {
const {onDelete} = this.props;
e.preventDefault();
e.stopPropagation();
onDelete && onDelete(item);
}
renderGroup(option: Option, index: number) { renderGroup(option: Option, index: number) {
const {classnames: cx, labelField} = this.props; const {classnames: cx, labelField} = this.props;
@ -75,7 +108,10 @@ export default class CheckboxesControl extends React.Component<
selectedOptions, selectedOptions,
disabled, disabled,
inline, inline,
labelClassName labelClassName,
labelField,
removable,
editable
} = this.props; } = this.props;
return ( return (
@ -88,7 +124,25 @@ export default class CheckboxesControl extends React.Component<
inline={inline} inline={inline}
labelClassName={labelClassName} labelClassName={labelClassName}
> >
{option.label} {removable ? (
<a data-tooltip="移除" data-position="left">
<Icon
icon="minus"
className="icon"
onClick={(e: any) => this.handleDeleteClick(e, option)}
/>
</a>
) : null}
{editable ? (
<a data-tooltip="编辑" data-position="left">
<Icon
icon="pencil"
className="icon"
onClick={(e: any) => this.handleEditClick(e, option)}
/>
</a>
) : null}
{option[labelField || 'label']}
</Checkbox> </Checkbox>
); );
} }
@ -107,7 +161,10 @@ export default class CheckboxesControl extends React.Component<
checkAll, checkAll,
classnames: cx, classnames: cx,
itemClassName, itemClassName,
labelClassName labelClassName,
creatable,
addApi,
createBtnLabel
} = this.props; } = this.props;
let body: Array<React.ReactNode> = []; let body: Array<React.ReactNode> = [];
@ -161,6 +218,13 @@ export default class CheckboxesControl extends React.Component<
) : ( ) : (
<span className={`Form-placeholder`}>{placeholder}</span> <span className={`Form-placeholder`}>{placeholder}</span>
)} )}
{(creatable || addApi) && !disabled ? (
<a className={cx('Checkboxes-addBtn')} onClick={this.handleAddClick}>
<Icon icon="plus" className="icon" />
{createBtnLabel}
</a>
) : null}
</div> </div>
); );
} }