123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- import { createVNode as _createVNode, mergeProps as _mergeProps } from "vue";
- import { watch, computed, defineComponent } from "vue";
- import { createNamespace, extend, pick, truthProp } from "../utils/index.mjs";
- import { CHECKBOX_GROUP_KEY } from "../checkbox-group/CheckboxGroup.mjs";
- import { useParent, useCustomFieldValue } from "@vant/use";
- import { useExpose } from "../composables/use-expose.mjs";
- import Checker, { checkerProps } from "./Checker.mjs";
- const [name, bem] = createNamespace("checkbox");
- const checkboxProps = extend({}, checkerProps, {
- bindGroup: truthProp
- });
- var stdin_default = defineComponent({
- name,
- props: checkboxProps,
- emits: ["change", "update:modelValue"],
- setup(props, {
- emit,
- slots
- }) {
- const {
- parent
- } = useParent(CHECKBOX_GROUP_KEY);
- const setParentValue = (checked2) => {
- const {
- name: name2
- } = props;
- const {
- max,
- modelValue
- } = parent.props;
- const value = modelValue.slice();
- if (checked2) {
- const overlimit = max && value.length >= max;
- if (!overlimit && !value.includes(name2)) {
- value.push(name2);
- if (props.bindGroup) {
- parent.updateValue(value);
- }
- }
- } else {
- const index = value.indexOf(name2);
- if (index !== -1) {
- value.splice(index, 1);
- if (props.bindGroup) {
- parent.updateValue(value);
- }
- }
- }
- };
- const checked = computed(() => {
- if (parent && props.bindGroup) {
- return parent.props.modelValue.indexOf(props.name) !== -1;
- }
- return !!props.modelValue;
- });
- const toggle = (newValue = !checked.value) => {
- if (parent && props.bindGroup) {
- setParentValue(newValue);
- } else {
- emit("update:modelValue", newValue);
- }
- };
- watch(() => props.modelValue, (value) => emit("change", value));
- useExpose({
- toggle,
- props,
- checked
- });
- useCustomFieldValue(() => props.modelValue);
- return () => _createVNode(Checker, _mergeProps({
- "bem": bem,
- "role": "checkbox",
- "parent": parent,
- "checked": checked.value,
- "onToggle": toggle
- }, props), pick(slots, ["default", "icon"]));
- }
- });
- export {
- stdin_default as default
- };
|