CheckboxGroup.mjs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import { createVNode as _createVNode } from "vue";
  2. import { watch, defineComponent } from "vue";
  3. import { numericProp, createNamespace, makeArrayProp } from "../utils/index.mjs";
  4. import { useChildren, useCustomFieldValue } from "@vant/use";
  5. import { useExpose } from "../composables/use-expose.mjs";
  6. const [name, bem] = createNamespace("checkbox-group");
  7. const checkboxGroupProps = {
  8. max: numericProp,
  9. disabled: Boolean,
  10. iconSize: numericProp,
  11. direction: String,
  12. modelValue: makeArrayProp(),
  13. checkedColor: String
  14. };
  15. const CHECKBOX_GROUP_KEY = Symbol(name);
  16. var stdin_default = defineComponent({
  17. name,
  18. props: checkboxGroupProps,
  19. emits: ["change", "update:modelValue"],
  20. setup(props, {
  21. emit,
  22. slots
  23. }) {
  24. const {
  25. children,
  26. linkChildren
  27. } = useChildren(CHECKBOX_GROUP_KEY);
  28. const updateValue = (value) => emit("update:modelValue", value);
  29. const toggleAll = (options = {}) => {
  30. if (typeof options === "boolean") {
  31. options = {
  32. checked: options
  33. };
  34. }
  35. const {
  36. checked,
  37. skipDisabled
  38. } = options;
  39. const checkedChildren = children.filter((item) => {
  40. if (!item.props.bindGroup) {
  41. return false;
  42. }
  43. if (item.props.disabled && skipDisabled) {
  44. return item.checked.value;
  45. }
  46. return checked != null ? checked : !item.checked.value;
  47. });
  48. const names = checkedChildren.map((item) => item.name);
  49. updateValue(names);
  50. };
  51. watch(() => props.modelValue, (value) => emit("change", value));
  52. useExpose({
  53. toggleAll
  54. });
  55. useCustomFieldValue(() => props.modelValue);
  56. linkChildren({
  57. props,
  58. updateValue
  59. });
  60. return () => {
  61. var _a;
  62. return _createVNode("div", {
  63. "class": bem([props.direction])
  64. }, [(_a = slots.default) == null ? void 0 : _a.call(slots)]);
  65. };
  66. }
  67. });
  68. export {
  69. CHECKBOX_GROUP_KEY,
  70. stdin_default as default
  71. };