Slider.tsx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import { css } from "@linaria/core";
  2. import { InputNumber, Slider } from "ant-design-vue";
  3. import { defineComponent, reactive, watchEffect } from "vue";
  4. import { bool, number } from "vue-types";
  5. export default defineComponent({
  6. props: {
  7. defaultValue: number().def(0),
  8. disabled: bool().def(false),
  9. value: number(),
  10. min: number(),
  11. max: number(),
  12. step: number(),
  13. },
  14. emits: ["change"],
  15. setup(props, { emit }) {
  16. const state = reactive({
  17. value: props.value,
  18. });
  19. const changeVal = () => {
  20. if (state.value == props.value) return;
  21. emit("change", state.value);
  22. };
  23. watchEffect(() => {
  24. if (props.value != undefined) {
  25. state.value = props.value;
  26. }
  27. });
  28. return () => {
  29. const { defaultValue, disabled, min, max, step } = props;
  30. const attr = {
  31. defaultValue,
  32. disabled: disabled,
  33. value: state.value,
  34. min,
  35. max,
  36. step,
  37. onChange: (value: any) => {
  38. state.value = value;
  39. changeVal();
  40. },
  41. };
  42. return (
  43. <div class={SliderView}>
  44. <Slider
  45. class="item_slider"
  46. tooltipVisible={false}
  47. {...attr}
  48. onAfterChange={() => changeVal()}
  49. />
  50. <InputNumber
  51. class="item_input"
  52. {...attr}
  53. onPressEnter={() => changeVal()}
  54. // onBlur={() => {
  55. // if (state.value !== props.value) changeVal();
  56. // }}
  57. />
  58. </div>
  59. );
  60. };
  61. },
  62. });
  63. const SliderView = css`
  64. flex: 1;
  65. display: flex;
  66. align-items: center;
  67. justify-content: center;
  68. .item_slider {
  69. flex: 1;
  70. }
  71. /* slider style */
  72. .ant-slider-disabled {
  73. opacity: 0.7;
  74. }
  75. .ant-slider-step {
  76. background-color: rgba(255, 255, 255, 0.27);
  77. }
  78. .ant-slider-track {
  79. border-radius: 4px;
  80. background-color: rgba(255, 255, 255, 1);
  81. }
  82. .ant-slider:not(.ant-slider-disabled):hover {
  83. .ant-slider-handle {
  84. background-color: @inf-primary-color;
  85. &:not(.ant-tooltip-open) {
  86. border-color: #fff;
  87. }
  88. }
  89. }
  90. .ant-slider {
  91. &.ant-slider-disabled {
  92. .ant-slider-handle {
  93. background-color: #bbb;
  94. opacity: 0.8;
  95. }
  96. }
  97. }
  98. .ant-slider-handle {
  99. width: 8px;
  100. border-radius: 2px;
  101. border-color: #fff;
  102. background-color: #fff;
  103. }
  104. .ant-slider-handle-click-focused {
  105. border-color: #fff;
  106. background-color: @inf-primary-color;
  107. }
  108. /* input style */
  109. .item_input {
  110. width: 42px;
  111. margin-left: 10px;
  112. border: none;
  113. padding: 2px 0;
  114. text-align: center;
  115. font-size: 12px;
  116. background-color: rgba(252, 254, 255, 0.1);
  117. .ant-input-number-handler-wrap {
  118. display: none;
  119. }
  120. .ant-input-number-input {
  121. height: auto;
  122. padding: 0 2px;
  123. text-align: center;
  124. }
  125. }
  126. `;