Skip to content

Commit

Permalink
Simplify React input code as well
Browse files Browse the repository at this point in the history
  • Loading branch information
barvian committed Oct 28, 2024
1 parent 05c3bb5 commit 104c6e8
Showing 1 changed file with 7 additions and 11 deletions.
18 changes: 7 additions & 11 deletions site/src/pages/[...framework]/examples/_Input/react/Component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,16 @@ export default function Input({ value = 0, min = -Infinity, max = Infinity, onCh

const handleInput: React.ChangeEventHandler<HTMLInputElement> = ({ currentTarget: el }) => {
setAnimated(false)
let next = value
if (el.value === '') {
onChange?.(defaultValue.current)
return
}
const num = parseInt(el.value)
if (isNaN(num) || (min != null && num < min) || (max != null && num > max)) {
// Revert input's value:
el.value = String(value)
next = defaultValue.current
} else {
// Manually update value in case they e.g. start with a "0" or end with a "."
// which won't trigger a DOM update (because the number is the same):
el.value = String(num)
onChange?.(num)
const num = parseInt(el.value)
if (!isNaN(num) && min <= num && num <= max) next = num
}
// Manually update the input.value in case the number stays the same i.e. 09 == 9
el.value = String(next)
onChange?.(next)
}

const handlePointerDown = (diff: number) => (event: React.PointerEvent<HTMLButtonElement>) => {
Expand Down

0 comments on commit 104c6e8

Please sign in to comment.