Skip to content

Commit

Permalink
chore(web): support enter key to submit (#1258)
Browse files Browse the repository at this point in the history
  • Loading branch information
mkumbobeaty authored Nov 19, 2024
1 parent c29f8a0 commit 79e53f9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
17 changes: 16 additions & 1 deletion web/src/beta/lib/reearth-ui/components/NumberInput/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { fonts, styled } from "@reearth/services/theme";
import { useState, useCallback, useEffect, FC, ChangeEvent } from "react";
import {
useState,
useCallback,
useEffect,
FC,
ChangeEvent,
KeyboardEvent
} from "react";

export type NumberInputProps = {
value?: number | string;
Expand Down Expand Up @@ -79,6 +86,13 @@ export const NumberInput: FC<NumberInputProps> = ({
setIsFocused(true);
}, []);

const handleKeyDown = useCallback((e: KeyboardEvent<HTMLInputElement>) => {
if (e.key === "Enter" || e.key === "Return") {
e.preventDefault();
(e.target as HTMLInputElement).blur();
}
}, []);

return (
<Wrapper
size={size}
Expand All @@ -93,6 +107,7 @@ export const NumberInput: FC<NumberInputProps> = ({
onChange={handleChange}
onBlur={handleBlur}
onFocus={handleFocus}
onKeyDown={handleKeyDown}
/>
{unit && <UnitWrapper> {unit}</UnitWrapper>}
</Wrapper>
Expand Down
13 changes: 12 additions & 1 deletion web/src/beta/lib/reearth-ui/components/TextInput/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,17 @@ export const TextInput: FC<TextInputProps> = ({
setIsFocused(true);
}, []);

const handleKeyDown = useCallback(
(e: KeyboardEvent<HTMLInputElement>) => {
if (e.key === "Enter" || e.key === "Return") {
e.preventDefault();
(e.target as HTMLInputElement).blur();
}
onKeyDown?.(e);
},
[onKeyDown]
);

return (
<Wrapper
size={size}
Expand All @@ -91,7 +102,7 @@ export const TextInput: FC<TextInputProps> = ({
onFocus={handleFocus}
appearance={appearance}
autoFocus={autoFocus}
onKeyDown={onKeyDown}
onKeyDown={handleKeyDown}
type={type}
/>
{actions && <ActionsWrapper>{actions}</ActionsWrapper>}
Expand Down

0 comments on commit 79e53f9

Please sign in to comment.