1- import { text , note , isCancel } from '@clack/prompts' ;
21import { setTimeout } from 'node:timers/promises' ;
2+ import { isCancel , note , text } from '@clack/prompts' ;
33
44async function main ( ) {
5- console . clear ( ) ;
6-
7- // Example demonstrating the issue with initial value validation
8- const name = await text ( {
9- message : 'Enter your name (letters and spaces only)' ,
10- initialValue : 'John123' , // Invalid initial value with numbers
11- validate : ( value ) => {
12- if ( ! / ^ [ a - z A - Z \s ] + $ / . test ( value ) ) return 'Name can only contain letters and spaces' ;
13- return undefined ;
14- } ,
15- } ) ;
16-
17- if ( ! isCancel ( name ) ) {
18- note ( `Valid name: ${ name } ` , 'Success' ) ;
19- }
20-
21- await setTimeout ( 1000 ) ;
22-
23- // Example with a valid initial value for comparison
24- const validName = await text ( {
25- message : 'Enter another name (letters and spaces only)' ,
26- initialValue : 'John Doe' , // Valid initial value
27- validate : ( value ) => {
28- if ( ! / ^ [ a - z A - Z \s ] + $ / . test ( value ) ) return 'Name can only contain letters and spaces' ;
29- return undefined ;
30- } ,
31- } ) ;
32-
33- if ( ! isCancel ( validName ) ) {
34- note ( `Valid name: ${ validName } ` , 'Success' ) ;
35- }
36-
37- await setTimeout ( 1000 ) ;
38-
5+ console . clear ( ) ;
6+
7+ // Example demonstrating the issue with initial value validation
8+ const name = await text ( {
9+ message : 'Enter your name (letters and spaces only)' ,
10+ initialValue : 'John123' , // Invalid initial value with numbers
11+ validate : ( value ) => {
12+ if ( ! / ^ [ a - z A - Z \s ] + $ / . test ( value ) ) return 'Name can only contain letters and spaces' ;
13+ return undefined ;
14+ } ,
15+ } ) ;
16+
17+ if ( ! isCancel ( name ) ) {
18+ note ( `Valid name: ${ name } ` , 'Success' ) ;
19+ }
20+
21+ await setTimeout ( 1000 ) ;
22+
23+ // Example with a valid initial value for comparison
24+ const validName = await text ( {
25+ message : 'Enter another name (letters and spaces only)' ,
26+ initialValue : 'John Doe' , // Valid initial value
27+ validate : ( value ) => {
28+ if ( ! / ^ [ a - z A - Z \s ] + $ / . test ( value ) ) return 'Name can only contain letters and spaces' ;
29+ return undefined ;
30+ } ,
31+ } ) ;
32+
33+ if ( ! isCancel ( validName ) ) {
34+ note ( `Valid name: ${ validName } ` , 'Success' ) ;
35+ }
36+
37+ await setTimeout ( 1000 ) ;
3938}
4039
41- main ( ) . catch ( console . error ) ;
40+ main ( ) . catch ( console . error ) ;
0 commit comments