Skip to content

Commit 4dbed13

Browse files
committed
fix: tree-select not update slots
1 parent 5643bfe commit 4dbed13

File tree

4 files changed

+62
-23
lines changed

4 files changed

+62
-23
lines changed

components/tree-select/index.tsx

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ import LoadingOutlined from '@ant-design/icons-vue/LoadingOutlined';
1212
import CaretDownOutlined from '@ant-design/icons-vue/CaretDownOutlined';
1313
import DownOutlined from '@ant-design/icons-vue/DownOutlined';
1414
import CloseOutlined from '@ant-design/icons-vue/CloseOutlined';
15-
import CloseCircleOutlined from '@ant-design/icons-vue/CloseCircleOutlined';
15+
import CloseCircleFilled from '@ant-design/icons-vue/CloseCircleFilled';
1616
import omit from 'omit.js';
17+
import { convertChildrenToData } from './utils';
1718

1819
const TreeSelect = defineComponent({
1920
TreeNode,
@@ -41,7 +42,7 @@ const TreeSelect = defineComponent({
4142
);
4243
},
4344
methods: {
44-
saveTreeSelect(node) {
45+
saveTreeSelect(node: any) {
4546
this.vcTreeSelect = node;
4647
},
4748
focus() {
@@ -51,7 +52,7 @@ const TreeSelect = defineComponent({
5152
blur() {
5253
this.vcTreeSelect.blur();
5354
},
54-
renderSwitcherIcon(prefixCls, { isLeaf, loading }) {
55+
renderSwitcherIcon(prefixCls: string, { isLeaf, loading }) {
5556
if (loading) {
5657
return <LoadingOutlined class={`${prefixCls}-switcher-loading-icon`} />;
5758
}
@@ -60,19 +61,19 @@ const TreeSelect = defineComponent({
6061
}
6162
return <CaretDownOutlined class={`${prefixCls}-switcher-icon`} />;
6263
},
63-
handleChange(...args) {
64+
handleChange(...args: any[]) {
6465
this.$emit('update:value', args[0]);
6566
this.$emit('change', ...args);
6667
},
67-
handleTreeExpand(...args) {
68+
handleTreeExpand(...args: any[]) {
6869
this.$emit('update:treeExpandedKeys', args[0]);
6970
this.$emit('treeExpand', ...args);
7071
},
71-
handleSearch(...args) {
72+
handleSearch(...args: any[]) {
7273
this.$emit('update:searchValue', args[0]);
7374
this.$emit('search', ...args);
7475
},
75-
updateTreeData(treeData) {
76+
updateTreeData(treeData: any[]) {
7677
const { $slots } = this;
7778
const defaultFields = {
7879
children: 'children',
@@ -164,9 +165,7 @@ const TreeSelect = defineComponent({
164165

165166
const finalRemoveIcon = removeIcon || <CloseOutlined class={`${prefixCls}-remove-icon`} />;
166167

167-
const finalClearIcon = clearIcon || (
168-
<CloseCircleOutlined class={`${prefixCls}-clear-icon`} theme="filled" />
169-
);
168+
const finalClearIcon = clearIcon || <CloseCircleFilled class={`${prefixCls}-clear-icon`} />;
170169
const VcTreeSelectProps = {
171170
...this.$attrs,
172171
switcherIcon: nodeProps => this.renderSwitcherIcon(prefixCls, nodeProps),
@@ -181,13 +180,12 @@ const TreeSelect = defineComponent({
181180
dropdownStyle: { maxHeight: '100vh', overflow: 'auto', ...dropdownStyle },
182181
treeCheckable: checkable,
183182
notFoundContent: notFoundContent || renderEmpty('Select'),
184-
...(treeData ? { treeData } : {}),
185183
class: cls,
186184
onChange: this.handleChange,
187185
onSearch: this.handleSearch,
188186
onTreeExpand: this.handleTreeExpand,
189187
ref: this.saveTreeSelect,
190-
children: getSlot(this),
188+
treeData: treeData ? treeData : convertChildrenToData(getSlot(this)),
191189
};
192190
return (
193191
<VcTreeSelect
@@ -206,4 +204,12 @@ TreeSelect.install = function(app: App) {
206204
return app;
207205
};
208206

209-
export default TreeSelect;
207+
export default TreeSelect as typeof TreeSelect & {
208+
readonly TreeNode: typeof TreeNode;
209+
210+
readonly SHOW_ALL: typeof SHOW_ALL;
211+
212+
readonly SHOW_PARENT: typeof SHOW_PARENT;
213+
214+
readonly SHOW_CHILD: typeof SHOW_CHILD;
215+
};

components/tree-select/interface.jsx renamed to components/tree-select/interface.tsx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import { tuple } from '../_util/type';
55
export const TreeData = PropTypes.shape({
66
key: PropTypes.string,
77
value: PropTypes.string,
8-
label: PropTypes.any,
9-
scopedSlots: PropTypes.object,
8+
label: PropTypes.VNodeChild,
9+
slots: PropTypes.object,
1010
children: PropTypes.array,
1111
}).loose;
1212

@@ -19,7 +19,7 @@ export const TreeSelectProps = () => ({
1919
labelInValue: PropTypes.looseBool,
2020
loadData: PropTypes.func,
2121
maxTagCount: PropTypes.number,
22-
maxTagPlaceholder: PropTypes.any,
22+
maxTagPlaceholder: PropTypes.VNodeChild,
2323
value: PropTypes.oneOfType([
2424
PropTypes.string,
2525
PropTypes.object,
@@ -33,11 +33,11 @@ export const TreeSelectProps = () => ({
3333
PropTypes.number,
3434
]),
3535
multiple: PropTypes.looseBool,
36-
notFoundContent: PropTypes.any,
36+
notFoundContent: PropTypes.VNodeChild,
3737
searchPlaceholder: PropTypes.string,
3838
searchValue: PropTypes.string,
3939
showCheckedStrategy: PropTypes.oneOf(tuple('SHOW_ALL', 'SHOW_PARENT', 'SHOW_CHILD')),
40-
suffixIcon: PropTypes.any,
40+
suffixIcon: PropTypes.VNodeChild,
4141
treeCheckable: PropTypes.looseBool,
4242
treeCheckStrictly: PropTypes.looseBool,
4343
treeData: PropTypes.arrayOf(Object),
@@ -52,8 +52,8 @@ export const TreeSelectProps = () => ({
5252
treeNodeFilterProp: PropTypes.string,
5353
treeNodeLabelProp: PropTypes.string,
5454
replaceFields: PropTypes.object.def({}),
55-
clearIcon: PropTypes.any,
56-
removeIcon: PropTypes.any,
55+
clearIcon: PropTypes.VNodeChild,
56+
removeIcon: PropTypes.VNodeChild,
5757

5858
onSelect: PropTypes.func,
5959
onChange: PropTypes.func,

components/tree-select/utils.tsx

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { flattenChildren, isValidElement } from '../_util/props-util';
2+
3+
export function convertChildrenToData(nodes: any[]): any[] {
4+
return flattenChildren(nodes)
5+
.map(node => {
6+
if (!isValidElement(node) || !node.type) {
7+
return null;
8+
}
9+
const { default: d, ...restSlot } = node.children || {};
10+
const children = d ? d() : [];
11+
const {
12+
key,
13+
props: { value, ...restProps },
14+
} = node;
15+
16+
const data = {
17+
key,
18+
value,
19+
...restProps,
20+
};
21+
Object.keys(restSlot).forEach(p => {
22+
if (typeof restSlot[p] === 'function') {
23+
data[p] = restSlot[p]();
24+
}
25+
});
26+
const childData = convertChildrenToData(children);
27+
if (childData.length) {
28+
data.children = childData;
29+
}
30+
31+
return data;
32+
})
33+
.filter(data => data);
34+
}

components/vc-tree-select/src/Select.jsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -245,12 +245,11 @@ const Select = defineComponent({
245245
raf(() => {
246246
const popupNode = findDOMNode(this.popup);
247247
const triggerContainer = findPopupContainer(popupNode, `${prefixCls}-dropdown`);
248-
const searchNode = this.popup.searchRef.current;
249248

250-
if (domNode && triggerContainer && searchNode) {
249+
if (domNode && triggerContainer) {
251250
scrollIntoView(domNode, triggerContainer, {
252251
onlyScrollIfNeeded: true,
253-
offsetTop: searchNode.offsetHeight,
252+
offsetTop: 0,
254253
});
255254
}
256255
});

0 commit comments

Comments
 (0)