Skip to content
This repository has been archived by the owner on Dec 6, 2021. It is now read-only.

Commit

Permalink
fix(slider): value precision is not ensured when step is < 1
Browse files Browse the repository at this point in the history
  • Loading branch information
b2nil committed Nov 29, 2020
1 parent f2331f7 commit 91c1612
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
18 changes: 16 additions & 2 deletions src/components/slider/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ const AtSlider = defineComponent({
_value: clampNumber(props.value!, props.min!, props.max!)
})

const precision = computed(() => 10 ** countDecimals(props.step))

const rootClass = computed(() => ({
'at-slider': true,
'at-slider--disabled': props.disabled
Expand All @@ -77,9 +79,20 @@ const AtSlider = defineComponent({
return Math.max(lower, Math.min(upper, value))
}

function countDecimals(value: number) {
if (Math.floor(value) === value) return 0
return value.toString().split(".")[1].length || 0
}

function ensurePrecision(value: number) {
return Math.round((value + Number.EPSILON) * precision.value) / precision.value
}

function handleChanging(e: CommonEvent): void {
const { _value } = state
const { value }: { value: number } = e.detail
let { value }: { value: number } = e.detail

value = ensurePrecision(value)

if (value !== _value) {
state._value = value
Expand All @@ -88,7 +101,8 @@ const AtSlider = defineComponent({
}

function handleChange(e: CommonEvent): void {
const { value } = e.detail
let { value } = e.detail
value = ensurePrecision(value)

state._value = value
props.onChange && props.onChange(value)
Expand Down
4 changes: 2 additions & 2 deletions src/pages/form/slider/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ export default defineComponent({
default: () => [
h(ExampleItem, null, {
default: () => [
h(View, { class: 'example-item__desc' }, { default: () => 'step=1' }),
h(AtSlider, { step: 1, value: 50, showValue: true })
h(View, { class: 'example-item__desc' }, { default: () => 'step=0.1' }),
h(AtSlider, { step: 0.1, value: 50, showValue: true })
]
}),
]
Expand Down

0 comments on commit 91c1612

Please sign in to comment.