Skip to content

Commit

Permalink
[#459] Feature: 값이 최소, 최대 값을 넘지 못하도록 limit 과정 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
giwan-dev committed Feb 7, 2020
1 parent 4f4b905 commit 626da50
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions packages/slider/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import Track from './track'
import Handle from './handle'

type SliderValue = readonly number[]
type ScaleFunction = (x: number) => number
type ValueTransformer = (x: number) => number

interface SliderProps {
initialValues?: SliderValue
Expand Down Expand Up @@ -43,9 +43,12 @@ const RailBase = styled.div`
transform: translate(0, -50%);
`

const IDENTICAL_SCALE: ScaleFunction = (x) => x
const IDENTICAL_SCALE: ValueTransformer = (x) => x

const LINEAR_FN_SET: ValueTransformer[] = [IDENTICAL_SCALE, IDENTICAL_SCALE]

const EXPONENT = 1 / Math.E
const [nonLinearScale, nonLinearScaleInverse]: ScaleFunction[] = [
const NON_LINEAR_FN_SET: ValueTransformer[] = [
(x) => Math.round(Math.pow(x, EXPONENT)),
(x) => Math.round(Math.pow(x, 1 / EXPONENT)),
]
Expand All @@ -61,8 +64,19 @@ export default function Slider({
}: SliderProps) {
const [values, setValues] = useState<SliderValue>(initialValues || [min, max])

const scaleFn = nonLinear ? nonLinearScale : IDENTICAL_SCALE
const scaleFnInverse = nonLinear ? nonLinearScaleInverse : IDENTICAL_SCALE
const [scaleFn, scaleFnInverse] = nonLinear
? NON_LINEAR_FN_SET
: LINEAR_FN_SET

const limiter: ValueTransformer = (value) => {
if (value < min) {
return min
}
if (value > max) {
return max
}
return value
}

const debouncedChangeHandler = useCallback(debounce(onChange, 500), [
onChange,
Expand All @@ -85,7 +99,9 @@ export default function Slider({
step={scaleFn(step)}
domain={[min, max].map(scaleFn)}
rootStyle={{ position: 'relative' }}
onUpdate={(newValues) => setValues(newValues.map(scaleFnInverse))}
onUpdate={(newValues) =>
setValues(newValues.map(scaleFnInverse).map(limiter))
}
>
<Rail>{() => <RailBase />}</Rail>

Expand Down

0 comments on commit 626da50

Please sign in to comment.