Skip to content

Commit

Permalink
feat(selection): add render return
Browse files Browse the repository at this point in the history
  • Loading branch information
chizukicn committed Oct 17, 2023
1 parent 726a035 commit f190456
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
14 changes: 3 additions & 11 deletions packages/core/src/selection/component.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { defineComponent, h, renderSlot } from "vue";
import type { HiSelectionSlotData } from "@hoci/core";
import { defineComponent, h } from "vue";
import { selectionEmits, selectionProps, useSelectionList } from "@hoci/core";

export const HiSelection = defineComponent({
Expand All @@ -13,15 +12,8 @@ export const HiSelection = defineComponent({
},
emits: selectionEmits,
setup(props, context) {
const { isActive, changeActive, renderItem } = useSelectionList(props, context);
const { slots } = context;
const { render } = useSelectionList(props, context);

const slotData: HiSelectionSlotData = {
isActive,
changeActive,
renderItem
};

return () => h(props.as, {}, renderSlot(slots, "default", slotData));
return () => h(props.as, {}, render());
}
});
22 changes: 17 additions & 5 deletions packages/core/src/selection/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import {
computed,
inject,
provide,
reactive
reactive,
renderSlot
} from "vue";
import {
classPropType,
Expand Down Expand Up @@ -127,7 +128,7 @@ export function useSelectionContext() {
export const useSelectionList = defineHookComponent({
props: selectionProps,
emits: selectionEmits,
setup(props, { emit }) {
setup(props, { emit, slots }) {
const options = reactive<Option[]>([]);

function toArray(value?: any | any[]): any[] {
Expand Down Expand Up @@ -231,19 +232,30 @@ export const useSelectionList = defineHookComponent({
}));


function renderItem() {
const renderItem = () => {
const children = options
.filter((e) => actives.includes(e.value))
.map((e) => e.render());
return props.multiple ? children : children[0];
}
};

const slotData: HiSelectionSlotData = {
isActive,
changeActive,
renderItem
};

const render = () => {
return renderSlot(slots, "default", slotData);
};

return {
options,
actives,
isActive,
changeActive,
renderItem
renderItem,
render
};
}
});
Expand Down

0 comments on commit f190456

Please sign in to comment.