11import { Input , Form , ConfigProvider } from "antd" ;
22import type { Expression , EnsembleAction } from "@ensembleui/react-framework" ;
33import { useRegisterBindings } from "@ensembleui/react-framework" ;
4- import {
5- useEffect ,
6- useMemo ,
7- useState ,
8- useCallback ,
9- useRef ,
10- type FormEvent ,
11- RefCallback ,
12- } from "react" ;
4+ import { useEffect , useMemo , useState , useCallback , useRef } from "react" ;
5+ import type { RefCallback , FormEvent } from "react" ;
136import { runes } from "runes2" ;
147import type { Rule } from "antd/es/form" ;
15- import { forEach } from "lodash-es" ;
16- import IMask , { InputMask } from "imask" ;
8+ import { forEach , isObject , omitBy } from "lodash-es" ;
9+ import IMask , { type InputMask } from "imask" ;
1710import type { EnsembleWidgetProps } from "../../shared/types" ;
1811import { WidgetRegistry } from "../../registry" ;
1912import type { TextStyles } from "../Text" ;
@@ -26,7 +19,10 @@ const widgetName = "TextInput";
2619export type TextInputProps = {
2720 hintStyle ?: TextStyles ;
2821 labelStyle ?: TextStyles ;
22+ /** @deprecated see {@link TextInputProps.multiline} */
2923 multiLine ?: Expression < boolean > ;
24+ /** Specify whether this Text Input should span multiple lines */
25+ multiline ?: Expression < boolean > ;
3026 maxLines ?: number ;
3127 maxLength ?: Expression < number > ;
3228 maxLengthEnforcement ?: Expression <
@@ -42,6 +38,7 @@ export type TextInputProps = {
4238 regexError ?: string ;
4339 maskError ?: string ;
4440 } ;
41+ onKeyDown : EnsembleAction ;
4542} & EnsembleWidgetProps < TextStyles > &
4643 FormInputProps < string > ;
4744
@@ -62,6 +59,7 @@ export const TextInput: React.FC<TextInputProps> = (props) => {
6259 ) ;
6360 const formInstance = Form . useFormInstance ( ) ;
6461 const action = useEnsembleAction ( props . onChange ) ;
62+ const onKeyDownAction = useEnsembleAction ( props . onKeyDown ) ;
6563
6664 const handleChange = useCallback (
6765 ( newValue : string ) => {
@@ -76,7 +74,7 @@ export const TextInput: React.FC<TextInputProps> = (props) => {
7674 rootRef ( node ) ;
7775 } ;
7876
79- const handleKeyDown = useCallback ( ( e : FormEvent < HTMLInputElement > ) => {
77+ const sanitizeNumberInput = useCallback ( ( e : FormEvent < HTMLInputElement > ) => {
8078 const target = e . target as HTMLInputElement ;
8179 target . value = target . value . replace ( / [ ^ 0 - 9 . ] / g, "" ) ;
8280 } , [ ] ) ;
@@ -92,6 +90,17 @@ export const TextInput: React.FC<TextInputProps> = (props) => {
9290 [ handleChange , mask ] ,
9391 ) ;
9492
93+ const handleKeyDown = useCallback (
94+ ( event : React . KeyboardEvent < HTMLTextAreaElement | HTMLInputElement > ) =>
95+ onKeyDownAction ?. callback ( {
96+ event : {
97+ ...omitBy ( event , isObject ) ,
98+ preventDefault : event . preventDefault . bind ( event ) ,
99+ } ,
100+ } ) ,
101+ [ onKeyDownAction ] ,
102+ ) ;
103+
95104 useEffect ( ( ) => {
96105 setValue ( values ?. initialValue ) ;
97106 } , [ values ?. initialValue ] ) ;
@@ -252,12 +261,13 @@ export const TextInput: React.FC<TextInputProps> = (props) => {
252261 theme = { { token : { colorTextPlaceholder : values ?. hintStyle ?. color } } }
253262 >
254263 < EnsembleFormItem rules = { rules } valuePropName = "value" values = { values } >
255- { values ?. multiLine ? (
264+ { values ?. multiLine || values ?. multiline ? (
256265 < Input . TextArea
257266 count = { maxLengthConfig }
258267 defaultValue = { values . value }
259268 disabled = { values . enabled === false }
260- onChange = { ( event ) : void => setValue ( event . target . value ) }
269+ onChange = { ( event ) : void => handleChange ( event . target . value ) }
270+ onKeyDown = { handleKeyDown }
261271 placeholder = { values . hintText ?? "" }
262272 ref = { rootRef }
263273 rows = { values . maxLines ? Number ( values . maxLines ) : 4 } // Adjust the number of rows as needed
@@ -276,8 +286,9 @@ export const TextInput: React.FC<TextInputProps> = (props) => {
276286 disabled = { values ?. enabled === false }
277287 onChange = { ( event ) : void => handleChange ( event . target . value ) }
278288 { ...( values ?. inputType === "number" && {
279- onInput : ( event ) : void => handleKeyDown ( event ) ,
289+ onInput : ( event ) : void => sanitizeNumberInput ( event ) ,
280290 } ) }
291+ onKeyDown = { handleKeyDown }
281292 onPaste = { handleInputPaste }
282293 placeholder = { values ?. hintText ?? "" }
283294 ref = { handleRef }
0 commit comments