Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #236 #293

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

## [Unreleased]

- Fixed an issue with the initialValue property of the number field. It is
now possible to empty the field while editting when an initialValue was set.
The initialValue will only be set at first render when the value is undefined
or when leaving the required field with an empty value. Leaving an optional
empty number field will not add the initialValue.
- Added the step property to the number fields. It specifies the interval
between legal numbers in the input field. Default is 1.

Expand Down
21 changes: 17 additions & 4 deletions packages/input.number/src/NumberInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {IcRoundNumbers} from '@alinea/ui/icons/IcRoundNumbers'
import {NumberField} from './NumberField'
import css from './NumberInput.module.scss'
import {fromModule} from '@alinea/ui'
import {useState} from 'react'

const styles = fromModule(css)

Expand All @@ -23,7 +24,8 @@ export function NumberInput({state, field}: NumberInputProps) {
maxValue,
step
} = field.options
const [value = initialValue, setValue] = useInput(state)
const [value, setValue] = useInput(state)
const [defined, setDefined] = useState<boolean>(typeof value === undefined)

return (
<InputLabel
Expand All @@ -38,14 +40,25 @@ export function NumberInput({state, field}: NumberInputProps) {
<input
type="number"
className={styles.root.input()}
value={String(value ?? '')}
onChange={e =>
value={
String(value ?? '') || (!defined ? String(initialValue ?? '') : '')
}
onChange={e => {
setValue(
e.currentTarget.value === ''
? undefined!
: Number(e.currentTarget.value)
)
}
if (!defined) setDefined(true)
}}
onBlur={e => {
if (
!optional &&
value === undefined &&
typeof initialValue !== 'undefined'
)
setValue(initialValue)
}}
min={minValue}
max={maxValue}
step={step || 1}
Expand Down