Skip to content

Commit

Permalink
feat(selection): add clearable.
Browse files Browse the repository at this point in the history
  • Loading branch information
chizuki committed Nov 21, 2022
1 parent f2d2d23 commit 4c214f9
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 9 deletions.
21 changes: 15 additions & 6 deletions packages/hoci/src/components/selection/list.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { syncRef } from "@vueuse/core";
import type { PropType } from "vue";
import { computed, defineComponent, h, provide, reactive, renderSlot, watch } from "vue";
import { computed, defineComponent, h, provide, reactive, renderSlot } from "vue";
import type { ActivateEvent } from "../../types";
import { classPropType, labelPropType, valuePropType } from "../../constants";
import { defineHookComponent, defineHookEmits, defineHookProps } from "../../shared";
Expand All @@ -15,10 +15,16 @@ export const selectionListProps = defineHookProps({
modelValue: {
type: valuePropType
},
/**
* 选中时的 class
*/
activeClass: {
type: classPropType,
default: ""
},
/**
* 每个选项的 class
*/
itemClass: {
type: classPropType,
default: ""
Expand All @@ -38,6 +44,12 @@ export const selectionListProps = defineHookProps({
type: [Boolean, Number],
default: () => false
},
/**
* 可清除
*/
clearable: {
type: Boolean
},
defaultValue: {
type: valuePropType
},
Expand Down Expand Up @@ -86,15 +98,12 @@ export const useSelectionList = defineHookComponent({
},
set(val) {
emit("update:modelValue", val);
emit("change", val);
}
});

syncRef(currentValue, modelValue, { immediate: true, deep: true });

watch(currentValue, val => {
emit("change", val);
}, { deep: true });

provide(
ActiveClassSymbol,
computed(() => props.activeClass)
Expand All @@ -120,7 +129,7 @@ export const useSelectionList = defineHookComponent({

function changeActive(option: any) {
if (isActive(option)) {
if (props.multiple) {
if (props.multiple || props.clearable) {
actives.splice(actives.indexOf(option), 1);
}
} else {
Expand Down
38 changes: 35 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4c214f9

Please sign in to comment.