fix: tree-select not update slots

pull/3053/head
tanjinzhou 2020-10-22 11:35:05 +08:00
parent 5643bfef88
commit 4dbed130e2
4 changed files with 62 additions and 23 deletions

View File

@ -12,8 +12,9 @@ import LoadingOutlined from '@ant-design/icons-vue/LoadingOutlined';
import CaretDownOutlined from '@ant-design/icons-vue/CaretDownOutlined'; import CaretDownOutlined from '@ant-design/icons-vue/CaretDownOutlined';
import DownOutlined from '@ant-design/icons-vue/DownOutlined'; import DownOutlined from '@ant-design/icons-vue/DownOutlined';
import CloseOutlined from '@ant-design/icons-vue/CloseOutlined'; import CloseOutlined from '@ant-design/icons-vue/CloseOutlined';
import CloseCircleOutlined from '@ant-design/icons-vue/CloseCircleOutlined'; import CloseCircleFilled from '@ant-design/icons-vue/CloseCircleFilled';
import omit from 'omit.js'; import omit from 'omit.js';
import { convertChildrenToData } from './utils';
const TreeSelect = defineComponent({ const TreeSelect = defineComponent({
TreeNode, TreeNode,
@ -41,7 +42,7 @@ const TreeSelect = defineComponent({
); );
}, },
methods: { methods: {
saveTreeSelect(node) { saveTreeSelect(node: any) {
this.vcTreeSelect = node; this.vcTreeSelect = node;
}, },
focus() { focus() {
@ -51,7 +52,7 @@ const TreeSelect = defineComponent({
blur() { blur() {
this.vcTreeSelect.blur(); this.vcTreeSelect.blur();
}, },
renderSwitcherIcon(prefixCls, { isLeaf, loading }) { renderSwitcherIcon(prefixCls: string, { isLeaf, loading }) {
if (loading) { if (loading) {
return <LoadingOutlined class={`${prefixCls}-switcher-loading-icon`} />; return <LoadingOutlined class={`${prefixCls}-switcher-loading-icon`} />;
} }
@ -60,19 +61,19 @@ const TreeSelect = defineComponent({
} }
return <CaretDownOutlined class={`${prefixCls}-switcher-icon`} />; return <CaretDownOutlined class={`${prefixCls}-switcher-icon`} />;
}, },
handleChange(...args) { handleChange(...args: any[]) {
this.$emit('update:value', args[0]); this.$emit('update:value', args[0]);
this.$emit('change', ...args); this.$emit('change', ...args);
}, },
handleTreeExpand(...args) { handleTreeExpand(...args: any[]) {
this.$emit('update:treeExpandedKeys', args[0]); this.$emit('update:treeExpandedKeys', args[0]);
this.$emit('treeExpand', ...args); this.$emit('treeExpand', ...args);
}, },
handleSearch(...args) { handleSearch(...args: any[]) {
this.$emit('update:searchValue', args[0]); this.$emit('update:searchValue', args[0]);
this.$emit('search', ...args); this.$emit('search', ...args);
}, },
updateTreeData(treeData) { updateTreeData(treeData: any[]) {
const { $slots } = this; const { $slots } = this;
const defaultFields = { const defaultFields = {
children: 'children', children: 'children',
@ -164,9 +165,7 @@ const TreeSelect = defineComponent({
const finalRemoveIcon = removeIcon || <CloseOutlined class={`${prefixCls}-remove-icon`} />; const finalRemoveIcon = removeIcon || <CloseOutlined class={`${prefixCls}-remove-icon`} />;
const finalClearIcon = clearIcon || ( const finalClearIcon = clearIcon || <CloseCircleFilled class={`${prefixCls}-clear-icon`} />;
<CloseCircleOutlined class={`${prefixCls}-clear-icon`} theme="filled" />
);
const VcTreeSelectProps = { const VcTreeSelectProps = {
...this.$attrs, ...this.$attrs,
switcherIcon: nodeProps => this.renderSwitcherIcon(prefixCls, nodeProps), switcherIcon: nodeProps => this.renderSwitcherIcon(prefixCls, nodeProps),
@ -181,13 +180,12 @@ const TreeSelect = defineComponent({
dropdownStyle: { maxHeight: '100vh', overflow: 'auto', ...dropdownStyle }, dropdownStyle: { maxHeight: '100vh', overflow: 'auto', ...dropdownStyle },
treeCheckable: checkable, treeCheckable: checkable,
notFoundContent: notFoundContent || renderEmpty('Select'), notFoundContent: notFoundContent || renderEmpty('Select'),
...(treeData ? { treeData } : {}),
class: cls, class: cls,
onChange: this.handleChange, onChange: this.handleChange,
onSearch: this.handleSearch, onSearch: this.handleSearch,
onTreeExpand: this.handleTreeExpand, onTreeExpand: this.handleTreeExpand,
ref: this.saveTreeSelect, ref: this.saveTreeSelect,
children: getSlot(this), treeData: treeData ? treeData : convertChildrenToData(getSlot(this)),
}; };
return ( return (
<VcTreeSelect <VcTreeSelect
@ -206,4 +204,12 @@ TreeSelect.install = function(app: App) {
return app; return app;
}; };
export default TreeSelect; export default TreeSelect as typeof TreeSelect & {
readonly TreeNode: typeof TreeNode;
readonly SHOW_ALL: typeof SHOW_ALL;
readonly SHOW_PARENT: typeof SHOW_PARENT;
readonly SHOW_CHILD: typeof SHOW_CHILD;
};

View File

@ -5,8 +5,8 @@ import { tuple } from '../_util/type';
export const TreeData = PropTypes.shape({ export const TreeData = PropTypes.shape({
key: PropTypes.string, key: PropTypes.string,
value: PropTypes.string, value: PropTypes.string,
label: PropTypes.any, label: PropTypes.VNodeChild,
scopedSlots: PropTypes.object, slots: PropTypes.object,
children: PropTypes.array, children: PropTypes.array,
}).loose; }).loose;
@ -19,7 +19,7 @@ export const TreeSelectProps = () => ({
labelInValue: PropTypes.looseBool, labelInValue: PropTypes.looseBool,
loadData: PropTypes.func, loadData: PropTypes.func,
maxTagCount: PropTypes.number, maxTagCount: PropTypes.number,
maxTagPlaceholder: PropTypes.any, maxTagPlaceholder: PropTypes.VNodeChild,
value: PropTypes.oneOfType([ value: PropTypes.oneOfType([
PropTypes.string, PropTypes.string,
PropTypes.object, PropTypes.object,
@ -33,11 +33,11 @@ export const TreeSelectProps = () => ({
PropTypes.number, PropTypes.number,
]), ]),
multiple: PropTypes.looseBool, multiple: PropTypes.looseBool,
notFoundContent: PropTypes.any, notFoundContent: PropTypes.VNodeChild,
searchPlaceholder: PropTypes.string, searchPlaceholder: PropTypes.string,
searchValue: PropTypes.string, searchValue: PropTypes.string,
showCheckedStrategy: PropTypes.oneOf(tuple('SHOW_ALL', 'SHOW_PARENT', 'SHOW_CHILD')), showCheckedStrategy: PropTypes.oneOf(tuple('SHOW_ALL', 'SHOW_PARENT', 'SHOW_CHILD')),
suffixIcon: PropTypes.any, suffixIcon: PropTypes.VNodeChild,
treeCheckable: PropTypes.looseBool, treeCheckable: PropTypes.looseBool,
treeCheckStrictly: PropTypes.looseBool, treeCheckStrictly: PropTypes.looseBool,
treeData: PropTypes.arrayOf(Object), treeData: PropTypes.arrayOf(Object),
@ -52,8 +52,8 @@ export const TreeSelectProps = () => ({
treeNodeFilterProp: PropTypes.string, treeNodeFilterProp: PropTypes.string,
treeNodeLabelProp: PropTypes.string, treeNodeLabelProp: PropTypes.string,
replaceFields: PropTypes.object.def({}), replaceFields: PropTypes.object.def({}),
clearIcon: PropTypes.any, clearIcon: PropTypes.VNodeChild,
removeIcon: PropTypes.any, removeIcon: PropTypes.VNodeChild,
onSelect: PropTypes.func, onSelect: PropTypes.func,
onChange: PropTypes.func, onChange: PropTypes.func,

View File

@ -0,0 +1,34 @@
import { flattenChildren, isValidElement } from '../_util/props-util';
export function convertChildrenToData(nodes: any[]): any[] {
return flattenChildren(nodes)
.map(node => {
if (!isValidElement(node) || !node.type) {
return null;
}
const { default: d, ...restSlot } = node.children || {};
const children = d ? d() : [];
const {
key,
props: { value, ...restProps },
} = node;
const data = {
key,
value,
...restProps,
};
Object.keys(restSlot).forEach(p => {
if (typeof restSlot[p] === 'function') {
data[p] = restSlot[p]();
}
});
const childData = convertChildrenToData(children);
if (childData.length) {
data.children = childData;
}
return data;
})
.filter(data => data);
}

View File

@ -245,12 +245,11 @@ const Select = defineComponent({
raf(() => { raf(() => {
const popupNode = findDOMNode(this.popup); const popupNode = findDOMNode(this.popup);
const triggerContainer = findPopupContainer(popupNode, `${prefixCls}-dropdown`); const triggerContainer = findPopupContainer(popupNode, `${prefixCls}-dropdown`);
const searchNode = this.popup.searchRef.current;
if (domNode && triggerContainer && searchNode) { if (domNode && triggerContainer) {
scrollIntoView(domNode, triggerContainer, { scrollIntoView(domNode, triggerContainer, {
onlyScrollIfNeeded: true, onlyScrollIfNeeded: true,
offsetTop: searchNode.offsetHeight, offsetTop: 0,
}); });
} }
}); });