checkbox-button.mjs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import { defineComponent, useSlots, inject, computed, openBlock, createElementBlock, normalizeClass, unref, withDirectives, isRef, withModifiers, vModelCheckbox, normalizeStyle, renderSlot, createTextVNode, toDisplayString, createCommentVNode } from 'vue';
  2. import { checkboxGroupContextKey } from './constants.mjs';
  3. import { checkboxProps, checkboxEmits } from './checkbox.mjs';
  4. import _export_sfc from '../../../_virtual/plugin-vue_export-helper.mjs';
  5. import { useCheckbox } from './composables/use-checkbox.mjs';
  6. import { useNamespace } from '../../../hooks/use-namespace/index.mjs';
  7. const __default__ = defineComponent({
  8. name: "ElCheckboxButton"
  9. });
  10. const _sfc_main = /* @__PURE__ */ defineComponent({
  11. ...__default__,
  12. props: checkboxProps,
  13. emits: checkboxEmits,
  14. setup(__props) {
  15. const props = __props;
  16. const slots = useSlots();
  17. const {
  18. isFocused,
  19. isChecked,
  20. isDisabled,
  21. checkboxButtonSize,
  22. model,
  23. actualValue,
  24. handleChange
  25. } = useCheckbox(props, slots);
  26. const checkboxGroup = inject(checkboxGroupContextKey, void 0);
  27. const ns = useNamespace("checkbox");
  28. const activeStyle = computed(() => {
  29. var _a, _b, _c, _d;
  30. const fillValue = (_b = (_a = checkboxGroup == null ? void 0 : checkboxGroup.fill) == null ? void 0 : _a.value) != null ? _b : "";
  31. return {
  32. backgroundColor: fillValue,
  33. borderColor: fillValue,
  34. color: (_d = (_c = checkboxGroup == null ? void 0 : checkboxGroup.textColor) == null ? void 0 : _c.value) != null ? _d : "",
  35. boxShadow: fillValue ? `-1px 0 0 0 ${fillValue}` : void 0
  36. };
  37. });
  38. const labelKls = computed(() => {
  39. return [
  40. ns.b("button"),
  41. ns.bm("button", checkboxButtonSize.value),
  42. ns.is("disabled", isDisabled.value),
  43. ns.is("checked", isChecked.value),
  44. ns.is("focus", isFocused.value)
  45. ];
  46. });
  47. return (_ctx, _cache) => {
  48. var _a, _b, _c, _d;
  49. return openBlock(), createElementBlock("label", {
  50. class: normalizeClass(unref(labelKls))
  51. }, [
  52. _ctx.trueValue || _ctx.falseValue || _ctx.trueLabel || _ctx.falseLabel ? withDirectives((openBlock(), createElementBlock("input", {
  53. key: 0,
  54. "onUpdate:modelValue": ($event) => isRef(model) ? model.value = $event : null,
  55. class: normalizeClass(unref(ns).be("button", "original")),
  56. type: "checkbox",
  57. name: _ctx.name,
  58. tabindex: _ctx.tabindex,
  59. disabled: unref(isDisabled),
  60. "true-value": (_b = (_a = _ctx.trueValue) != null ? _a : _ctx.trueLabel) != null ? _b : true,
  61. "false-value": (_d = (_c = _ctx.falseValue) != null ? _c : _ctx.falseLabel) != null ? _d : false,
  62. onChange: unref(handleChange),
  63. onFocus: ($event) => isFocused.value = true,
  64. onBlur: ($event) => isFocused.value = false,
  65. onClick: withModifiers(() => {
  66. }, ["stop"])
  67. }, null, 42, ["onUpdate:modelValue", "name", "tabindex", "disabled", "true-value", "false-value", "onChange", "onFocus", "onBlur", "onClick"])), [
  68. [vModelCheckbox, unref(model)]
  69. ]) : withDirectives((openBlock(), createElementBlock("input", {
  70. key: 1,
  71. "onUpdate:modelValue": ($event) => isRef(model) ? model.value = $event : null,
  72. class: normalizeClass(unref(ns).be("button", "original")),
  73. type: "checkbox",
  74. name: _ctx.name,
  75. tabindex: _ctx.tabindex,
  76. disabled: unref(isDisabled),
  77. value: unref(actualValue),
  78. onChange: unref(handleChange),
  79. onFocus: ($event) => isFocused.value = true,
  80. onBlur: ($event) => isFocused.value = false,
  81. onClick: withModifiers(() => {
  82. }, ["stop"])
  83. }, null, 42, ["onUpdate:modelValue", "name", "tabindex", "disabled", "value", "onChange", "onFocus", "onBlur", "onClick"])), [
  84. [vModelCheckbox, unref(model)]
  85. ]),
  86. _ctx.$slots.default || _ctx.label ? (openBlock(), createElementBlock("span", {
  87. key: 2,
  88. class: normalizeClass(unref(ns).be("button", "inner")),
  89. style: normalizeStyle(unref(isChecked) ? unref(activeStyle) : void 0)
  90. }, [
  91. renderSlot(_ctx.$slots, "default", {}, () => [
  92. createTextVNode(toDisplayString(_ctx.label), 1)
  93. ])
  94. ], 6)) : createCommentVNode("v-if", true)
  95. ], 2);
  96. };
  97. }
  98. });
  99. var CheckboxButton = /* @__PURE__ */ _export_sfc(_sfc_main, [["__file", "checkbox-button.vue"]]);
  100. export { CheckboxButton as default };
  101. //# sourceMappingURL=checkbox-button.mjs.map