vue-admin-core
Version:
A Component Library for Vue 3
202 lines (199 loc) • 6.7 kB
JavaScript
import { defineComponent, h, ref, computed, reactive, toRefs } from 'vue';
import { connect, mapProps, mapReadPretty } from '@formily/vue';
import '../../__builtins__/shared/index.mjs';
import { ElCheckboxButton, ElCheckbox, ElCheckboxGroup, UPDATE_MODEL_EVENT, CHANGE_EVENT } from 'element-plus';
import '../../preview-text/index.mjs';
import '../../../components/config-provider/index.mjs';
import { remove, flattenDeep, isFunction, omit } from 'lodash-es';
import { toJS } from '@formily/reactive';
import { useArrToStr } from '../../__builtins__/hooks/useArrToStr.mjs';
import { resolveComponent } from '../../__builtins__/shared/resolve-component.mjs';
import { transformComponent } from '../../__builtins__/shared/transform-component.mjs';
import { useGlobalConfig } from '../../../components/config-provider/src/hooks/use-global-config.mjs';
import { PreviewText } from '../../preview-text/src/index.mjs';
import { composeExport } from '../../__builtins__/shared/utils.mjs';
const CheckboxOption = defineComponent({
name: "FCheckbox",
inheritAttrs: false,
props: {
option: {
type: Object,
default: null
}
},
setup(curtomProps, { attrs, slots }) {
return () => {
const props = attrs;
const option = curtomProps == null ? void 0 : curtomProps.option;
if (option) {
const children = {
default: () => {
var _a;
return [resolveComponent((_a = slots.default) != null ? _a : option.label, { option })];
}
};
const newProps = { ...props };
Object.assign(newProps, option);
newProps.label = option.value;
delete newProps.value;
return h(
attrs.optionType === "button" ? ElCheckboxButton : ElCheckbox,
{
...newProps
},
children
);
}
return h(
ElCheckbox,
{
...props
},
slots
);
};
}
});
const TransformElCheckboxGroup = transformComponent(ElCheckboxGroup, {
change: "update:modelValue"
});
const CheckboxGroupOption = defineComponent({
name: "FCheckboxGroup",
props: {
...ElCheckboxGroup.props,
modelValue: {
type: [Array, String],
default: () => []
},
options: {
type: Array,
default: () => []
},
optionType: {
type: String,
default: "default"
},
mutualExclusion: {
type: Array,
default: () => []
},
props: {
type: Object,
default: () => ({
children: "children",
label: "label",
value: "value",
disabled: "disabled"
})
}
},
emits: [UPDATE_MODEL_EVENT, CHANGE_EVENT],
setup(customProps, { attrs, slots, emit }) {
const config = useGlobalConfig();
const checkbox = ref();
const label = computed(() => customProps.props.label || "label");
const value = computed(() => customProps.props.value || "value");
const _props = useArrToStr(customProps, { attrs, slots }, {
vm: checkbox,
always: true
});
const mutualExclusionFn = (checked, value2) => {
const _modelValue = toJS(attrs.modelValue);
if (!checked)
return;
const mutualExclusion = customProps.mutualExclusion;
if (mutualExclusion.indexOf("*") !== -1) {
if (mutualExclusion.indexOf(value2) !== -1) {
emit("change", [value2]);
return;
} else {
remove(_modelValue, (val) => mutualExclusion.indexOf(val) !== -1);
emit("change", _modelValue);
return;
}
}
if (flattenDeep(mutualExclusion).indexOf(value2) === -1)
return;
if (checked && Array.isArray(mutualExclusion[0])) {
mutualExclusion.forEach((item) => {
if (item.indexOf(value2) === -1) {
remove(_modelValue, (val) => item.indexOf(val) !== -1 && item.indexOf(value2) === -1);
}
});
} else {
remove(_modelValue, (val) => mutualExclusion.indexOf(val) !== -1 && val !== value2);
}
emit("change", _modelValue);
};
return () => {
const options = customProps.options || [];
const children = options.length !== 0 ? {
default: () => options.map((option) => {
var _a, _b, _c, _d;
if (typeof option === "string") {
return h(
Checkbox,
{
option: {
label: option,
value: option
},
optionType: customProps.optionType
},
(slots == null ? void 0 : slots.option) ? { default: () => {
var _a2;
return (_a2 = slots == null ? void 0 : slots.option) == null ? void 0 : _a2.call(slots, { option });
} } : {}
);
} else {
const {
[label.value]: __label,
[value.value]: __value,
[config.value.dictLabelKey]: _label,
[config.value.dictValueKey]: _value,
...other
} = option;
return h(
Checkbox,
{
...other,
option: {
label: __label || _label,
value: __value || _value
},
optionType: customProps.optionType,
disabled: isFunction((_a = customProps.props) == null ? void 0 : _a.disabled) ? (_b = customProps.props) == null ? void 0 : _b.disabled(option) : ((_c = customProps.props) == null ? void 0 : _c.disabled) ? option[(_d = customProps.props) == null ? void 0 : _d.disabled] : false,
onChange: customProps.mutualExclusion.length > 0 ? (checked) => mutualExclusionFn(checked, __value || _value) : null
},
(slots == null ? void 0 : slots.option) ? { default: () => {
var _a2;
return (_a2 = slots == null ? void 0 : slots.option) == null ? void 0 : _a2.call(slots, { option });
} } : {}
);
}
})
} : slots;
return h(
TransformElCheckboxGroup,
reactive({
...omit(toRefs(customProps), ["options", "props"]),
..._props,
ref: (ref2) => checkbox.value = ref2
}),
children
);
};
}
});
const CheckboxGroup = connect(
CheckboxGroupOption,
mapProps({ dataSource: "options", value: "modelValue" }),
mapReadPretty(PreviewText.Select, {
multiple: true
})
);
const Checkbox = composeExport(connect(CheckboxOption), {
Group: CheckboxGroup
});
export { Checkbox, Checkbox as default };
//# sourceMappingURL=index.mjs.map