File tree Expand file tree Collapse file tree 3 files changed +64
-1
lines changed
packages/runtime/src/widgets/Form Expand file tree Collapse file tree 3 files changed +64
-1
lines changed Original file line number Diff line number Diff line change 1+ ---
2+ " @ensembleui/react-runtime " : patch
3+ ---
4+
5+ Fix: avoid empty field to validate
Original file line number Diff line number Diff line change @@ -196,7 +196,7 @@ export const TextInput: React.FC<TextInputProps> = (props) => {
196196 if ( regex ) {
197197 rulesArray . push ( {
198198 validator : ( _ , inputValue ?: string ) => {
199- if ( new RegExp ( regex ) . test ( inputValue || "" ) ) {
199+ if ( ! inputValue || new RegExp ( regex ) . test ( inputValue || "" ) ) {
200200 return Promise . resolve ( ) ;
201201 }
202202 return Promise . reject (
Original file line number Diff line number Diff line change @@ -290,5 +290,63 @@ describe("Form", () => {
290290 expect ( screen . getByText ( "Please enter a value" ) ) . toBeInTheDocument ( ) ;
291291 } ) ;
292292 } ) ;
293+
294+ test ( "validate only if field is not empty" , async ( ) => {
295+ render (
296+ < Form
297+ children = { [
298+ {
299+ name : "TextInput" ,
300+ properties : {
301+ label : "Regex test" ,
302+ validator : {
303+ regex : / ^ \d { 5 } $ / ,
304+ regexError : "Please enter a valid 5-digit zip code" ,
305+ } ,
306+ } ,
307+ } ,
308+ {
309+ name : "Button" ,
310+ properties : {
311+ label : "Submit" ,
312+ submitForm : true ,
313+ } ,
314+ } ,
315+ ] }
316+ id = "form"
317+ /> ,
318+ {
319+ wrapper : FormTestWrapper ,
320+ } ,
321+ ) ;
322+
323+ const validateBtn = screen . getByText ( "Submit" ) ;
324+ fireEvent . click ( validateBtn ) ;
325+
326+ await waitFor ( ( ) => {
327+ expect (
328+ screen . queryByText ( "Please enter a valid 5-digit zip code" ) ,
329+ ) . not . toBeInTheDocument ( ) ;
330+ } ) ;
331+
332+ const input = screen . getByLabelText ( "Regex test" ) ;
333+ fireEvent . change ( input , { target : { value : "12" } } ) ;
334+ fireEvent . click ( validateBtn ) ;
335+
336+ await waitFor ( ( ) => {
337+ expect (
338+ screen . queryByText ( "Please enter a valid 5-digit zip code" ) ,
339+ ) . toBeInTheDocument ( ) ;
340+ } ) ;
341+
342+ fireEvent . change ( input , { target : { value : "" } } ) ;
343+ fireEvent . click ( validateBtn ) ;
344+
345+ await waitFor ( ( ) => {
346+ expect (
347+ screen . queryByText ( "Please enter a valid 5-digit zip code" ) ,
348+ ) . not . toBeInTheDocument ( ) ;
349+ } ) ;
350+ } ) ;
293351} ) ;
294352/* eslint-enable react/no-children-prop */
You can’t perform that action at this time.
0 commit comments