Skip to content
This repository was archived by the owner on Mar 25, 2025. It is now read-only.

Commit 846092f

Browse files
danielkaxisTigge
authored andcommitted
feat(input): show decimals if step is defined
If attribute step is specified, show base values together with decimals.
1 parent 0582fe2 commit 846092f

File tree

1 file changed

+32
-4
lines changed

1 file changed

+32
-4
lines changed

packages/core/src/Input/Input.tsx

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
RefObject,
1010
KeyboardEventHandler,
1111
MouseEventHandler,
12+
useMemo,
1213
} from 'react'
1314
import styled, { css, useTheme } from 'styled-components'
1415

@@ -21,7 +22,7 @@ import { VisibilityIcon, NotVisibilityIcon, ErrorIcon } from './icons'
2122

2223
type BaseElement = HTMLInputElement
2324
type BaseProps = InputHTMLAttributes<BaseElement>
24-
export type NumberInputType = number | ''
25+
export type NumberInputType = number | string | ''
2526
export type InputChangeHandler = ChangeEventHandler<BaseElement>
2627
export type InputValueChangeHandler<T> = (value: T) => void
2728
export type TextInputWidth = 'small' | 'medium' | 'large' | 'full'
@@ -468,9 +469,36 @@ export const TextInput: FC<TextInputProps> = props => (
468469
)
469470

470471
export interface NumberInputProps extends BaseInputProps<NumberInputType> {}
471-
export const NumberInput: FC<NumberInputProps> = props => (
472-
<Input {...props} type="number" />
473-
)
472+
export const NumberInput: FC<NumberInputProps> = ({
473+
value,
474+
step,
475+
...props
476+
}) => {
477+
const decimalValue = useMemo(() => {
478+
if (step === undefined) {
479+
return 0
480+
}
481+
const [_, decimals] = step.toString().split('.') as [
482+
string,
483+
string | undefined
484+
]
485+
486+
return decimals?.length ?? 0
487+
}, [step])
488+
489+
return (
490+
<Input
491+
value={
492+
step !== undefined && value !== ''
493+
? parseFloat(value.toString()).toFixed(decimalValue)
494+
: value
495+
}
496+
step={step}
497+
{...props}
498+
type="number"
499+
/>
500+
)
501+
}
474502

475503
export interface TextInputCredentialsProps extends BaseInputProps<string> {
476504
readonly type: TextInputCredentialsType

0 commit comments

Comments
 (0)