Switch.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. var __defProp = Object.defineProperty;
  2. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  3. var __getOwnPropNames = Object.getOwnPropertyNames;
  4. var __hasOwnProp = Object.prototype.hasOwnProperty;
  5. var __export = (target, all) => {
  6. for (var name2 in all)
  7. __defProp(target, name2, { get: all[name2], enumerable: true });
  8. };
  9. var __copyProps = (to, from, except, desc) => {
  10. if (from && typeof from === "object" || typeof from === "function") {
  11. for (let key of __getOwnPropNames(from))
  12. if (!__hasOwnProp.call(to, key) && key !== except)
  13. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  14. }
  15. return to;
  16. };
  17. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  18. var stdin_exports = {};
  19. __export(stdin_exports, {
  20. default: () => stdin_default
  21. });
  22. module.exports = __toCommonJS(stdin_exports);
  23. var import_vue = require("vue");
  24. var import_vue2 = require("vue");
  25. var import_utils = require("../utils");
  26. var import_use = require("@vant/use");
  27. var import_loading = require("../loading");
  28. const [name, bem] = (0, import_utils.createNamespace)("switch");
  29. const switchProps = {
  30. size: import_utils.numericProp,
  31. loading: Boolean,
  32. disabled: Boolean,
  33. modelValue: import_utils.unknownProp,
  34. activeColor: String,
  35. inactiveColor: String,
  36. activeValue: {
  37. type: import_utils.unknownProp,
  38. default: true
  39. },
  40. inactiveValue: {
  41. type: import_utils.unknownProp,
  42. default: false
  43. }
  44. };
  45. var stdin_default = (0, import_vue2.defineComponent)({
  46. name,
  47. props: switchProps,
  48. emits: ["change", "update:modelValue"],
  49. setup(props, {
  50. emit,
  51. slots
  52. }) {
  53. const isChecked = () => props.modelValue === props.activeValue;
  54. const onClick = () => {
  55. if (!props.disabled && !props.loading) {
  56. const newValue = isChecked() ? props.inactiveValue : props.activeValue;
  57. emit("update:modelValue", newValue);
  58. emit("change", newValue);
  59. }
  60. };
  61. const renderLoading = () => {
  62. if (props.loading) {
  63. const color = isChecked() ? props.activeColor : props.inactiveColor;
  64. return (0, import_vue.createVNode)(import_loading.Loading, {
  65. "class": bem("loading"),
  66. "color": color
  67. }, null);
  68. }
  69. if (slots.node) {
  70. return slots.node();
  71. }
  72. };
  73. (0, import_use.useCustomFieldValue)(() => props.modelValue);
  74. return () => {
  75. var _a;
  76. const {
  77. size,
  78. loading,
  79. disabled,
  80. activeColor,
  81. inactiveColor
  82. } = props;
  83. const checked = isChecked();
  84. const style = {
  85. fontSize: (0, import_utils.addUnit)(size),
  86. backgroundColor: checked ? activeColor : inactiveColor
  87. };
  88. return (0, import_vue.createVNode)("div", {
  89. "role": "switch",
  90. "class": bem({
  91. on: checked,
  92. loading,
  93. disabled
  94. }),
  95. "style": style,
  96. "tabindex": disabled ? void 0 : 0,
  97. "aria-checked": checked,
  98. "onClick": onClick
  99. }, [(0, import_vue.createVNode)("div", {
  100. "class": bem("node")
  101. }, [renderLoading()]), (_a = slots.background) == null ? void 0 : _a.call(slots)]);
  102. };
  103. }
  104. });