Skip to content

Commit

Permalink
Merge pull request #733 from illacloud/fix/slider-min-value
Browse files Browse the repository at this point in the history
Fix/slider min value
  • Loading branch information
AruSeito committed Jul 27, 2023
2 parents cad5352 + ead349c commit a2b7cb9
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 16 deletions.
3 changes: 1 addition & 2 deletions packages/slider/src/style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ export function applyBgColor(
export const applySliderWrapper = css`
width: 100%;
height: 40px;
padding: 0 8px;
box-sizing: content-box;
display: flex;
align-items: center;
Expand Down Expand Up @@ -161,4 +160,4 @@ export function applyNumTick(
transform: translateX(-50%);
cursor: ${disabled ? "auto" : "pointer"};
`
}
}
32 changes: 20 additions & 12 deletions packages/slider/src/useOffset.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { useState, useCallback, useMemo, useEffect } from "react"
import { BarLocation } from "./content"
import { IUseOffsetReturn } from "./interface"
import { formatValue, verifyRightValue, verifyLeftValue } from "./utils"
import {
formatValue,
verifyRightValue,
verifyLeftValue,
processNumber,
} from "./utils"

export const useOffset = (
min: number,
Expand All @@ -15,8 +20,8 @@ export const useOffset = (
const [partLength, setPartLength] = useState<number>(0)
const [width, setWidth] = useState<number>(0)
const cacheValue = useMemo(() => {
return formatValue(currentValue)
}, [currentValue])
return formatValue(currentValue, step)
}, [currentValue, step])

const getOffsetValueFromState = useCallback(
(rightVal: number, leftVal?: number) => {
Expand Down Expand Up @@ -81,7 +86,7 @@ export const useOffset = (
val = tempVal
}
onSingleEnd(x, startValue)
setBarLength(Math.round(startValue / step) * partLength + val)
setBarLength(Math.round((startValue - min) / step) * partLength + val)
}

const onRangeDragging = (
Expand Down Expand Up @@ -158,9 +163,10 @@ export const useOffset = (
} else {
currentVal = val
}
onAfterChange && onAfterChange(currentVal)
onChange && onChange(currentVal)
setCurrentValue(currentVal)
const formatVal = processNumber(currentVal, step)
onAfterChange && onAfterChange(formatVal)
onChange && onChange(formatVal)
setCurrentValue(formatVal)
let [_, rightVal] = getOffsetValueFromState(currentVal)
setRightOffset(rightVal)
}
Expand Down Expand Up @@ -199,9 +205,10 @@ export const useOffset = (
break
}
}
onAfterChange && onAfterChange(currentVal)
onChange && onChange(currentVal)
setCurrentValue(currentVal.join(","))
const formatVal = currentVal.map((v) => processNumber(v, step))
onAfterChange && onAfterChange(formatVal)
onChange && onChange(formatVal)
setCurrentValue(formatVal.join(","))
let [leftVal, rightVal] = getOffsetValueFromState(
currentVal[1],
currentVal[0],
Expand Down Expand Up @@ -245,8 +252,9 @@ export const useOffset = (
return offset
}
})
onAfterChange && onAfterChange(currentRangeVal)
setCurrentValue(currentRangeVal.join(","))
const formatVal = currentRangeVal.map((v) => processNumber(v, step))
onAfterChange && onAfterChange(formatVal)
setCurrentValue(formatVal.join(","))
}

const onClickTick = (v: number) => {
Expand Down
26 changes: 24 additions & 2 deletions packages/slider/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { BarLocation } from "./content"

export const formatValue = (val: string | number) => {
export const formatValue = (val: string | number, step: number) => {
if (typeof val !== "number" && !val) return 0
if (typeof val === "number") return val
return Array.from(val.split(","), (v) => parseInt(v))
return Array.from(val.split(","), (v) => {
if (getDecimalDigitsOrZero(step) !== 0) {
return processNumber(parseFloat(v), step)
}
return parseInt(v)
})
}

export const getMarkBound = (
Expand Down Expand Up @@ -72,3 +77,20 @@ export const verifyValue = (value: number | number[]) => {
}

export const getSafeStep = (step: number) => (step && step > 0 ? step : 1)

export const getDecimalDigitsOrZero = (num: number): number => {
if (Number.isInteger(num)) {
return 0
} else {
const numStr = num.toString()
const decimalIndex = numStr.indexOf(".")
return decimalIndex === -1 ? 0 : numStr.length - decimalIndex - 1
}
}

export const processNumber = (num: number, step: number): number => {
const decimalDigits = getDecimalDigitsOrZero(step)
return decimalDigits === 0
? Math.floor(num)
: parseFloat(num.toFixed(decimalDigits))
}

0 comments on commit a2b7cb9

Please sign in to comment.