Skip to content

Commit 142afcd

Browse files
feat(TextArea): expose onKeydown on TextArea (#2532)
1 parent c1b92aa commit 142afcd

File tree

4 files changed

+44
-1
lines changed

4 files changed

+44
-1
lines changed

.changeset/twenty-planets-tell.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@razorpay/blade": minor
3+
---
4+
5+
feat(TextArea): expose `onKeydown` on TextArea and allow `numOfLines={1}`

packages/blade/src/components/Input/BaseInput/BaseInput.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ type BaseInputCommonProps = FormInputLabelProps &
229229
/**
230230
* Sets the textarea's number of lines
231231
*/
232-
numberOfLines?: 2 | 3 | 4 | 5;
232+
numberOfLines?: 1 | 2 | 3 | 4 | 5;
233233
/**
234234
* Sets the accessibility label for the input
235235
*/

packages/blade/src/components/Input/TextArea/TextArea.stories.tsx

+21
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { Button } from '~components/Button';
1010
import { getStyledPropsArgTypes } from '~components/Box/BaseBox/storybookArgTypes';
1111
import { Box } from '~components/Box';
1212
import { Text } from '~components/Typography';
13+
import { ToastContainer, useToast } from '~components/Toast';
1314

1415
const propsCategory = {
1516
BASE_PROPS: 'TextArea Props',
@@ -462,6 +463,26 @@ export const TextAreaWithTags: StoryFn<typeof TextAreaComponent> = ({ ...args })
462463
);
463464
};
464465

466+
export const TextAreaWithEnterSubmit: StoryFn<typeof TextAreaComponent> = ({ ...args }) => {
467+
const toast = useToast();
468+
return (
469+
<Box display="flex" flexDirection="column">
470+
<ToastContainer />
471+
<TextAreaComponent
472+
{...args}
473+
numberOfLines={3}
474+
placeholder="Press Shift + Enter for next line and Enter for submit"
475+
onKeyDown={({ event, value }) => {
476+
if (!event.shiftKey && event.key === 'Enter') {
477+
event.preventDefault();
478+
toast.show({ content: `Submit: ${value}`, color: 'positive', type: 'informational' });
479+
}
480+
}}
481+
/>
482+
</Box>
483+
);
484+
};
485+
465486
export const TextAreaWithControlledTags: StoryFn<typeof TextAreaComponent> = ({ ...args }) => {
466487
const [tags, setTags] = React.useState<string[]>([]);
467488

packages/blade/src/components/Input/TextArea/TextArea.tsx

+17
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import type {
2121
DataAnalyticsAttribute,
2222
} from '~utils/types';
2323
import { hintMarginTop } from '~components/Form/formTokens';
24+
import type { FormInputOnKeyDownEvent } from '~components/Form/FormTypes';
2425

2526
type TextAreaCommonProps = Pick<
2627
BaseInputProps,
@@ -57,6 +58,16 @@ type TextAreaCommonProps = Pick<
5758
* Event handler to handle the onClick event for clear button. Used when `showClearButton` is `true`
5859
*/
5960
onClearButtonClick?: () => void;
61+
62+
onKeyDown?: ({
63+
name,
64+
value,
65+
event,
66+
}: {
67+
name?: FormInputOnKeyDownEvent['name'];
68+
value: string;
69+
event: FormInputOnKeyDownEvent['event'];
70+
}) => void;
6071
} & TaggedInputProps &
6172
StyledPropsBlade;
6273

@@ -114,6 +125,7 @@ const _TextArea: React.ForwardRefRenderFunction<BladeElementRef, TextAreaProps>
114125
onFocus,
115126
onBlur,
116127
onSubmit,
128+
onKeyDown,
117129
placeholder,
118130
value,
119131
maxCharacters,
@@ -244,6 +256,11 @@ const _TextArea: React.ForwardRefRenderFunction<BladeElementRef, TextAreaProps>
244256
}}
245257
onKeyDown={(e) => {
246258
handleTaggedInputKeydown(e);
259+
onKeyDown?.({
260+
name: e.name,
261+
value: e.event.currentTarget.value,
262+
event: e.event,
263+
});
247264
}}
248265
onSubmit={onSubmit}
249266
trailingFooterSlot={(value) => {

0 commit comments

Comments
 (0)