u-switch.vue 4.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import { ref, watch, } from 'vue';
  2. const __sfc__ = defineComponent({
  3. __name: 'u-switch',
  4. props: {
  5. value: {
  6. type: Boolean,
  7. default: false
  8. },
  9. activeText: {
  10. type: String,
  11. default: '已上线'
  12. },
  13. inactiveText: {
  14. type: String,
  15. default: '已下线'
  16. },
  17. activeValue: {
  18. type: [Number,
  19. String,
  20. Boolean],
  21. default: true
  22. },
  23. inactiveValue: {
  24. type: [Number,
  25. String,
  26. Boolean],
  27. default: false
  28. },
  29. },
  30. emits: ['update:value', 'change'],
  31. setup(__props) {
  32. const __ins = getCurrentInstance()!;
  33. const _ctx = __ins.proxy as InstanceType<typeof __sfc__>;
  34. const _cache = __ins.renderCache;
  35. const props = __props;
  36. function emit(event: string, ...do_not_transform_spread: Array<any | null>) {
  37. __ins.emit(event, ...do_not_transform_spread);
  38. }
  39. const isOn = ref(props.value);
  40. // keep `isOn` in sync when prop changes using watchEffect to avoid
  41. // generics/return-type confusion in UTS.
  42. watchEffect(() => {
  43. const v = props.value;
  44. if (v != null) {
  45. isOn.value = v as boolean;
  46. }
  47. });
  48. const toggleSwitch = () => {
  49. isOn.value = !isOn.value;
  50. emit('update:value', isOn.value ? props.activeValue : props.inactiveValue);
  51. emit('change', isOn.value ? props.activeValue : props.inactiveValue);
  52. };
  53. return (): any | null => {
  54. return _cE("view", _uM({
  55. class: "container",
  56. onClick: toggleSwitch
  57. }), [
  58. _cE("view", _uM({
  59. class: _nC(['switch', isOn.value ? 'switch-checked' : 'switch-nochecked'])
  60. }), [
  61. _cE("view", _uM({
  62. class: "switch-handle",
  63. style: _nS(isOn.value ? 'left: 0;' : 'right: 0;')
  64. }), null, 4 /* STYLE */),
  65. isTrue(isOn.value)
  66. ? _cE("text", _uM({ key: 0 }), _tD(_ctx.activeText), 1 /* TEXT */)
  67. : _cE("text", _uM({ key: 1 }), _tD(_ctx.inactiveText), 1 /* TEXT */)
  68. ], 2 /* CLASS */)
  69. ]);
  70. };
  71. }
  72. });
  73. export default __sfc__;
  74. export type USwitchComponentPublicInstance = InstanceType<typeof __sfc__>;
  75. const GenComponentsUSwitchUSwitchStyles = [_uM([["container", _pS(_uM([["width", "156rpx"]]))], ["switch", _pS(_uM([["position", "relative"], ["width", "156rpx"], ["height", "50rpx"], ["borderTopLeftRadius", "40rpx"], ["borderTopRightRadius", "40rpx"], ["borderBottomRightRadius", "40rpx"], ["borderBottomLeftRadius", "40rpx"], ["backgroundColor", "#d5d5d5"], ["transitionProperty", "backgroundColor"], ["transitionDuration", "0.3s"], ["transitionTimingFunction", "ease"], ["textAlign", "center"]]))], ["switch-handle", _uM([["", _uM([["position", "absolute"], ["top", "3rpx"], ["width", "44rpx"], ["height", "44rpx"], ["borderTopLeftRadius", "22rpx"], ["borderTopRightRadius", "22rpx"], ["borderBottomRightRadius", "22rpx"], ["borderBottomLeftRadius", "22rpx"], ["backgroundImage", "none"], ["backgroundColor", "#ffffff"], ["borderTopWidth", "2rpx"], ["borderRightWidth", "2rpx"], ["borderBottomWidth", "2rpx"], ["borderLeftWidth", "2rpx"], ["borderTopStyle", "solid"], ["borderRightStyle", "solid"], ["borderBottomStyle", "solid"], ["borderLeftStyle", "solid"], ["borderTopColor", "#e0e0e0"], ["borderRightColor", "#e0e0e0"], ["borderBottomColor", "#e0e0e0"], ["borderLeftColor", "#e0e0e0"], ["boxShadow", "0 2rpx 8rpx rgba(0, 0, 0, 0.12)"], ["transitionProperty", "all"], ["transitionDuration", "0.3s"], ["transitionTimingFunction", "cubic-bezier(0.4,0,0.2,1)"]])], [".switch-checked ", _uM([["left", "109rpx"]])]])], ["switch-checked", _pS(_uM([["backgroundColor", "#ffda59"]]))], ["switch-nochecked", _pS(_uM([["backgroundColor", "#d5d5d5"]]))], ["label", _pS(_uM([["position", "absolute"], ["width", "156rpx"], ["height", "50rpx"], ["lineHeight", "50rpx"], ["textAlign", "center"], ["color", "#ffffff"], ["fontSize", "28rpx"], ["opacity", 0.7]]))], ["@TRANSITION", _uM([["switch", _uM([["property", "backgroundColor"], ["duration", "0.3s"], ["timingFunction", "ease"]])], ["switch-handle", _uM([["property", "all"], ["duration", "0.3s"], ["timingFunction", "cubic-bezier(0.4,0,0.2,1)"]])]])]])];
  76. //# sourceMappingURL=u-switch.vue.map