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

feat: CheckboxGroup/Select/List support change component tag name #3322

Merged
merged 10 commits into from
Sep 23, 2024
14 changes: 9 additions & 5 deletions src/checkbox/__tests__/index.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -157,14 +157,18 @@ describe('Checkbox CheckboxGroup', () => {
describe('@event', () => {
it('Event passthrough', async () => {
const wrapper = mount({
components: {
ACheckboxGroup: CheckboxGroup,
ACheckbox: Checkbox,
},
render() {
return (
<CheckboxGroup>
<Checkbox value="gz">广州</Checkbox>
<Checkbox value="sz" disabled>
<a-checkbox-group>
<a-checkbox value="gz">广州</a-checkbox>
<a-checkbox value="sz" disabled>
深圳
</Checkbox>
</CheckboxGroup>
</a-checkbox>
</a-checkbox-group>
);
},
});
Expand Down
4 changes: 3 additions & 1 deletion src/checkbox/group.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import intersection from 'lodash/intersection';
import isObject from 'lodash/isObject';
import isUndefined from 'lodash/isUndefined';
import { VNode } from 'vue';
import { getVNodeComponentName, getVueComponentName } from '../utils/helper';
import Checkbox from './checkbox';
import props from './checkbox-group-props';
import { CheckboxOptionObj, TdCheckboxProps, CheckboxGroupValue } from './type';
Expand Down Expand Up @@ -196,7 +197,8 @@ export default defineComponent({
if (!nodes) return;
for (let i = 0, len = nodes.length; i < len; i++) {
const vNode = nodes[i];
if (vNode.componentOptions && /TCheckbox/.test(vNode.tag)) {
const componentName = getVNodeComponentName(vNode);
if (vNode.componentOptions && componentName && componentName === getVueComponentName(Checkbox)) {
(vNode.componentOptions.propsData as any).storeKey = storeKey;
}
if (vNode.children?.length) {
Expand Down
5 changes: 4 additions & 1 deletion src/list/hooks/useListItem.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { VNode } from 'vue';
import { computed, getCurrentInstance } from '@vue/composition-api';
import { getVNodeComponentName, getVueComponentName } from '../../utils/helper';
import ListItem from '../list-item';

const useListItems = () => {
const instance = getCurrentInstance();
Expand All @@ -8,7 +10,8 @@ const useListItems = () => {
const listItems = computed(() => {
const computedListItems: VNode[] = [];
currentSlots.forEach((child) => {
if (child.componentOptions?.tag === 't-list-item') {
const componentName = getVNodeComponentName(child);
if (componentName && componentName === getVueComponentName(ListItem)) {
computedListItems.push({
class: child.data.staticClass,
style: child.data.staticStyle,
Expand Down
8 changes: 6 additions & 2 deletions src/select/hooks/useSelectOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import {
} from '@vue/composition-api';
import { VNode } from 'vue';
import get from 'lodash/get';
import { getVNodeComponentName, getVueComponentName } from '../../utils/helper';
import Option from '../option';
import OptionGroup from '../optionGroup';
import {
TdSelectProps, TdOptionProps, SelectOptionGroup, SelectValue,
} from '../type';
Expand Down Expand Up @@ -62,7 +65,8 @@ export default function useSelectOptions(
// 处理 slots 中 t-option 与 t-option-group
const currentSlots = instance.proxy.$slots.default || [];
currentSlots.forEach((child) => {
if (child.componentOptions?.tag === 't-option') {
const componentName = getVNodeComponentName(child);
if (componentName && componentName === getVueComponentName(Option)) {
// 独立选项
innerOptions.push({
// 单独处理 style 和 class 参数的透传
Expand All @@ -74,7 +78,7 @@ export default function useSelectOptions(
index: dynamicIndex,
} as TdOptionProps);
dynamicIndex += 1;
} else if (child.componentOptions?.tag === 't-option-group') {
} else if (componentName && componentName === getVueComponentName(OptionGroup)) {
// 分组选项
const groupOption = {
group: (child.componentOptions.propsData as TdOptionProps)?.label,
Expand Down
7 changes: 4 additions & 3 deletions src/tabs/tabs.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Vue, { VNode, VueConstructor } from 'vue';
import kebabCase from 'lodash/kebabCase';
import { getVNodeComponentName, getVueComponentName } from '../utils/helper';
import props from './props';
import TTabPanel from './tab-panel';
import TTabNav from './tab-nav';
Expand Down Expand Up @@ -68,9 +68,10 @@ export default mixins(Vue as VueConstructor<TabParentInjectVue>, classPrefixMixi
return;
}
const newPanels = this.listPanels
.filter((child) => getVNodeComponentName(child) === getVueComponentName(TTabPanel))
.map((panel: VNode) => panel.componentInstance as InstanceType<typeof TTabPanel>)
.filter(Boolean)
.filter((child) => kebabCase(child?.$vnode?.tag).endsWith('t-tab-panel')); // 不可用classPrefix替换 此处是判断组件tag
.filter(Boolean);

const isUnchanged = () => newPanels.length === this.panels.length && this.panels.every((panel, index) => panel === newPanels[index]);
if (isUnchanged() && !force) return;
this.panels = newPanels;
Expand Down
9 changes: 9 additions & 0 deletions src/utils/helper.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import camelCase from 'lodash/camelCase';
import type { VNode } from 'vue';

export function omit(obj: object, fields: string[]): object {
const shallowCopy = {
Expand Down Expand Up @@ -153,3 +154,11 @@ export function setTransform(value: string): object {
'-webkit-transform': value,
};
}

export function getVueComponentName(component: any) {
return component?.options?.name || component?.name;
}

export function getVNodeComponentName(vnode: VNode) {
return (vnode?.componentOptions?.Ctor as any)?.options?.name;
}
Loading