Skip to content

Commit 34f7c03

Browse files
committed
【级联选择器】完善全选功能的回显案例
1 parent b6af119 commit 34f7c03

4 files changed

Lines changed: 134 additions & 16 deletions

File tree

src/components/c-cascader/demos/customer-render-two.md

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ desc: 全选
88

99
import React, { useState } from 'react';
1010
import { CCascader, Input } from 'cloud-react';
11-
const LABEL_KEY = {
11+
const LABEL_ENUM = {
1212
fj: '贵州 - 黔西南布依族苗族自治州',
1313
fuzhou: '福州',
1414
mawei: '马尾',
@@ -21,6 +21,7 @@ const LABEL_KEY = {
2121
chaoyang: '朝阳区',
2222
haidian: '海淀区'
2323
}
24+
2425
const addressOptions = [{
2526
label: '贵州 - 黔西南布依族苗族自治州',
2627
value: 'fj',
@@ -63,19 +64,11 @@ const addressOptions = [{
6364

6465
export default function Demo() {
6566
const [ value, setValue ] = useState([['zj']]);
66-
const [ inputValue, setInputValue] =useState('');
67-
const maxTagCount = addressOptions.reduce((acc, item) => {
68-
if (item.children && item.children.length){
69-
acc = acc + item.children.length;
70-
} else {
71-
acc += 1;
72-
}
73-
return acc;
74-
}, 0)
67+
const [ inputValue, setInputValue] =useState('浙江');
7568

7669
const onChange = (value, selectedOptions, isSelectedAll) => {
7770
setValue(value);
78-
setInputValue(isSelectedAll ? '全部' : value.map(x => x[x.length - 1]).map(x => LABEL_KEY[x]).join(','));
71+
setInputValue(isSelectedAll ? '全部' : value.map(x => x[x.length - 1]).map(x => LABEL_ENUM[x]).join(','));
7972
}
8073

8174
const filter = (inputValue, path) => {
@@ -93,12 +86,11 @@ export default function Demo() {
9386
value={value}
9487
multiple
9588
allowClear
96-
showSearch={{ filter: filter }}
97-
maxTagCount={1}>
89+
showSearch={{ filter: filter }}>
9890
<Input
9991
placeholder={'请选择'}
10092
value={inputValue}
101-
style={{ width: 170 }}
93+
style={{ width: 170, fontSize: '12px' }}
10294
/>
10395
</CCascader>
10496
</div>
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
---
2+
order: 1
3+
title: 级联选择器
4+
desc: 扩展菜单
5+
---
6+
7+
```jsx
8+
9+
import React, { useState } from 'react';
10+
import { CCascader, Input, Checkbox } from 'cloud-react';
11+
const LABEL_ENUM = {
12+
fj: '贵州 - 黔西南布依族苗族自治州',
13+
fuzhou: '福州',
14+
mawei: '马尾',
15+
mawei1: '马尾1',
16+
quanzhou: '泉州',
17+
zj: '浙江',
18+
hangzhou: '杭州',
19+
yuhang: '余杭',
20+
bj: '北京',
21+
chaoyang: '朝阳区',
22+
haidian: '海淀区'
23+
}
24+
25+
const addressOptions = [{
26+
label: '贵州 - 黔西南布依族苗族自治州',
27+
value: 'fj',
28+
children: [{
29+
label: '福州',
30+
value: 'fuzhou',
31+
children: [{
32+
label: '马尾',
33+
value: 'mawei',
34+
},{
35+
label: '马尾1',
36+
value: 'mawei1',
37+
}],
38+
}, {
39+
label: '泉州',
40+
value: 'quanzhou',
41+
}],
42+
}, {
43+
label: '浙江',
44+
value: 'zj',
45+
children: [{
46+
label: '杭州',
47+
value: 'hangzhou',
48+
children: [{
49+
label: '余杭',
50+
value: 'yuhang',
51+
}],
52+
}],
53+
}, {
54+
label: '北京',
55+
value: 'bj',
56+
children: [{
57+
label: '朝阳区',
58+
value: 'chaoyang',
59+
}, {
60+
label: '海淀区',
61+
value: 'haidian',
62+
}],
63+
}];
64+
65+
export default function Demo() {
66+
const [ checked, setChecked] = useState(false);
67+
const [ value, setValue ] = useState([['zj']]);
68+
const [ inputValue, setInputValue] =useState('浙江');
69+
70+
const onChange = (value, selectedOptions, isSelectedAll) => {
71+
setValue(value);
72+
setChecked(!!isSelectedAll);
73+
setInputValue(isSelectedAll ? '全部' : value.map(x => x[x.length - 1]).map(x => LABEL_ENUM[x]).join(','));
74+
}
75+
76+
const filter = (inputValue, path) => {
77+
return path.some(option => option.label.toLowerCase().indexOf(inputValue.toLowerCase()) > -1);
78+
}
79+
80+
const handleALlChange = value => {
81+
setChecked(value);
82+
if (value) {
83+
setValue([['zj'], ['fj'], ['bj']]);
84+
return;
85+
}
86+
setValue([]);
87+
}
88+
return (
89+
<div>
90+
<div style={{ marginBottom: 24 }}>多选级联组件</div>
91+
<CCascader
92+
options={addressOptions}
93+
onChange={onChange}
94+
placeholder="Please select"
95+
value={value}
96+
multiple
97+
allowClear
98+
showSearch={{ filter: filter }}
99+
dropdownRender={menus => (
100+
<div>
101+
{menus}
102+
<hr />
103+
<Checkbox checked={checked} onChange={handleALlChange} style={{ marginLeft: 12 }}>
104+
全选
105+
</Checkbox>
106+
</div>
107+
)}>
108+
<Input
109+
placeholder={'请选择'}
110+
value={inputValue}
111+
style={{ width: 170, fontSize: '12px' }}
112+
/>
113+
</CCascader>
114+
</div>
115+
);
116+
}
117+
```

src/components/c-cascader/index.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,16 @@ class Cascader extends Component {
3434
}
3535

3636
componentDidUpdate(prevProps) {
37+
const { options, hasSelectAll, value } = this.props;
38+
if (JSON.stringify(prevProps.options) !== JSON.stringify(options)) {
39+
this.initOptions();
40+
}
41+
// hasSelectAll: 全选选项是内部实现提供的。 仅在组件刚挂着的时候赋值,外部取值
3742
if (
38-
JSON.stringify(prevProps.options) !== JSON.stringify(this.props.options)
43+
!hasSelectAll
44+
&& JSON.stringify(prevProps.value) !== JSON.stringify(value)
3945
) {
40-
this.initOptions();
46+
this.setState({ value });
4147
}
4248
}
4349

src/components/c-cascader/index.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,6 @@ group:
8181

8282
### 全选 - 自定义回显格式
8383
<embed src="@components/c-cascader/demos/customer-render-two.md" />
84+
85+
### 自定义级联Footer
86+
<embed src="@components/c-cascader/demos/dropdown-render.md" />

0 commit comments

Comments
 (0)