Skip to content

Commit ada0ff6

Browse files
committed
feat(Select): add showDataSourceChildren to ignore children fix #1955
1 parent 12d937b commit ada0ff6

File tree

6 files changed

+36
-8
lines changed

6 files changed

+36
-8
lines changed

docs/select/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ const dataSource = [
9595
| isPreview | 是否为预览态 | Boolean | - |
9696
| renderPreview | 预览态模式下渲染的内容<br><br>**签名**:<br>Function(value: number) => void<br>**参数**:<br>_value_: {number} 评分值 | Function | - |
9797
| autoHighlightFirstItem | 自动高亮第一个元素 | Boolean | true |
98+
| showDataSourceChildren | 是否展示 dataSource 中 children | Boolean | - |
9899
| onChange | Select发生改变时触发的回调<br><br>**签名**:<br>Function(value: mixed, actionType: String, item: mixed) => void<br>**参数**:<br>_value_: {mixed} 选中的值<br>_actionType_: {String} 触发的方式, 'itemClick', 'enter', 'tag'<br>_item_: {mixed} 选中的值的对象数据 (useDetailValue=false有效) | Function | - |
99100
| hasBorder | 是否有边框 | Boolean | - |
100101
| hasArrow | 是否有下拉箭头 | Boolean | true |

docs/tab/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ Fusion 提供了三级选项卡,分别用于不同的场景。
3333

3434
| 参数 | 说明 | 类型 | 默认值 |
3535
| ------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------- | -------- |
36-
| activeKey | 被激活的选项卡的 key, 赋值则tab为受控组件, 用户无法切换 | Number/String | - |
3736
| size | 尺寸<br><br>**可选值**:<br>'small', 'medium' | Enum | 'medium' |
37+
| activeKey | 被激活的选项卡的 key, 赋值则tab为受控组件, 用户无法切换 | Number/String | - |
3838
| shape | 外观形态<br><br>**可选值**:<br>'pure', 'wrapped', 'text', 'capsule' | Enum | 'pure' |
3939
| defaultActiveKey | 初始化时被激活的选项卡的 key | Number/String | - |
4040
| animation | 是否开启动效 | Boolean | true |

src/select/base.jsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ export default class Base extends React.Component {
140140
* 自动高亮第一个元素
141141
*/
142142
autoHighlightFirstItem: PropTypes.bool,
143+
showDataSourceChildren: PropTypes.bool,
143144
};
144145

145146
static defaultProps = {
@@ -157,6 +158,7 @@ export default class Base extends React.Component {
157158
},
158159
locale: zhCN.Select,
159160
autoHighlightFirstItem: true,
161+
showDataSourceChildren: true,
160162
};
161163

162164
constructor(props) {
@@ -165,6 +167,7 @@ export default class Base extends React.Component {
165167
this.dataStore = new DataStore({
166168
filter: props.filter,
167169
filterLocal: props.filterLocal,
170+
showDataSourceChildren: props.showDataSourceChildren,
168171
});
169172

170173
this.state = {
@@ -506,7 +509,7 @@ export default class Base extends React.Component {
506509
* @param {Array} dataSource
507510
*/
508511
renderMenuItem(dataSource) {
509-
const { prefix, itemRender } = this.props;
512+
const { prefix, itemRender, showDataSourceChildren } = this.props;
510513
// If it has.
511514
let searchKey;
512515
if (this.isAutoComplete) {
@@ -520,7 +523,7 @@ export default class Base extends React.Component {
520523
if (!item) {
521524
return null;
522525
}
523-
if (Array.isArray(item.children)) {
526+
if (Array.isArray(item.children) && showDataSourceChildren) {
524527
return (
525528
<MenuGroup key={index} label={item.label}>
526529
{this.renderMenuItem(item.children)}

src/select/data-store.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class DataStore {
1616
key: undefined,
1717
addonKey: false,
1818
filterLocal: true,
19+
showDataSourceChildren: true,
1920
...options,
2021
};
2122

@@ -37,7 +38,11 @@ class DataStore {
3738
updateByDS(dataSource, isChildren = false) {
3839
this.dataSource = isChildren
3940
? parseDataSourceFromChildren(dataSource)
40-
: normalizeDataSource(dataSource);
41+
: normalizeDataSource(
42+
dataSource,
43+
0,
44+
this.options.showDataSourceChildren
45+
);
4146
return this.updateAll();
4247
}
4348

@@ -71,15 +76,22 @@ class DataStore {
7176
}
7277

7378
updateAll() {
74-
const { key, filter, filterLocal } = this.options;
79+
const {
80+
key,
81+
filter,
82+
filterLocal,
83+
showDataSourceChildren,
84+
} = this.options;
7585
this.menuDataSource = filterDataSource(
7686
this.dataSource,
7787
filterLocal ? key : '',
7888
filter,
7989
this.options.addonKey
8090
);
8191

82-
this.flattenDataSource = flattingDataSource(this.menuDataSource);
92+
this.flattenDataSource = showDataSourceChildren
93+
? flattingDataSource(this.menuDataSource)
94+
: this.menuDataSource;
8395

8496
this.mapDataSource = {};
8597
this.flattenDataSource.forEach(item => {

src/select/select.jsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,10 @@ class Select extends Base {
171171
* 展开下拉菜单时是否自动焦点到弹层
172172
*/
173173
popupAutoFocus: PropTypes.bool,
174+
/**
175+
* 是否展示 dataSource 中 children
176+
*/
177+
showDataSourceChildren: PropTypes.bool,
174178
};
175179

176180
static defaultProps = {

src/select/util.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,11 @@ export function parseDataSourceFromChildren(children, deep = 0) {
159159
* label priority: label > 'value' > 'index'
160160
* disabled: disabled === true
161161
*/
162-
export function normalizeDataSource(dataSource, deep = 0) {
162+
export function normalizeDataSource(
163+
dataSource,
164+
deep = 0,
165+
showDataSourceChildren = true
166+
) {
163167
const source = [];
164168

165169
dataSource.forEach((item, index) => {
@@ -175,7 +179,11 @@ export function normalizeDataSource(dataSource, deep = 0) {
175179

176180
const item2 = { deep };
177181
// deep < 1: only 2 level allowed
178-
if (Array.isArray(item.children) && deep < 1) {
182+
if (
183+
Array.isArray(item.children) &&
184+
deep < 1 &&
185+
showDataSourceChildren
186+
) {
179187
// handle group
180188
item2.label = item.label || item.value || `Group ${index}`;
181189
// parse children

0 commit comments

Comments
 (0)