index.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. exports.__esModule = true;
  4. exports.default = void 0;
  5. var _utils = require("../utils");
  6. var _shared = require("./shared");
  7. var _field = require("../mixins/field");
  8. var _loading = _interopRequireDefault(require("../loading"));
  9. // Utils
  10. // Mixins
  11. // Components
  12. var _createNamespace = (0, _utils.createNamespace)('switch'),
  13. createComponent = _createNamespace[0],
  14. bem = _createNamespace[1];
  15. var _default = createComponent({
  16. mixins: [_field.FieldMixin],
  17. props: _shared.switchProps,
  18. computed: {
  19. checked: function checked() {
  20. return this.value === this.activeValue;
  21. },
  22. style: function style() {
  23. return {
  24. fontSize: (0, _utils.addUnit)(this.size),
  25. backgroundColor: this.checked ? this.activeColor : this.inactiveColor
  26. };
  27. }
  28. },
  29. methods: {
  30. onClick: function onClick(event) {
  31. this.$emit('click', event);
  32. if (!this.disabled && !this.loading) {
  33. var newValue = this.checked ? this.inactiveValue : this.activeValue;
  34. this.$emit('input', newValue);
  35. this.$emit('change', newValue);
  36. }
  37. },
  38. genLoading: function genLoading() {
  39. var h = this.$createElement;
  40. if (this.loading) {
  41. var color = this.checked ? this.activeColor : this.inactiveColor;
  42. return h(_loading.default, {
  43. "class": bem('loading'),
  44. "attrs": {
  45. "color": color
  46. }
  47. });
  48. }
  49. }
  50. },
  51. render: function render() {
  52. var h = arguments[0];
  53. var checked = this.checked,
  54. loading = this.loading,
  55. disabled = this.disabled;
  56. return h("div", {
  57. "class": bem({
  58. on: checked,
  59. loading: loading,
  60. disabled: disabled
  61. }),
  62. "attrs": {
  63. "role": "switch",
  64. "aria-checked": String(checked)
  65. },
  66. "style": this.style,
  67. "on": {
  68. "click": this.onClick
  69. }
  70. }, [h("div", {
  71. "class": bem('node')
  72. }, [this.genLoading()])]);
  73. }
  74. });
  75. exports.default = _default;