Skip to content

Commit

Permalink
Input number on enter (#1982)
Browse files Browse the repository at this point in the history
  • Loading branch information
RunDevelopment authored Jul 24, 2023
1 parent d6052dd commit e38d512
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions src/renderer/components/inputs/elements/AdvanceNumberInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,21 @@ export const AdvancedNumberInput = memo(
inputHeight,
noRepeatOnBlur = false,
}: AdvancedNumberInputProps) => {
const getNumericValue = (): number | undefined => {
const valAsNumber =
precision > 0
? parseFloat(inputString || String(defaultValue))
: Math.round(parseFloat(inputString || String(defaultValue)));

if (!Number.isNaN(valAsNumber)) {
return Number(clamp(valAsNumber, min, max).toFixed(precision));
}
};
const onBlur = noRepeatOnBlur
? noop
: () => {
const valAsNumber =
precision > 0
? parseFloat(inputString || String(defaultValue))
: Math.round(parseFloat(inputString || String(defaultValue)));

if (!Number.isNaN(valAsNumber)) {
const value = Number(clamp(valAsNumber, min, max).toFixed(precision));

const value = getNumericValue();
if (value !== undefined) {
// Make sure the input value has been altered so onChange gets correct value if adjustment needed
setImmediate(() => {
setInput(value);
Expand All @@ -91,6 +95,15 @@ export const AdvancedNumberInput = memo(
}
};

const onKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => {
if (e.key === 'Enter') {
const value = getNumericValue();
if (value !== undefined) {
setInput(value);
}
}
};

if (small) {
return (
<InputGroup
Expand Down Expand Up @@ -132,6 +145,7 @@ export const AdvancedNumberInput = memo(
p={1}
size={1}
w={inputWidth}
onKeyDown={onKeyDown}
/>
<NumberInputStepper w={4}>
<NumberIncrementStepper />
Expand Down Expand Up @@ -179,6 +193,7 @@ export const AdvancedNumberInput = memo(
px={unit ? 2 : 4}
size={1}
w={inputWidth}
onKeyDown={onKeyDown}
/>
<NumberInputStepper>
<NumberIncrementStepper />
Expand Down

0 comments on commit e38d512

Please sign in to comment.