Skip to content

Commit 983fa14

Browse files
committed
【级联选择器】添加备注
1 parent f9170ec commit 983fa14

1 file changed

Lines changed: 52 additions & 32 deletions

File tree

src/components/c-cascader/index.js

Lines changed: 52 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
/* eslint-disable */
2-
31
import React, { Component } from 'react';
42
import CascaderMenu from 'rc-cascader';
5-
import Icon from '../icon';
63
import PropTypes from 'prop-types';
74
import { prefixCls } from '@utils';
85
import {
@@ -11,23 +8,27 @@ import {
118
DefaultRenderEmpty,
129
ALL_VALUE,
1310
} from './constant.js';
11+
import Icon from '../icon';
1412
import './index.less';
1513

1614
class Cascader extends Component {
17-
state = {
18-
inputValue: '',
19-
open: false,
20-
options: [],
21-
// 组件内容选中内容
22-
value: [],
23-
};
15+
constructor(props) {
16+
super(props);
17+
this.state = {
18+
// inputValue: '',
19+
open: false,
20+
options: [],
21+
// 组件内容选中内容。
22+
value: [],
23+
};
24+
}
2425

2526
componentDidMount() {
26-
this.setState({
27-
inputValue: this.props.defaultValue
28-
.map((o) => o.label)
29-
.join(this.props.splitInput),
30-
});
27+
// this.setState({
28+
// inputValue: this.props.defaultValue
29+
// .map((o) => o.label)
30+
// .join(this.props.splitInput),
31+
// });
3132
this.setState({ value: this.props.value });
3233
this.initOptions();
3334
}
@@ -73,33 +74,36 @@ class Cascader extends Component {
7374
handleChange = (value, valueObj) => {
7475
const { value: preValue } = this.state;
7576
const { hasSelectAll } = this.props;
76-
this.props.onChange(value, valueObj);
7777
if (!hasSelectAll) {
7878
this.setState({ value });
79+
this.props.onChange(value, valueObj);
7980
return;
8081
}
8182

82-
// 选中全部按钮
83+
// 包含全选,需要处理选中数据。
84+
// 【选中全部按钮】获取最后一次点击的Item,是否和全选值相等,
8385
if (
8486
JSON.stringify(value[value.length - 1]) === JSON.stringify([ALL_VALUE])
8587
) {
8688
this.handelAllChange(true);
8789
return;
8890
}
89-
// 取消全部按钮
91+
// 取消全部按钮】1、上一次代码的value是否包含全选值 2、 本次value是否取消了全选。
9092
if (
9193
preValue?.some(
9294
(x) => JSON.stringify(x) === JSON.stringify([ALL_VALUE]),
93-
) &&
94-
value.every((x) => JSON.stringify(x) !== JSON.stringify([ALL_VALUE]))
95+
)
96+
&& value.every((x) => JSON.stringify(x) !== JSON.stringify([ALL_VALUE]))
9597
) {
9698
this.handelAllChange(false);
9799
return;
98100
}
99-
// // 点击其他多选按钮
101+
// 【点击其他项目】
100102
this.setState({ value: this.getInnerValue(value) });
103+
this.props.onChange(value, valueObj);
101104
};
102105

106+
// 全选项目是否选中, 选中
103107
handelAllChange = (checked) => {
104108
const {
105109
options,
@@ -116,17 +120,18 @@ class Cascader extends Component {
116120
};
117121

118122
getInnerValue = (value) => {
119-
const { hasSelectAll, options } = this.props;
120-
// 不支持全选按钮
121-
if (!hasSelectAll) {
122-
return value;
123-
}
124-
// 点击其他多选按钮
123+
const { options } = this.props;
124+
125+
// value数据结构 [['ALL'], ['pId1','pId11'], ['pId2','pId21'], ['pId3']]
126+
// 获取除全选以外的选中项
125127
const removeAllItem = value.filter(
126128
(item) => JSON.stringify(item) !== JSON.stringify([ALL_VALUE]),
127129
);
130+
131+
// 获取选中的一级节点。
128132
const parentValue = removeAllItem.filter((item) => item.length === 1);
129133

134+
// 一级节点是否全部选中
130135
if (parentValue.length === options.length) {
131136
return [[ALL_VALUE], ...removeAllItem];
132137
}
@@ -139,7 +144,6 @@ class Cascader extends Component {
139144

140145
render() {
141146
const {
142-
splitInput,
143147
multiple,
144148
showSearch,
145149
loadingIcon,
@@ -149,7 +153,7 @@ class Cascader extends Component {
149153
} = this.props;
150154
const iconClasses = this.state.open ? 'open' : 'close';
151155
const checkable = multiple ? (
152-
<div className={`${this.props.prefixCls}-checkbox-inner`}></div>
156+
<div className={`${this.props.prefixCls}-checkbox-inner`} />
153157
) : (
154158
false
155159
);
@@ -189,9 +193,7 @@ class Cascader extends Component {
189193
}
190194
Cascader.defaultProps = {
191195
onChange() {},
192-
maxTagPlaceholder: (text) => {
193-
return text?.length ? `+ ${text.length}` : '';
194-
},
196+
maxTagPlaceholder: (text) => (text?.length ? `+ ${text.length}` : ''),
195197
multiple: false,
196198
allowClear: false,
197199
disabled: false,
@@ -218,6 +220,24 @@ Cascader.propTypes = {
218220
options: PropTypes.array.isRequired,
219221
borderRadiusSize: PropTypes.oneOf(['small', 'default', 'large']),
220222
hasSelectAll: PropTypes.bool,
223+
onChange: PropTypes.func,
224+
maxTagPlaceholder: PropTypes.func,
225+
multiple: PropTypes.bool,
226+
allowClear: PropTypes.bool,
227+
disabled: PropTypes.bool,
228+
transitionName: PropTypes.string,
229+
inputIcon: PropTypes.element,
230+
prefixCls: PropTypes.string,
231+
popupClassName: PropTypes.string,
232+
placement: PropTypes.string,
233+
showArrow: PropTypes.bool,
234+
expandTrigger: PropTypes.string,
235+
fieldNames: PropTypes.object,
236+
expandIcon: PropTypes.element,
237+
clearIcon: PropTypes.element,
238+
loadingIcon: undefined,
239+
removeIcon: PropTypes.element,
240+
splitInput: PropTypes.string,
221241
};
222242

223243
export default Cascader;

0 commit comments

Comments
 (0)