@v-c/slider 1.0.1 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -36,8 +36,6 @@ const Track = defineComponent({
36
36
  }
37
37
 
38
38
  // ============================ Render ============================
39
- const positionStyle: CSSProperties = {}
40
-
41
39
  return () => {
42
40
  const { prefixCls, index, onStartMove, replaceCls, start, end } = props
43
41
 
@@ -55,6 +53,8 @@ const Track = defineComponent({
55
53
  },
56
54
  classNames.track,
57
55
  )
56
+
57
+ const positionStyle: CSSProperties = {}
58
58
  switch (direction.value) {
59
59
  case 'rtl':
60
60
  positionStyle.right = `${offsetStart * 100}%`
@@ -120,7 +120,6 @@ function useDrag(
120
120
 
121
121
  const onStartMove: OnStartMove = (e, valueIndex, startValues?: number[]) => {
122
122
  e.stopPropagation()
123
- console.log('onStartMove', valueIndex)
124
123
  // 如果是点击 track 触发的,需要传入变化后的初始值,而不能直接用 rawValues
125
124
  const initialValues = startValues || rawValues.value
126
125
  const originValue = initialValues[valueIndex]
@@ -5,7 +5,7 @@ import { computed } from 'vue'
5
5
  type FormatRangeValue = (value: number) => number
6
6
 
7
7
  /** Format value align with step */
8
- type FormatStepValue = (value: number) => number
8
+ type FormatStepValue = (value: number) => number | null
9
9
 
10
10
  /** Format value align with step & marks */
11
11
  type FormatValue = (value: number) => number
@@ -32,59 +32,58 @@ export type OffsetValues = (
32
32
  export default function useOffset(
33
33
  min: number,
34
34
  max: number,
35
- step: number,
35
+ step: number | null,
36
36
  markList: InternalMarkObj[],
37
37
  allowCross: boolean,
38
- pushable: false | number,
38
+ pushable: false | number | null,
39
39
  ): [FormatValue, OffsetValues] {
40
40
  const formatRangeValue = computed<FormatRangeValue>(() => {
41
41
  return (val: number) => Math.max(min, Math.min(max, val))
42
42
  }).value
43
43
 
44
- const formatStepValue = computed<FormatStepValue>(() => {
45
- return (val: number) => {
46
- if (step !== null) {
47
- const stepValue = min + Math.round((formatRangeValue(val) - min) / step) * step
44
+ const formatStepValue: FormatStepValue = (val: number) => {
45
+ if (step !== null) {
46
+ const stepValue = min + Math.round((formatRangeValue(val) - min) / step) * step
48
47
 
49
- // Cut number in case to be like 0.30000000000000004
50
- const getDecimal = (num: number) => (String(num).split('.')[1] || '').length
51
- const maxDecimal = Math.max(getDecimal(step), getDecimal(max), getDecimal(min))
52
- const fixedValue = Number(stepValue.toFixed(maxDecimal))
48
+ // Cut number in case to be like 0.30000000000000004
49
+ const getDecimal = (num: number) => (String(num).split('.')[1] || '').length
50
+ const maxDecimal = Math.max(getDecimal(step), getDecimal(max), getDecimal(min))
51
+ const fixedValue = Number(stepValue.toFixed(maxDecimal))
53
52
 
54
- return min <= fixedValue && fixedValue <= max ? fixedValue : null
55
- }
56
- return null
53
+ return min <= fixedValue && fixedValue <= max ? fixedValue : null
57
54
  }
58
- }).value
55
+ return null
56
+ }
59
57
 
60
- const formatValue = computed<FormatValue>(() => {
61
- return (val: number) => {
62
- const formatNextValue = formatRangeValue(val)
58
+ const formatValue = (val: number) => {
59
+ const formatNextValue = formatRangeValue(val)
63
60
 
64
- // List align values
65
- const alignValues = markList.map<number>(mark => mark.value)
66
- if (step !== null) {
67
- alignValues.push(formatStepValue(val))
61
+ // List align values
62
+ const alignValues = markList.map<number>(mark => mark && mark.value)
63
+ if (step !== null) {
64
+ const stepValue = formatStepValue(val)
65
+ if (stepValue !== null) {
66
+ alignValues.push(stepValue)
68
67
  }
68
+ }
69
69
 
70
- // min & max
71
- alignValues.push(min, max)
70
+ // min & max
71
+ alignValues.push(min, max)
72
72
 
73
- // Align with marks
74
- let closeValue = alignValues[0]
75
- let closeDist = max - min
73
+ // Align with marks
74
+ let closeValue = alignValues[0]
75
+ let closeDist = max - min
76
76
 
77
- alignValues.forEach((alignValue) => {
78
- const dist = Math.abs(formatNextValue - alignValue)
79
- if (dist <= closeDist) {
80
- closeValue = alignValue
81
- closeDist = dist
82
- }
83
- })
77
+ alignValues.forEach((alignValue) => {
78
+ const dist = Math.abs(formatNextValue - alignValue)
79
+ if (dist <= closeDist) {
80
+ closeValue = alignValue
81
+ closeDist = dist
82
+ }
83
+ })
84
84
 
85
- return closeValue
86
- }
87
- }).value
85
+ return closeValue
86
+ }
88
87
 
89
88
  // ========================== Offset ==========================
90
89
  // Single Value
@@ -106,16 +105,25 @@ export default function useOffset(
106
105
  potentialValues.push(min, max)
107
106
 
108
107
  // In case origin value is align with mark but not with step
109
- potentialValues.push(formatStepValue(originValue))
108
+ const originStepValue = formatStepValue(originValue)
109
+ if (originStepValue !== null) {
110
+ potentialValues.push(originStepValue)
111
+ }
110
112
 
111
113
  // Put offset step value also
112
114
  const sign = offset > 0 ? 1 : -1
113
115
 
114
116
  if (mode === 'unit') {
115
- potentialValues.push(formatStepValue(originValue + sign * step))
117
+ const allStepValues = formatStepValue(originValue + sign * step)
118
+ if (allStepValues !== null) {
119
+ potentialValues.push(allStepValues)
120
+ }
116
121
  }
117
122
  else {
118
- potentialValues.push(formatStepValue(targetDistValue))
123
+ const targetStepValue = formatStepValue(targetDistValue)
124
+ if (targetStepValue !== null) {
125
+ potentialValues.push(targetStepValue)
126
+ }
119
127
  }
120
128
 
121
129
  // Find close one