AddressList.mjs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import { defineComponent, computed, createVNode as _createVNode } from "vue";
  2. import { truthProp, numericProp, makeArrayProp, createNamespace, makeStringProp } from "../utils/index.mjs";
  3. import { Button } from "../button/index.mjs";
  4. import { RadioGroup } from "../radio-group/index.mjs";
  5. import { CheckboxGroup } from "../checkbox-group/index.mjs";
  6. import AddressListItem from "./AddressListItem.mjs";
  7. const [name, bem, t] = createNamespace("address-list");
  8. const addressListProps = {
  9. list: makeArrayProp(),
  10. modelValue: [...numericProp, Array],
  11. switchable: truthProp,
  12. disabledText: String,
  13. disabledList: makeArrayProp(),
  14. showAddButton: truthProp,
  15. addButtonText: String,
  16. defaultTagText: String,
  17. rightIcon: makeStringProp("edit")
  18. };
  19. var stdin_default = defineComponent({
  20. name,
  21. props: addressListProps,
  22. emits: ["add", "edit", "select", "clickItem", "editDisabled", "selectDisabled", "update:modelValue"],
  23. setup(props, {
  24. slots,
  25. emit
  26. }) {
  27. const singleChoice = computed(() => !Array.isArray(props.modelValue));
  28. const renderItem = (item, index, disabled) => {
  29. const onEdit = () => emit(disabled ? "editDisabled" : "edit", item, index);
  30. const onClick = (event) => emit("clickItem", item, index, {
  31. event
  32. });
  33. const onSelect = () => {
  34. emit(disabled ? "selectDisabled" : "select", item, index);
  35. if (!disabled) {
  36. if (singleChoice.value) {
  37. emit("update:modelValue", item.id);
  38. } else {
  39. const value = props.modelValue;
  40. if (value.includes(item.id)) {
  41. emit("update:modelValue", value.filter((id) => id !== item.id));
  42. } else {
  43. emit("update:modelValue", [...value, item.id]);
  44. }
  45. }
  46. }
  47. };
  48. return _createVNode(AddressListItem, {
  49. "key": item.id,
  50. "address": item,
  51. "disabled": disabled,
  52. "switchable": props.switchable,
  53. "singleChoice": singleChoice.value,
  54. "defaultTagText": props.defaultTagText,
  55. "rightIcon": props.rightIcon,
  56. "onEdit": onEdit,
  57. "onClick": onClick,
  58. "onSelect": onSelect
  59. }, {
  60. bottom: slots["item-bottom"],
  61. tag: slots.tag
  62. });
  63. };
  64. const renderList = (list, disabled) => {
  65. if (list) {
  66. return list.map((item, index) => renderItem(item, index, disabled));
  67. }
  68. };
  69. const renderBottom = () => props.showAddButton ? _createVNode("div", {
  70. "class": [bem("bottom"), "van-safe-area-bottom"]
  71. }, [_createVNode(Button, {
  72. "round": true,
  73. "block": true,
  74. "type": "primary",
  75. "text": props.addButtonText || t("add"),
  76. "class": bem("add"),
  77. "onClick": () => emit("add")
  78. }, null)]) : void 0;
  79. return () => {
  80. var _a, _b;
  81. const List = renderList(props.list);
  82. const DisabledList = renderList(props.disabledList, true);
  83. const DisabledText = props.disabledText && _createVNode("div", {
  84. "class": bem("disabled-text")
  85. }, [props.disabledText]);
  86. return _createVNode("div", {
  87. "class": bem()
  88. }, [(_a = slots.top) == null ? void 0 : _a.call(slots), !singleChoice.value && Array.isArray(props.modelValue) ? _createVNode(CheckboxGroup, {
  89. "modelValue": props.modelValue
  90. }, {
  91. default: () => [List]
  92. }) : _createVNode(RadioGroup, {
  93. "modelValue": props.modelValue
  94. }, {
  95. default: () => [List]
  96. }), DisabledText, DisabledList, (_b = slots.default) == null ? void 0 : _b.call(slots), renderBottom()]);
  97. };
  98. }
  99. });
  100. export {
  101. addressListProps,
  102. stdin_default as default
  103. };