UNPKG

4.14 kBJavaScriptView Raw
1var __create = Object.create;
2var __defProp = Object.defineProperty;
3var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4var __getOwnPropNames = Object.getOwnPropertyNames;
5var __getProtoOf = Object.getPrototypeOf;
6var __hasOwnProp = Object.prototype.hasOwnProperty;
7var __export = (target, all) => {
8 for (var name2 in all)
9 __defProp(target, name2, { get: all[name2], enumerable: true });
10};
11var __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};
19var __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));
27var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
28var stdin_exports = {};
29__export(stdin_exports, {
30 checkboxProps: () => checkboxProps,
31 default: () => stdin_default
32});
33module.exports = __toCommonJS(stdin_exports);
34var import_vue = require("vue");
35var import_utils = require("../utils");
36var import_CheckboxGroup = require("../checkbox-group/CheckboxGroup");
37var import_use = require("@vant/use");
38var import_use_expose = require("../composables/use-expose");
39var import_Checker = __toESM(require("./Checker"));
40const [name, bem] = (0, import_utils.createNamespace)("checkbox");
41const 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});
49var 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});