Skip to content

Commit

Permalink
Fix decimal steps for Number input
Browse files Browse the repository at this point in the history
  • Loading branch information
asimonok committed May 15, 2024
1 parent 3576b8a commit 0de05a6
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 8 deletions.
2 changes: 1 addition & 1 deletion packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Change Log

## 2.0.0 (2024-05-15)
## 2.0.1 (2024-05-15)

### Features

Expand Down
2 changes: 1 addition & 1 deletion packages/components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,5 +86,5 @@
"typecheck": "tsc --emitDeclarationOnly false --noEmit"
},
"types": "dist/index.d.ts",
"version": "2.0.0"
"version": "2.0.1"
}
22 changes: 21 additions & 1 deletion packages/components/src/components/Form/Form.story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const meta = {
const form = useFormBuilder<{
opacity: number;
step: number;
decimal: number;
normalize: [number, number];
custom: string;
color: string;
Expand Down Expand Up @@ -62,7 +63,26 @@ const meta = {
9: '9',
},
label: 'Step',
description: 'Move to 2 points',
description: 'Move by 2 step',
view: {
grow: true,
},
})
.addSlider({
path: 'decimal',
defaultValue: 1,
step: 0.01,
min: 1,
max: 9,
marks: {
1: '1',
3: '3',
5: '5',
7: '7',
9: '9',
},
label: 'Decimal step',
description: 'Move by 0.01 step',
view: {
grow: true,
},
Expand Down
10 changes: 5 additions & 5 deletions packages/components/src/components/NumberInput/NumberInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,22 +77,22 @@ export const NumberInput: React.FC<Props> = ({ value, onChange, min, max, step,
* Round value by step
*/
if (step !== undefined) {
let availableValue = step;
let availableValue = step * 1000;

if (min !== undefined) {
availableValue = min;
availableValue = min * 1000;
}

/**
* Find nearest available value
* Next available value will be taken if entered value between allowed range
* 7 will be used for 6 entered value with 5,7 available values
*/
while (availableValue * 1000 < v * 1000) {
availableValue += step;
while (availableValue < v * 1000) {
availableValue += step * 1000;
}

v = availableValue;
v = availableValue / 1000;
}

if (max !== undefined && v > max) {
Expand Down

0 comments on commit 0de05a6

Please sign in to comment.