|
| 1 | +import { UseCheckboxGroupProps } from "./checkbox.types" |
| 2 | +import { ThemingProps } from "@chakra-ui/styled-system" |
| 3 | +import { |
| 4 | + defineComponent, |
| 5 | + h, |
| 6 | + ComputedRef, |
| 7 | + PropType, |
| 8 | + computed, |
| 9 | + renderSlot, |
| 10 | +} from "vue" |
| 11 | +import { createContext, vueThemingProps } from "@chakra-ui/vue-utils" |
| 12 | +import { ComponentWithProps, DeepPartial } from "@chakra-ui/vue-system" |
| 13 | +import { useCheckboxGroup } from "./use-checkbox-group" |
| 14 | + |
| 15 | +export interface CCheckboxGroupProps |
| 16 | + extends UseCheckboxGroupProps, |
| 17 | + Omit<ThemingProps<"Checkbox">, "orientation"> {} |
| 18 | + |
| 19 | +type CheckboxGroupContext = ComputedRef< |
| 20 | + ThemingProps & { |
| 21 | + isDisabled?: boolean |
| 22 | + value: (string | number)[] |
| 23 | + handleChange(value: number | string, isChecked: boolean): void |
| 24 | + } |
| 25 | +> |
| 26 | + |
| 27 | +const [CheckboxGroupProvider, useCheckboxGroupContext] = |
| 28 | + createContext<CheckboxGroupContext>({ |
| 29 | + strict: false, |
| 30 | + name: "CheckboxGroupContext", |
| 31 | + }) |
| 32 | + |
| 33 | +export const CCheckboxGroup: ComponentWithProps< |
| 34 | + DeepPartial<CCheckboxGroupProps> |
| 35 | +> = defineComponent({ |
| 36 | + name: "CCheckboxGroup", |
| 37 | + props: { |
| 38 | + modelValue: { |
| 39 | + type: Object as PropType<(string | number)[]>, |
| 40 | + // eslint-disable-next-line vue/require-valid-default-prop |
| 41 | + default: () => [], |
| 42 | + }, |
| 43 | + isDisabled: { |
| 44 | + type: Boolean as PropType<boolean>, |
| 45 | + default: false, |
| 46 | + }, |
| 47 | + ...vueThemingProps, |
| 48 | + }, |
| 49 | + emits: ["change", "update:modelValue"], |
| 50 | + setup(props, { emit, slots }) { |
| 51 | + const checkBoxGroupValue = computed<(number | string)[]>({ |
| 52 | + get() { |
| 53 | + return props.modelValue |
| 54 | + }, |
| 55 | + set(value) { |
| 56 | + emit("update:modelValue", value) |
| 57 | + }, |
| 58 | + }) |
| 59 | + |
| 60 | + const { modelValue, handleChange } = useCheckboxGroup({ |
| 61 | + modelValue: checkBoxGroupValue, |
| 62 | + isDisabled: computed(() => props.isDisabled), |
| 63 | + }) |
| 64 | + |
| 65 | + const checkboxGroupContext = computed(() => ({ |
| 66 | + size: props.size, |
| 67 | + variant: props.variant, |
| 68 | + isDisabled: props.isDisabled, |
| 69 | + colorScheme: props.colorScheme, |
| 70 | + value: modelValue.value, |
| 71 | + handleChange, |
| 72 | + })) |
| 73 | + |
| 74 | + CheckboxGroupProvider(checkboxGroupContext) |
| 75 | + return () => renderSlot(slots, "default") |
| 76 | + }, |
| 77 | +}) |
| 78 | + |
| 79 | +export { useCheckboxGroupContext } |
0 commit comments