Skip to content
Draft
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
59 changes: 42 additions & 17 deletions packages/design/src/Counter.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type {ChangeEvent} from 'react';
import React from 'react';
import React, {useEffect, useState} from 'react';
import {Card} from './Card';
import {cn} from './helpers/cn';

Expand Down Expand Up @@ -53,6 +53,17 @@ export const Counter: React.FC<CounterProps> = ({
minCount = 0,
step = 1,
}) => {
// Local state to track the input value while typing
const [inputValue, setInputValue] = useState(String(count));
const [isFocused, setIsFocused] = useState(false);

// Update local input value when count changes from outside (e.g., increment/decrement)
useEffect(() => {
if (!isFocused) {
setInputValue(String(count));
}
}, [count, isFocused]);

const decrement = () => {
if (count > minCount) {
setCount(Math.max(minCount, count - step));
Expand All @@ -63,6 +74,29 @@ export const Counter: React.FC<CounterProps> = ({
setCount(count + step);
};

const validateAndSetCount = (value: string) => {
if (value.trim() === '') {
setCount(step === 1 ? 1 : minCount);
return;
}

const parsedValue = parseInt(value, 10);
if (isNaN(parsedValue)) {
setInputValue(String(count));
return;
}

const validValue = Math.max(parsedValue, minCount);

// For steps > 1, round to the nearest valid step
if (step > 1) {
const roundedValue = Math.round(validValue / step) * step;
setCount(Math.max(roundedValue, minCount));
} else {
setCount(validValue);
}
};

return (
<Card
style={container}
Expand All @@ -74,23 +108,14 @@ export const Counter: React.FC<CounterProps> = ({
}
type="number"
onClick={(e) => e.currentTarget.select()}
value={count}
value={inputValue}
onChange={(e: ChangeEvent<HTMLInputElement>) => {
if (e.target.value.trim() === '') {
setCount(step === 1 ? 1 : minCount);
return;
}

const inputValue = parseInt(e.target.value, 10);
const validValue = Math.max(inputValue, minCount);

// For steps > 1, round to the nearest valid step
if (step > 1) {
const roundedValue = Math.round(validValue / step) * step;
setCount(Math.max(roundedValue, minCount));
} else {
setCount(validValue);
}
setInputValue(e.target.value);
}}
onFocus={() => setIsFocused(true)}
onBlur={() => {
setIsFocused(false);
validateAndSetCount(inputValue);
}}
/>
<div className="flex flex-col h-full">
Expand Down