@ark-ui/vue
Version:
A collection of unstyled, accessible UI components for Vue, utilizing state machines for seamless interaction.
47 lines (46 loc) • 1.87 kB
JavaScript
import { DEFAULT_ENVIRONMENT, useEnvironmentContext } from "../../providers/environment/use-environment-context.js";
import { DEFAULT_LOCALE, useLocaleContext } from "../../providers/locale/use-locale-context.js";
import { cleanProps } from "../../utils/clean-props.js";
import { useCheckboxGroupContext } from "./use-checkbox-group-context.js";
import { useFieldContext } from "../field/use-field-context.js";
import * as checkbox from "@zag-js/checkbox";
import { computed, toValue, useId } from "vue";
import { mergeProps as mergeProps$1, normalizeProps as normalizeProps$1, useMachine } from "@zag-js/vue";
//#region src/components/checkbox/use-checkbox.ts
var useCheckbox = (props = {}, emit) => {
const id = useId();
const env = useEnvironmentContext(DEFAULT_ENVIRONMENT);
const locale = useLocaleContext(DEFAULT_LOCALE);
const field = useFieldContext();
const checkboxGroup = useCheckboxGroupContext();
const computedProps = computed(() => {
const propsValue = toValue(props);
return mergeProps$1(checkboxGroup?.value.getItemProps({ value: propsValue.value }) ?? {}, propsValue);
});
const context = computed(() => {
const localProps = toValue(computedProps);
return {
id,
ids: {
label: field?.value.ids.label,
hiddenInput: field?.value.ids.control
},
disabled: field?.value.disabled,
readOnly: field?.value.readOnly,
invalid: field?.value.invalid,
required: field?.value.required,
dir: locale.value.dir,
getRootNode: env?.value.getRootNode,
...cleanProps(localProps),
onCheckedChange(details) {
emit?.("checkedChange", details);
emit?.("update:checked", details.checked);
localProps.onCheckedChange?.(details);
}
};
});
const service = useMachine(checkbox.machine, context);
return computed(() => checkbox.connect(service, normalizeProps$1));
};
//#endregion
export { useCheckbox };