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 all 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
32 changes: 8 additions & 24 deletions src/components/input/input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import { CloseCircleFill } from 'antd-mobile-icons'
import { NativeProps, withNativeProps } from '../../utils/native-props'
import { mergeProps } from '../../utils/with-default-props'
import classNames from 'classnames'
import { useIsomorphicLayoutEffect } from 'ahooks'
import { bound } from '../../utils/bound'
import { isIOS } from '../../utils/validate'
import { useConfig } from '../config-provider'
import useInputHandleKeyDown from './useInputHandleKeyDown'

const classPrefix = `adm-input`

Expand Down 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 All @@ -87,6 +80,12 @@ export const Input = forwardRef<InputRef, InputProps>((p, ref) => {
const compositionStartRef = useRef(false)
const nativeInputRef = useRef<HTMLInputElement>(null)
const { locale } = useConfig()
const handleKeydown = useInputHandleKeyDown({
onEnterPress: props.onEnterPress,
onKeyDown: props.onKeyDown,
nativeInputRef,
enterKeyHint: props.enterKeyHint,
})

useImperativeHandle(ref, () => ({
clear: () => {
Expand All @@ -103,21 +102,6 @@ export const Input = forwardRef<InputRef, InputProps>((p, ref) => {
},
}))

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

useIsomorphicLayoutEffect(() => {
if (!props.enterKeyHint) return
nativeInputRef.current?.setAttribute('enterkeyhint', props.enterKeyHint)
return () => {
nativeInputRef.current?.removeAttribute('enterkeyhint')
}
}, [props.enterKeyHint])

function checkValue() {
let nextValue = value
if (props.type === 'number') {
Expand Down
37 changes: 37 additions & 0 deletions src/components/input/useInputHandleKeyDown.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { useIsomorphicLayoutEffect } from 'ahooks'

interface InputHandleKeyDownType<T> {
onEnterPress?: (e: React.KeyboardEvent<T>) => void
onKeyDown?: (e: React.KeyboardEvent<T>) => void
enterKeyHint?: React.InputHTMLAttributes<HTMLInputElement>['enterKeyHint']
nativeInputRef: React.RefObject<T>
}

export default function useInputHandleKeyDown<
T extends HTMLInputElement | HTMLTextAreaElement,
>({
onEnterPress,
onKeyDown,
nativeInputRef,
enterKeyHint,
}: InputHandleKeyDownType<T>) {
const handleKeydown = (e: React.KeyboardEvent<T>) => {
if (onEnterPress && (e.code === 'Enter' || e.keyCode === 13)) {
onEnterPress(e)
}
onKeyDown?.(e)
}

useIsomorphicLayoutEffect(() => {
const ele = nativeInputRef.current

if (!enterKeyHint || !ele) return

ele.setAttribute('enterkeyhint', enterKeyHint)
return () => {
ele.removeAttribute('enterkeyhint')
}
}, [enterKeyHint])

return handleKeydown
}
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 })
})
})
19 changes: 19 additions & 0 deletions src/components/text-area/text-area.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { NativeProps, withNativeProps } from '../../utils/native-props'
import { usePropsValue } from '../../utils/use-props-value'
import { mergeProps } from '../../utils/with-default-props'
import { devError } from '../../utils/dev-log'
import useInputHandleKeyDown from '../../components/input/useInputHandleKeyDown'

const classPrefix = 'adm-text-area'

Expand All @@ -24,6 +25,7 @@ export type TextAreaProps = Pick<
| 'onCompositionStart'
| 'onCompositionEnd'
| 'onClick'
| 'onKeyDown'
> & {
onChange?: (val: string) => void
value?: string
Expand All @@ -39,6 +41,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 @@ -82,6 +93,13 @@ export const TextArea = forwardRef<TextAreaRef, TextAreaProps>(
// https://github.com/ant-design/ant-design-mobile/issues/6051
const hiddenTextAreaRef = useRef<HTMLTextAreaElement>(null)

const handleKeydown = useInputHandleKeyDown({
onEnterPress: props.onEnterPress,
onKeyDown: props.onKeyDown,
nativeInputRef: nativeTextAreaRef,
enterKeyHint: props.enterKeyHint,
})

useImperativeHandle(ref, () => ({
clear: () => {
setValue('')
Expand Down Expand Up @@ -182,6 +200,7 @@ export const TextArea = forwardRef<TextAreaRef, TextAreaProps>(
onFocus={props.onFocus}
onBlur={props.onBlur}
onClick={props.onClick}
onKeyDown={handleKeydown}
/>
{count}

Expand Down
Loading