Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(TextArea): add enterkeyhint and onEnterPress #6598

Merged
merged 6 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 1 addition & 8 deletions src/components/input/input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export type InputProps = Pick<
| 'placeholder'
| 'readOnly'
| 'disabled'
| 'enterKeyHint'
> & {
value?: string
defaultValue?: string
Expand All @@ -53,14 +54,6 @@ export type InputProps = Pick<
onlyShowClearWhenFocus?: boolean
onClear?: () => void
onEnterPress?: (e: React.KeyboardEvent<HTMLInputElement>) => void
enterKeyHint?:
| 'enter'
| 'done'
| 'go'
| 'next'
| 'previous'
| 'search'
| 'send'
min?: number
max?: number
} & NativeProps<
Expand Down
23 changes: 23 additions & 0 deletions src/components/text-area/tests/text-area.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
const maxRows = 5
const { getByRole } = render(<TextArea autoSize={{ minRows, maxRows }} />)
const textarea = getByRole('textbox')
const hiddenTextarea = document.querySelector(

Check warning on line 36 in src/components/text-area/tests/text-area.test.tsx

View workflow job for this annotation

GitHub Actions / check

Forbidden non-null assertion
`.${classPrefix}-element-hidden`
)!
// mock
Expand Down Expand Up @@ -123,7 +123,7 @@

test('set rows should be work', () => {
const { getByRole } = render(<TextArea rows={1} autoSize />)
const hiddenTextarea = document.querySelector(

Check warning on line 126 in src/components/text-area/tests/text-area.test.tsx

View workflow job for this annotation

GitHub Actions / check

Forbidden non-null assertion
`.${classPrefix}-element-hidden`
)!
const textarea = getByRole('textbox')
Expand All @@ -142,4 +142,27 @@
const textarea = getByRole('textbox')
expect(textarea).toHaveAttribute('rows', '1')
})

test('should works with `onEnterPress`', async () => {
const onEnterPress = jest.fn()
const { getByRole } = render(
<TextArea defaultValue={'testValue'} onEnterPress={onEnterPress} />
)
const textarea = getByRole('textbox') as HTMLTextAreaElement
expect(textarea).toBeInTheDocument()
act(() => {
textarea.focus()
})
fireEvent.keyDown(textarea, { code: 'Enter' })
expect(onEnterPress).toBeCalledTimes(1)
fireEvent.keyUp(textarea, { code: 'Enter' })

fireEvent.keyDown(textarea, { keyCode: 13 })
expect(onEnterPress).toBeCalledTimes(2)
fireEvent.keyUp(textarea, { keyCode: 13 })

fireEvent.keyDown(textarea, { keyCode: 14 })
expect(onEnterPress).toBeCalledTimes(2)
fireEvent.keyUp(textarea, { keyCode: 14 })
})
})
29 changes: 29 additions & 0 deletions src/components/text-area/text-area.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export type TextAreaProps = Pick<
| 'onCompositionStart'
| 'onCompositionEnd'
| 'onClick'
| 'onKeyDown'
> & {
onChange?: (val: string) => void
value?: string
Expand All @@ -39,6 +40,15 @@ export type TextAreaProps = Pick<
maxRows?: number
}
id?: string
onEnterPress?: (e: React.KeyboardEvent<HTMLTextAreaElement>) => void
enterKeyHint?:
| 'enter'
| 'done'
| 'go'
| 'next'
| 'previous'
| 'search'
| 'send'
} & NativeProps<
| '--font-size'
| '--color'
Expand Down Expand Up @@ -119,6 +129,24 @@ export const TextArea = forwardRef<TextAreaRef, TextAreaProps>(
textArea.style.height = `${height}px`
}, [value, autoSize])

const handleKeydown = (e: React.KeyboardEvent<HTMLTextAreaElement>) => {
if (props.onEnterPress && (e.code === 'Enter' || e.keyCode === 13)) {
props.onEnterPress(e)
}
props.onKeyDown?.(e)
}

useIsomorphicLayoutEffect(() => {
susiwen8 marked this conversation as resolved.
Show resolved Hide resolved
if (!props.enterKeyHint) return
nativeTextAreaRef.current?.setAttribute(
'enterkeyhint',
props.enterKeyHint
)
return () => {
nativeTextAreaRef.current?.removeAttribute('enterkeyhint')
}
}, [props.enterKeyHint])

const compositingRef = useRef(false)

let count
Expand Down Expand Up @@ -182,6 +210,7 @@ export const TextArea = forwardRef<TextAreaRef, TextAreaProps>(
onFocus={props.onFocus}
onBlur={props.onBlur}
onClick={props.onClick}
onKeyDown={handleKeydown}
/>
{count}

Expand Down
Loading