u-switch.vue.ts 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import {
  2. ref,
  3. watch,
  4. } from 'vue';
  5. const __sfc__ = defineComponent({
  6. __name: 'u-switch',
  7. props: {
  8. value: {
  9. type: Boolean,
  10. default: false
  11. },
  12. activeText: {
  13. type: String,
  14. default: '已上线'
  15. },
  16. inactiveText: {
  17. type: String,
  18. default: '已下线'
  19. },
  20. activeValue: {
  21. type: [Number,
  22. String,
  23. Boolean],
  24. default: true
  25. },
  26. inactiveValue: {
  27. type: [Number,
  28. String,
  29. Boolean],
  30. default: false
  31. },
  32. },
  33. emits: ['update:value', 'change'],
  34. setup(__props) {
  35. const __ins = getCurrentInstance()!;
  36. const _ctx = __ins.proxy as InstanceType<typeof __sfc__>;
  37. const _cache = __ins.renderCache;
  38. const props = __props;
  39. function emit(event: string, ...do_not_transform_spread: Array<any | null>) {
  40. __ins.emit(event, ...do_not_transform_spread)
  41. }
  42. const isOn = ref(props.value);
  43. // keep `isOn` in sync when prop changes using watchEffect to avoid
  44. // generics/return-type confusion in UTS.
  45. watchEffect(() => {
  46. const v = props.value;
  47. if (v != null) {
  48. isOn.value = v as boolean;
  49. }
  50. });
  51. const toggleSwitch = () => {
  52. isOn.value = !isOn.value;
  53. emit('update:value', isOn.value ? props.activeValue : props.inactiveValue);
  54. emit('change', isOn.value ? props.activeValue : props.inactiveValue);
  55. };
  56. return (): any | null => {
  57. return _cE("view", _uM({
  58. class: "container",
  59. onClick: toggleSwitch
  60. }), [
  61. _cE("view", _uM({
  62. class: _nC(['switch', isOn.value ? 'switch-checked' : 'switch-nochecked'])
  63. }), [
  64. _cE("view", _uM({
  65. class: "switch-handle",
  66. style: _nS(isOn.value ? 'left: 0;' : 'right: 0;')
  67. }), null, 4 /* STYLE */),
  68. isTrue(isOn.value)
  69. ? _cE("text", _uM({ key: 0 }), _tD(_ctx.activeText), 1 /* TEXT */)
  70. : _cE("text", _uM({ key: 1 }), _tD(_ctx.inactiveText), 1 /* TEXT */)
  71. ], 2 /* CLASS */)
  72. ])
  73. }
  74. }
  75. })
  76. export default __sfc__
  77. export type USwitchComponentPublicInstance = InstanceType<typeof __sfc__>;
  78. 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)"]])]])]])]