Skip to content

Commit 5eabc43

Browse files
authored
fix(data-grid): fix inline number cell editing (#1073)
* fix(data-grid): fix NumberCell only allowing one character at a time - Track isEditing transition with ref to only focus on edit start - Remove select() call that was selecting all content on every keystroke - type=number inputs don't support setSelectionRange, so just focus * style(data-grid): hide number input spinner controls Add CSS to hide browser default spinner/stepper buttons on number inputs for a cleaner Airtable-like UX * chore: orgnaize
1 parent 6a801c5 commit 5eabc43

2 files changed

Lines changed: 11 additions & 5 deletions

File tree

‎public/r/data-grid.json‎

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

‎src/components/data-grid/data-grid-cell-variants.tsx‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -384,12 +384,15 @@ export function NumberCell<TData>({
384384
const [value, setValue] = React.useState(String(initialValue ?? ""));
385385
const inputRef = React.useRef<HTMLInputElement>(null);
386386
const containerRef = React.useRef<HTMLDivElement>(null);
387+
387388
const cellOpts = cell.column.columnDef.meta?.cell;
388389
const numberCellOpts = cellOpts?.variant === "number" ? cellOpts : null;
389390
const min = numberCellOpts?.min;
390391
const max = numberCellOpts?.max;
391392
const step = numberCellOpts?.step;
392393

394+
const prevIsEditingRef = React.useRef(isEditing);
395+
393396
const prevInitialValueRef = React.useRef(initialValue);
394397
if (initialValue !== prevInitialValueRef.current) {
395398
prevInitialValueRef.current = initialValue;
@@ -449,9 +452,12 @@ export function NumberCell<TData>({
449452
);
450453

451454
React.useEffect(() => {
452-
if (isEditing && inputRef.current) {
455+
const wasEditing = prevIsEditingRef.current;
456+
prevIsEditingRef.current = isEditing;
457+
458+
// Only focus when we start editing (transition from false to true)
459+
if (isEditing && !wasEditing && inputRef.current) {
453460
inputRef.current.focus();
454-
inputRef.current.select();
455461
}
456462
}, [isEditing]);
457463

@@ -473,15 +479,15 @@ export function NumberCell<TData>({
473479
>
474480
{isEditing ? (
475481
<input
476-
ref={inputRef}
477482
type="number"
483+
ref={inputRef}
478484
value={value}
479485
min={min}
480486
max={max}
481487
step={step}
488+
className="w-full border-none bg-transparent p-0 outline-none [appearance:textfield] [&::-webkit-inner-spin-button]:appearance-none [&::-webkit-outer-spin-button]:appearance-none"
482489
onBlur={onBlur}
483490
onChange={onChange}
484-
className="w-full border-none bg-transparent p-0 outline-none"
485491
/>
486492
) : (
487493
<span data-slot="grid-cell-content">{value}</span>

0 commit comments

Comments
 (0)