element-plus
Version:
A Component Library for Vue 3
88 lines (85 loc) • 3.53 kB
JavaScript
import { defineComponent, ref, onMounted, computed, provide, reactive, toRefs, watch, openBlock, createElementBlock, unref, normalizeClass, renderSlot, Fragment, renderList, createBlock, mergeProps, nextTick } from 'vue';
import { radioGroupProps, radioGroupEmits, radioDefaultProps } from './radio-group.mjs';
import { radioGroupKey } from './constants.mjs';
import { isEqual } from 'lodash-unified';
import Radio from './radio2.mjs';
import _export_sfc from '../../../_virtual/plugin-vue_export-helper.mjs';
import { useNamespace } from '../../../hooks/use-namespace/index.mjs';
import { useId } from '../../../hooks/use-id/index.mjs';
import { useFormItem, useFormItemInputId } from '../../form/src/hooks/use-form-item.mjs';
import { debugWarn } from '../../../utils/error.mjs';
import { UPDATE_MODEL_EVENT, CHANGE_EVENT } from '../../../constants/event.mjs';
const __default__ = defineComponent({
name: "ElRadioGroup"
});
const _sfc_main = /* @__PURE__ */ defineComponent({
...__default__,
props: radioGroupProps,
emits: radioGroupEmits,
setup(__props, { emit }) {
const props = __props;
const ns = useNamespace("radio");
const radioId = useId();
const radioGroupRef = ref();
const { formItem } = useFormItem();
const { inputId: groupId, isLabeledByFormItem } = useFormItemInputId(props, {
formItemContext: formItem
});
const changeEvent = (value) => {
emit(UPDATE_MODEL_EVENT, value);
nextTick(() => emit(CHANGE_EVENT, value));
};
onMounted(() => {
const radios = radioGroupRef.value.querySelectorAll("[type=radio]");
const firstLabel = radios[0];
if (!Array.from(radios).some((radio) => radio.checked) && firstLabel) {
firstLabel.tabIndex = 0;
}
});
const name = computed(() => {
return props.name || radioId.value;
});
const aliasProps = computed(() => ({
...radioDefaultProps,
...props.props
}));
const getOptionProps = (option) => {
const base = {
label: option[aliasProps.value.label],
value: option[aliasProps.value.value],
disabled: option[aliasProps.value.disabled]
};
return { ...option, ...base };
};
provide(radioGroupKey, reactive({
...toRefs(props),
changeEvent,
name
}));
watch(() => props.modelValue, (newVal, oldValue) => {
if (props.validateEvent && !isEqual(newVal, oldValue)) {
formItem == null ? void 0 : formItem.validate("change").catch((err) => debugWarn());
}
});
return (_ctx, _cache) => {
return openBlock(), createElementBlock("div", {
id: unref(groupId),
ref_key: "radioGroupRef",
ref: radioGroupRef,
class: normalizeClass(unref(ns).b("group")),
role: "radiogroup",
"aria-label": !unref(isLabeledByFormItem) ? _ctx.ariaLabel || "radio-group" : void 0,
"aria-labelledby": unref(isLabeledByFormItem) ? unref(formItem).labelId : void 0
}, [
renderSlot(_ctx.$slots, "default", {}, () => [
(openBlock(true), createElementBlock(Fragment, null, renderList(props.options, (item, index) => {
return openBlock(), createBlock(Radio, mergeProps({ key: index }, getOptionProps(item)), null, 16);
}), 128))
])
], 10, ["id", "aria-label", "aria-labelledby"]);
};
}
});
var RadioGroup = /* @__PURE__ */ _export_sfc(_sfc_main, [["__file", "radio-group.vue"]]);
export { RadioGroup as default };
//# sourceMappingURL=radio-group2.mjs.map