From dbb1a79219b58e7f503dad23dca34b1f0e7faace Mon Sep 17 00:00:00 2001 From: Tim Date: Mon, 16 Dec 2024 10:17:20 +0100 Subject: [PATCH] fix(TextInput): submit not triggering --- src/common/TextInput/TextInput.tsx | 33 ++++++++++++------------------ 1 file changed, 13 insertions(+), 20 deletions(-) diff --git a/src/common/TextInput/TextInput.tsx b/src/common/TextInput/TextInput.tsx index b409f229c..573f69c7d 100644 --- a/src/common/TextInput/TextInput.tsx +++ b/src/common/TextInput/TextInput.tsx @@ -1,32 +1,25 @@ // Copyright (C) 2017-2024 Smart code 203358507 -import React from 'react'; +import React, { forwardRef, useCallback } from 'react'; +import { type KeyboardEvent, type InputHTMLAttributes } from 'react'; import classnames from 'classnames'; import styles from './styles.less'; -type Props = React.InputHTMLAttributes & { +type Props = InputHTMLAttributes & { className?: string; disabled?: boolean; - onKeyDown?: (event: React.KeyboardEvent) => void; - onSubmit?: (event: React.KeyboardEvent) => void; + onKeyDown?: (event: KeyboardEvent) => void; + onSubmit?: (event: KeyboardEvent) => void; }; -const TextInput = React.forwardRef((props, ref) => { - const { onSubmit, className, disabled, ...rest } = props; +const TextInput = forwardRef((props, ref) => { + const onKeyDown = useCallback((event: KeyboardEvent) => { + props.onKeyDown && props.onKeyDown(event); - const onKeyDown = React.useCallback((event: React.KeyboardEvent) => { - if (typeof props.onKeyDown === 'function') { - props.onKeyDown(event); + if (event.key === 'Enter' ) { + props.onSubmit && props.onSubmit(event); } - - if ( - event.key === 'Enter' && - !(event.nativeEvent as any).submitPrevented && - typeof onSubmit === 'function' - ) { - onSubmit(event); - } - }, [props.onKeyDown, onSubmit]); + }, [props.onKeyDown, props.onSubmit]); return ( ((props, ref) => { autoComplete={'off'} spellCheck={false} tabIndex={0} + {...props} ref={ref} - className={classnames(className, styles['text-input'], { disabled })} + className={classnames(props.className, styles['text-input'], { 'disabled': props.disabled })} onKeyDown={onKeyDown} - {...rest} /> ); });