@ark-ui/vue
Version:
A collection of unstyled, accessible UI components for Vue, utilizing state machines for seamless interaction.
45 lines (42 loc) • 1.65 kB
JavaScript
import * as ratingGroup from '@zag-js/rating-group';
import { useMachine, normalizeProps } from '@zag-js/vue';
import { useId, computed, toValue } from 'vue';
import { useEnvironmentContext, DEFAULT_ENVIRONMENT } from '../../providers/environment/use-environment-context.js';
import { useLocaleContext, DEFAULT_LOCALE } from '../../providers/locale/use-locale-context.js';
import { cleanProps } from '../../utils/clean-props.js';
import { useFieldContext } from '../field/use-field-context.js';
const useRatingGroup = (props = {}, emit) => {
const id = useId();
const env = useEnvironmentContext(DEFAULT_ENVIRONMENT);
const locale = useLocaleContext(DEFAULT_LOCALE);
const field = useFieldContext();
const context = computed(() => {
const localProps = toValue(props);
return {
id,
ids: {
label: field?.value.ids.label,
hiddenInput: field?.value.ids.control
},
disabled: field?.value.disabled,
readOnly: field?.value.readOnly,
required: field?.value.required,
dir: locale.value.dir,
value: localProps.modelValue,
getRootNode: env?.value.getRootNode,
...cleanProps(localProps),
onValueChange(details) {
emit?.("valueChange", details);
emit?.("update:modelValue", details.value);
localProps.onValueChange?.(details);
},
onHoverChange: (details) => {
emit?.("hoverChange", details);
localProps.onHoverChange?.(details);
}
};
});
const service = useMachine(ratingGroup.machine, context);
return computed(() => ratingGroup.connect(service, normalizeProps));
};
export { useRatingGroup };