Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(select): 修复t-option方式渲染时内部数组的清除逻辑 #1028

Merged
merged 1 commit into from
Jun 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/select/option.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ export default defineComponent({
tSelect && tSelect.getOptions({ ...context, ...props });
});
onBeforeUnmount(() => {
tSelect && tSelect.hasSlotOptions.value && tSelect.destroyOptions(context);
tSelect && tSelect.hasSlotOptions.value && tSelect.destroyOptions({ ...props });
});

// 处理虚拟滚动节点挂载
Expand Down
39 changes: 9 additions & 30 deletions src/select/select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
onUpdated,
provide,
} from '@vue/composition-api';
import { CreateElement, VNode } from 'vue';
import { CreateElement } from 'vue';
import isFunction from 'lodash/isFunction';
import debounce from 'lodash/debounce';
import get from 'lodash/get';
Expand All @@ -35,6 +35,7 @@ import SelectInput, {
import FakeArrow from '../common-components/fake-arrow';
import Option from './option';
import SelectPanel from './select-panel';
import { parseOptions } from './util';

export type OptionInstance = InstanceType<typeof Option>;

Expand Down Expand Up @@ -258,8 +259,7 @@ export default defineComponent({
});

const debounceOnRemote = debounce(() => {
instance.emit('search', tInputValue.value, context);
// emitEvent<Parameters<TdSelectProps['onSearch']>>(this, 'search', tInputValue.value);
instance.emit('search', tInputValue.value);
}, 300);

watch(
Expand Down Expand Up @@ -489,7 +489,7 @@ export default defineComponent({
const tempValue = Array.isArray(value.value) ? [].concat(value.value) : [];
tempValue.splice(index, 1);
setValue(tempValue, { trigger: 'uncheck', e });
instance.emit('remove', { value: val, data: removeOption[0], e }, context);
instance.emit('remove', { value: val, data: removeOption[0], e });
};
const hideMenu = (context: PopupVisibleChangeContext) => {
visible.value = false;
Expand Down Expand Up @@ -521,7 +521,7 @@ export default defineComponent({
focusing.value = false;
setTInputValue('');
visible.value = false;
instance.emit('clear', { e }, context);
instance.emit('clear', { e });
};
const getOptions = (option: OptionInstance) => {
// create option值不push到options里
Expand Down Expand Up @@ -549,18 +549,18 @@ export default defineComponent({
});
};
const createOption = (value: string) => {
instance.emit('create', value, context);
instance.emit('create', value);
};
const focus = (value: string, context: { e: FocusEvent }) => {
focusing.value = true;
instance.emit('focus', { value, e: context?.e }, context);
instance.emit('focus', { value, e: context?.e });
};
const blur = (value: string, context: { e: FocusEvent | KeyboardEvent }) => {
focusing.value = false;
instance.emit('blur', { value, e: context?.e }, context);
instance.emit('blur', { value, e: context?.e });
};
const enter = (value: string, context: { e: KeyboardEvent }) => {
instance.emit('enter', { value, e: context?.e, inputValue: tInputValue.value }, context);
instance.emit('enter', { value, e: context?.e, inputValue: tInputValue.value });
};
const getOverlayElm = (): HTMLElement => {
let r;
Expand All @@ -586,27 +586,6 @@ export default defineComponent({
isInit.value = true;
hasSlotOptions.value = true;
}

function parseOptions(vnodes: VNode[]): TdOptionProps[] {
if (!vnodes) return [];
return vnodes.reduce((options, vnode) => {
const { componentOptions } = vnode;
if (componentOptions?.tag === 't-option') {
const propsData = componentOptions.propsData as any;
return options.concat({
label: propsData.label,
value: propsData.value,
disabled: propsData.disabled,
content: componentOptions.children ? () => componentOptions.children : propsData.content,
default: propsData.default,
});
}
if (componentOptions?.tag === 't-option-group') {
return options.concat(parseOptions(componentOptions.children));
}
return options;
}, []);
}
};

onMounted(() => {
Expand Down
24 changes: 24 additions & 0 deletions src/select/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { VNode } from 'vue';
import { TdOptionProps } from './type';

// eslint-disable-next-line import/prefer-default-export
export function parseOptions(vnodes: VNode[]): TdOptionProps[] {
if (!vnodes) return [];
return vnodes.reduce((options, vnode) => {
const { componentOptions } = vnode;
if (componentOptions?.tag === 't-option') {
const propsData = componentOptions.propsData as any;
return options.concat({
label: propsData.label,
value: propsData.value,
disabled: propsData.disabled,
content: componentOptions.children ? () => componentOptions.children : propsData.content,
default: propsData.default,
});
}
if (componentOptions?.tag === 't-option-group') {
return options.concat(parseOptions(componentOptions.children));
}
return options;
}, []);
}