Skip to content

Commit 25c5d9d

Browse files
BA-2197: default social input send behavior (#199)
* Default social input send behavior * Versioning
1 parent 0d77f36 commit 25c5d9d

File tree

6 files changed

+318
-1040
lines changed

6 files changed

+318
-1040
lines changed

packages/components/CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# @baseapp-frontend/components
22

3+
## 1.0.7
4+
5+
### Patch Changes
6+
7+
- Enhanced the SocialInput component by introducing an optional keydown handler prop. This update enables custom keyboard event handling, with default behavior triggering submission on CTRL + Enter.
8+
39
## 1.0.6
410

511
### Patch Changes

packages/components/modules/__shared__/web/SocialInput/__storybook__/SocialInput.mdx

+7
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import { Meta } from '@storybook/addon-docs'
3030
- **submit** (function): Form submission handler.
3131
- **onCancelReply** (function, optional): Handler for canceling a reply.
3232
- **form** (UseFormReturn): react-hook-form form instance.
33+
- **onKeyDown** (function, optional): Custom keydown handler for the input field. Defaults to send with CTRL + Enter
3334

3435
## Notes
3536

@@ -63,6 +64,12 @@ const MyComponent = () => {
6364
submit={() => {}}
6465
onCancelReply={() => {}}
6566
form={useForm()}
67+
onKeyDown={(event, onSubmit) => {
68+
if (event.key === 'Enter' && !event.shiftKey) {
69+
event.preventDefault()
70+
onSubmit()
71+
}
72+
}}
6673
/>
6774
)
6875
}

packages/components/modules/__shared__/web/SocialInput/index.tsx

+13-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use client'
22

3-
import { KeyboardEventHandler, forwardRef } from 'react'
3+
import { KeyboardEvent, KeyboardEventHandler, forwardRef } from 'react'
44

55
import { SocialTextField as DefaultSocialTextField } from '@baseapp-frontend/design-system/components/web/inputs'
66

@@ -68,6 +68,7 @@ const SocialInput = forwardRef<HTMLInputElement, SocialInputProps>(
6868
SocialUpsertActions = DefaultSocialUpsertActions,
6969
SubmitActions = DefaultSubmitActions,
7070
SubmitActionsProps = {},
71+
onKeyDown,
7172
formId = 'text-field-form',
7273
submit,
7374
isLoading,
@@ -78,10 +79,18 @@ const SocialInput = forwardRef<HTMLInputElement, SocialInputProps>(
7879
},
7980
ref,
8081
) => {
81-
const handleKeyDown: KeyboardEventHandler<HTMLDivElement> = (event) => {
82-
if (event.key === 'Enter' && !event.shiftKey) {
82+
const defaultKeyDown = (event: KeyboardEvent<HTMLDivElement>, onSubmit: VoidFunction) => {
83+
if (event.key === 'Enter' && event.ctrlKey) {
8384
event.preventDefault()
84-
form.handleSubmit(submit)(event)
85+
onSubmit()
86+
}
87+
}
88+
89+
const handleKeyDown: KeyboardEventHandler<HTMLDivElement> = (event) => {
90+
if (onKeyDown) {
91+
onKeyDown(event, () => form.handleSubmit(submit)(event))
92+
} else {
93+
defaultKeyDown(event, () => form.handleSubmit(submit)(event))
8594
}
8695
}
8796

packages/components/modules/__shared__/web/SocialInput/types.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { FC } from 'react'
1+
import { FC, KeyboardEvent } from 'react'
22

33
import { SocialTextFieldProps } from '@baseapp-frontend/design-system/components/web/inputs'
44

@@ -15,6 +15,7 @@ export interface SocialInputProps {
1515
SocialUpsertActions?: FC
1616
SubmitActions?: FC<SubmitActionsProps>
1717
SubmitActionsProps?: Partial<SubmitActionsProps>
18+
onKeyDown?: (event: KeyboardEvent<HTMLDivElement>, onSubmit: VoidFunction) => void
1819
formId?: string
1920
submit: (data: SocialUpsertForm) => void
2021
isLoading: boolean

packages/components/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@baseapp-frontend/components",
33
"description": "BaseApp components modules such as comments, notifications, messages, and more.",
4-
"version": "1.0.6",
4+
"version": "1.0.7",
55
"sideEffects": false,
66
"scripts": {
77
"babel:bundle": "babel modules -d tmp-babel --extensions .ts,.tsx --ignore '**/__tests__/**','**/__storybook__/**'",

0 commit comments

Comments
 (0)