UNPKG

@fecp/mobile

Version:

93 lines (92 loc) 2.78 kB
import { useAttrs, ref, watch, computed, createBlock, openBlock, unref, mergeProps, isRef, withCtx, createElementBlock, toDisplayString } from "vue"; import { MobileField } from "../field/index.mjs"; import { getOptions } from "../../../utils/optionUtil.mjs"; import { MobileRadioGroup } from "../radioGroup/index.mjs"; const _hoisted_1 = { key: 1 }; const _sfc_main = { __name: "FieldRadio", props: { modelValue: { type: [String, Number], default: "" }, options: { type: Array, default: [] }, fieldNames: { type: Object, default: { text: "text", value: "value", disabled: "disabled" } }, readonly: { type: Boolean, default: false } }, emits: ["update:modelValue"], setup(__props, { emit: __emit }) { const props = __props; const attrs = useAttrs(); const finalOptions = ref([]); getOptions({ ...attrs, staticOptions: props.options, optionsFieldNames: props.fieldNames }).then((data) => { finalOptions.value = data; }); watch( () => props.options, () => { getOptions({ ...attrs, staticOptions: props.options, optionsFieldNames: props.fieldNames }).then((data) => { finalOptions.value = data; }); }, { deep: true } ); const emit = __emit; const compValue = computed({ get: () => { return props.modelValue; }, set: (val) => { emit("update:modelValue", val); } }); const fieldTextValue = computed(() => { const selectedOption = finalOptions.value.find( (option) => option.value === compValue.value ); return selectedOption ? selectedOption.text : ""; }); return (_ctx, _cache) => { return openBlock(), createBlock(unref(MobileField), mergeProps(_ctx.$attrs, { readonly: __props.readonly, modelValue: unref(fieldTextValue), "onUpdate:modelValue": _cache[1] || (_cache[1] = ($event) => isRef(fieldTextValue) ? fieldTextValue.value = $event : null) }), { input: withCtx(() => [ !__props.readonly ? (openBlock(), createBlock(unref(MobileRadioGroup), mergeProps({ key: 0 }, _ctx.$attrs, { options: unref(finalOptions), modelValue: unref(compValue), "onUpdate:modelValue": _cache[0] || (_cache[0] = ($event) => isRef(compValue) ? compValue.value = $event : null) }), null, 16, ["options", "modelValue"])) : (openBlock(), createElementBlock("span", _hoisted_1, toDisplayString(unref(fieldTextValue)), 1)) ]), _: 1 }, 16, ["readonly", "modelValue"]); }; } }; export { _sfc_main as default };