Skip to content

Commit

Permalink
feat(item): add reject event
Browse files Browse the repository at this point in the history
  • Loading branch information
chizukicn committed Dec 8, 2023
1 parent 5249e5a commit 77a5f18
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 11 deletions.
3 changes: 2 additions & 1 deletion packages/core/src/item/component.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { defineComponent, h } from "vue";
import { itemProps, useSelectionItem } from "@hoci/core";
import { itemEmits, itemProps, useSelectionItem } from "@hoci/core";
import { capitalize } from "tslx";

export const HiItem = defineComponent({
Expand All @@ -11,6 +11,7 @@ export const HiItem = defineComponent({
default: "div"
}
},
emits: itemEmits,
setup(props, context) {
const { render, activate, className, isDisabled, activateEvent } = useSelectionItem(
props,
Expand Down
11 changes: 8 additions & 3 deletions packages/core/src/item/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { ActivateEvent, ElementLike } from "@hoci/shared";
import { defineHookComponent, defineHookProps, valuePropType } from "@hoci/shared";
import { defineHookComponent, defineHookEmits, defineHookProps, valuePropType } from "@hoci/shared";
import type { PropType } from "vue";
import { computed, watch } from "vue";
import { tryOnScopeDispose } from "@vueuse/core";
Expand Down Expand Up @@ -34,17 +34,22 @@ export const itemProps = defineHookProps({
}
});

export const itemEmits = defineHookEmits(["reject"]);


export const useSelectionItem = defineHookComponent({
props: itemProps,
setup(props, { slots }) {
emits: itemEmits,
setup(props, { slots, emit }) {
const context = useSelectionContext();

const activate = () => {
if (props.disabled) {
emit("reject", props.value);
context.reject(props.value);
return;
}
context.changeActive(props.value);
context.activate(props.value);
};

const label = computed(() => {
Expand Down
27 changes: 22 additions & 5 deletions packages/core/src/selection/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ export interface Option {


export interface HiSelectionContext {
activate: (_: any) => void
reject: (_: any) => void
/**
* @deprecated
* use activate instead
*/
changeActive: (_: any) => void
isActive: (_: any) => boolean
init?: InitFunction
Expand Down Expand Up @@ -106,7 +112,8 @@ export const selectionEmits = defineHookEmits([
"update:modelValue",
"change",
"load",
"unload"
"unload",
"reject"
]);

const HiSelectionContextSymbol: InjectionKey<HiSelectionContext> = Symbol("[hi-selection]context");
Expand All @@ -116,7 +123,9 @@ export function useSelectionContext() {
const sharedConfig = useSharedConfig();
return inject(HiSelectionContextSymbol, {
isActive: () => false,
activate: () => {},
changeActive: () => {},
reject: () => {},
activeClass: "active",
unactiveClass: "unactive",
disabledClass: "disabled",
Expand Down Expand Up @@ -174,7 +183,7 @@ export const useSelectionList = defineHookComponent({
return actives.includes(value);
}

function changeActive(value: any) {
function activate(value: any) {
if (isActive(value)) {
if (props.multiple || props.clearable) {
actives.splice(actives.indexOf(value), 1);
Expand All @@ -195,6 +204,12 @@ export const useSelectionList = defineHookComponent({
}
}


function reject(value: any) {
emit("reject", value);
}


const init = (option: Option) => {
function remove() {
const index = options.findIndex((e) => e.id === option.id);
Expand Down Expand Up @@ -228,8 +243,10 @@ export const useSelectionList = defineHookComponent({
defaultValue: computed(() => props.defaultValue),
activateEvent: computed(() => props.activateEvent ?? sharedConfig.activateEvent),
active: currentValue,
changeActive,
activate,
changeActive: activate,
isActive,
reject,
init
}));

Expand All @@ -244,7 +261,7 @@ export const useSelectionList = defineHookComponent({

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

Expand All @@ -256,7 +273,7 @@ export const useSelectionList = defineHookComponent({
options,
actives,
isActive,
changeActive,
changeActive: activate,
renderItem,
render
};
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/switch/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export const switchProps = defineHookProps({

export type HiSwitchProps = typeof switchProps;

export const switchEmits = defineHookEmits(["update:modelValue", "change"]);
export const switchEmits = defineHookEmits(["update:modelValue", "change", "reject"]);

export const useSwitch = defineHookComponent({
props: switchProps,
Expand All @@ -52,6 +52,7 @@ export const useSwitch = defineHookComponent({

const toggle = function (value?: any) {
if (props.disabled) {
context.emit("reject", value);
return;
}
const oldValue = modelValue.value;
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/tab-pane/component.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { defineComponent, h } from "vue";
import { capitalize } from "tslx";
import { itemProps, useSelectionItem } from "../item";
import { itemEmits, itemProps, useSelectionItem } from "../item";

export const HiTabPane = defineComponent({
props: {
...itemProps
},
emits: itemEmits,
setup(props, context) {
const { className, activateEvent, activate, isDisabled, label } = useSelectionItem(props, context);
return () => {
Expand Down

0 comments on commit 77a5f18

Please sign in to comment.