Skip to content

Commit 0c873e9

Browse files
committed
Fix gap input, allow any integer gap
1 parent ff2b91d commit 0c873e9

File tree

2 files changed

+36
-7
lines changed

2 files changed

+36
-7
lines changed

plugins/tidyup/src/App.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ const SortingSchema = v.union(allSortings.map(sorting => v.literal(sorting)))
339339
type Sorting = v.InferOutput<typeof SortingSchema>
340340

341341
const ColumnCountSchema = v.pipe(v.number(), v.integer(), v.minValue(1))
342-
const GapSchema = v.pipe(v.number(), v.integer(), v.minValue(0), v.multipleOf(10))
342+
const GapSchema = v.pipe(v.number(), v.integer(), v.minValue(0))
343343

344344
export function App() {
345345
const isAllowedToSetAttributes = useIsAllowedTo("setAttributes")

plugins/tidyup/src/Stepper.tsx

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { useEffect, useState } from "react"
12
import { isNumber } from "./isNumber"
23

34
import "./Stepper.css"
@@ -36,23 +37,51 @@ function clamp(value: number, min: number, max: number) {
3637
}
3738

3839
export function Stepper({ value = 0, min = 0, step: stepAmount = 1, onChange }: Props) {
40+
const [inputValue, setInputValue] = useState<string>(() => String(value))
41+
42+
useEffect(() => {
43+
setInputValue(String(value))
44+
}, [value])
45+
3946
const step = (direction: -1 | 1) => {
40-
onChange(clamp(value + stepAmount * direction, 0, Infinity))
47+
onChange(clamp(value + stepAmount * direction, min, Infinity))
48+
}
49+
50+
const commitInput = () => {
51+
const trimmed = inputValue.trim()
52+
if (trimmed === "") {
53+
setInputValue(String(value))
54+
return
55+
}
56+
57+
const parsed = Number(trimmed)
58+
if (!isNumber(parsed)) {
59+
setInputValue(String(value))
60+
return
61+
}
62+
63+
const snapped = Math.round(clamp(parsed, min, Infinity))
64+
65+
setInputValue(String(snapped))
66+
onChange(snapped)
4167
}
4268

4369
return (
4470
<div className="stepper">
4571
<input
4672
className="stepper-input"
4773
type="number"
48-
value={value}
74+
value={inputValue}
4975
min={min}
5076
step={stepAmount}
5177
onChange={event => {
52-
const numberValue = event.currentTarget.valueAsNumber
53-
const value = isNumber(numberValue) ? numberValue : 0
54-
55-
onChange(value)
78+
setInputValue(event.currentTarget.value)
79+
}}
80+
onBlur={commitInput}
81+
onKeyDown={event => {
82+
if (event.key === "Enter") {
83+
commitInput()
84+
}
5685
}}
5786
/>
5887

0 commit comments

Comments
 (0)