Skip to content

Commit

Permalink
feat Checkbox
Browse files Browse the repository at this point in the history
  • Loading branch information
JasKang committed Jan 15, 2024
1 parent 42c0f4e commit ddb123f
Showing 1 changed file with 60 additions and 1 deletion.
61 changes: 60 additions & 1 deletion packages/vue/src/Checkbox/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,64 @@
import { useControllableValue } from '../utils/useControllableValue'
import { defineComponent, type ExtractPublicPropTypes, ref, type SlotsType, type VNode, ComponentPropsOptions } from 'vue'
import {
defineComponent,
type ExtractPublicPropTypes,
ref,
type SlotsType,
type VNode,
ComponentPropsOptions,
provide,
PropType,
computed,
InjectionKey,
ComputedRef,
} from 'vue'

const checkboxGroupProps = {
value: Array as PropType<unknown[]>,
disabled: Boolean,
} satisfies ComponentPropsOptions

export type CheckboxGroupProps = ExtractPublicPropTypes<typeof checkboxGroupProps>

const CheckboxGroupInjectKey: InjectionKey<{
value: ComputedRef<unknown[]>
disabled: ComputedRef<boolean>
insert: (val: unknown) => void
remove: (val: unknown) => void
}> = Symbol('CheckboxGroupInjectKey')

export const CheckboxGroup = defineComponent({
name: 'ZCheckboxGroup',
props: checkboxGroupProps,
emits: {
'update:value': (value: unknown[]) => true,
change: (value: any[]) => true,
},
setup(props, { emit, slots }) {
const [state, setState] = useControllableValue<unknown[]>(props, {
defaultValue: [],
onChange: (val: any[]) => {
emit('change', val)
},
})
provide(CheckboxGroupInjectKey, {
value: state,
disabled: computed(() => props.disabled),
insert: (val: unknown) => {
if (state.value.indexOf(val) === -1) {
state.value.push(val)
}
},
remove: (val: unknown) => {
const index = state.value.indexOf(val)
if (index !== -1) {
state.value.splice(index, 1)
}
},
})
return () => <div class="flex flex-col space-y-2">{slots.default?.()}</div>
},
})

const props = {
value: null,
Expand Down

1 comment on commit ddb123f

@vercel
Copy link

@vercel vercel bot commented on ddb123f Jan 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

tailv – ./

tailv-git-main-jaskang.vercel.app
elenext.vercel.app
tailv-jaskang.vercel.app
elenext.jaskang.vip

Please sign in to comment.