Checkbox.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. var __create = Object.create;
  2. var __defProp = Object.defineProperty;
  3. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  4. var __getOwnPropNames = Object.getOwnPropertyNames;
  5. var __getProtoOf = Object.getPrototypeOf;
  6. var __hasOwnProp = Object.prototype.hasOwnProperty;
  7. var __export = (target, all) => {
  8. for (var name2 in all)
  9. __defProp(target, name2, { get: all[name2], enumerable: true });
  10. };
  11. var __copyProps = (to, from, except, desc) => {
  12. if (from && typeof from === "object" || typeof from === "function") {
  13. for (let key of __getOwnPropNames(from))
  14. if (!__hasOwnProp.call(to, key) && key !== except)
  15. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  16. }
  17. return to;
  18. };
  19. var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  20. // If the importer is in node compatibility mode or this is not an ESM
  21. // file that has been converted to a CommonJS file using a Babel-
  22. // compatible transform (i.e. "__esModule" has not been set), then set
  23. // "default" to the CommonJS "module.exports" for node compatibility.
  24. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  25. mod
  26. ));
  27. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  28. var stdin_exports = {};
  29. __export(stdin_exports, {
  30. checkboxProps: () => checkboxProps,
  31. default: () => stdin_default
  32. });
  33. module.exports = __toCommonJS(stdin_exports);
  34. var import_vue = require("vue");
  35. var import_utils = require("../utils");
  36. var import_CheckboxGroup = require("../checkbox-group/CheckboxGroup");
  37. var import_use = require("@vant/use");
  38. var import_use_expose = require("../composables/use-expose");
  39. var import_Checker = __toESM(require("./Checker"));
  40. const [name, bem] = (0, import_utils.createNamespace)("checkbox");
  41. const checkboxProps = (0, import_utils.extend)({}, import_Checker.checkerProps, {
  42. shape: String,
  43. bindGroup: import_utils.truthProp,
  44. indeterminate: {
  45. type: Boolean,
  46. default: null
  47. }
  48. });
  49. var stdin_default = (0, import_vue.defineComponent)({
  50. name,
  51. props: checkboxProps,
  52. emits: ["change", "update:modelValue"],
  53. setup(props, {
  54. emit,
  55. slots
  56. }) {
  57. const {
  58. parent
  59. } = (0, import_use.useParent)(import_CheckboxGroup.CHECKBOX_GROUP_KEY);
  60. const setParentValue = (checked2) => {
  61. const {
  62. name: name2
  63. } = props;
  64. const {
  65. max,
  66. modelValue
  67. } = parent.props;
  68. const value = modelValue.slice();
  69. if (checked2) {
  70. const overlimit = max && value.length >= +max;
  71. if (!overlimit && !value.includes(name2)) {
  72. value.push(name2);
  73. if (props.bindGroup) {
  74. parent.updateValue(value);
  75. }
  76. }
  77. } else {
  78. const index = value.indexOf(name2);
  79. if (index !== -1) {
  80. value.splice(index, 1);
  81. if (props.bindGroup) {
  82. parent.updateValue(value);
  83. }
  84. }
  85. }
  86. };
  87. const checked = (0, import_vue.computed)(() => {
  88. if (parent && props.bindGroup) {
  89. return parent.props.modelValue.indexOf(props.name) !== -1;
  90. }
  91. return !!props.modelValue;
  92. });
  93. const toggle = (newValue = !checked.value) => {
  94. if (parent && props.bindGroup) {
  95. setParentValue(newValue);
  96. } else {
  97. emit("update:modelValue", newValue);
  98. }
  99. if (props.indeterminate !== null) emit("change", newValue);
  100. };
  101. (0, import_vue.watch)(() => props.modelValue, (value) => {
  102. if (props.indeterminate === null) emit("change", value);
  103. });
  104. (0, import_use_expose.useExpose)({
  105. toggle,
  106. props,
  107. checked
  108. });
  109. (0, import_use.useCustomFieldValue)(() => props.modelValue);
  110. return () => (0, import_vue.createVNode)(import_Checker.default, (0, import_vue.mergeProps)({
  111. "bem": bem,
  112. "role": "checkbox",
  113. "parent": parent,
  114. "checked": checked.value,
  115. "onToggle": toggle
  116. }, props), (0, import_utils.pick)(slots, ["default", "icon"]));
  117. }
  118. });