Skip to content

Commit 00b204f

Browse files
Default social input send behavior
1 parent a0d1a3b commit 00b204f

File tree

4 files changed

+310
-1039
lines changed

4 files changed

+310
-1039
lines changed

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) => {
68+
if (event.key === 'Enter' && !event.shiftKey) {
69+
event.preventDefault()
70+
console.log('Enter key pressed')
71+
}
72+
}}
6673
/>
6774
)
6875
}

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

+12-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,11 +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))
8592
}
93+
defaultKeyDown(event, () => form.handleSubmit(submit)(event))
8694
}
8795

8896
const isCreateButtonDisabled = isLoading || !form.formState.isValid || !form.formState.isDirty

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

0 commit comments

Comments
 (0)