10
10
11
11
import * as React from 'react' ;
12
12
import type { RNTesterModule } from '../../types/RNTesterTypes' ;
13
- import { Alert , StyleSheet , Text , TouchableHighlight , View } from 'react-native' ;
13
+ import { Alert , StyleSheet , Text , Pressable , View } from 'react-native' ;
14
14
15
15
// Shows log on the screen
16
16
const Log = ( { message} : { message : string } ) =>
@@ -31,14 +31,14 @@ const AlertWithDefaultButton = () => {
31
31
32
32
return (
33
33
< View >
34
- < TouchableHighlight
34
+ < Pressable
35
35
testID = "alert-with-default-button"
36
36
style = { styles . wrapper }
37
37
onPress = { ( ) => Alert . alert ( 'Alert' , alertMessage ) } >
38
38
< View style = { styles . button } >
39
39
< Text > Tap to view alert</ Text >
40
40
</ View >
41
- </ TouchableHighlight >
41
+ </ Pressable >
42
42
</ View >
43
43
) ;
44
44
} ;
@@ -50,7 +50,7 @@ const AlertWithTwoButtons = () => {
50
50
51
51
return (
52
52
< View >
53
- < TouchableHighlight
53
+ < Pressable
54
54
style = { styles . wrapper }
55
55
onPress = { ( ) =>
56
56
Alert . alert ( 'Action Required!' , alertMessage , [
@@ -61,7 +61,7 @@ const AlertWithTwoButtons = () => {
61
61
< View style = { styles . button } >
62
62
< Text > Tap to view alert</ Text >
63
63
</ View >
64
- </ TouchableHighlight >
64
+ </ Pressable >
65
65
< Log message = { message } />
66
66
</ View >
67
67
) ;
@@ -74,7 +74,7 @@ const AlertWithThreeButtons = () => {
74
74
75
75
return (
76
76
< View >
77
- < TouchableHighlight
77
+ < Pressable
78
78
testID = "alert-with-three-buttons"
79
79
style = { styles . wrapper }
80
80
onPress = { ( ) =>
@@ -87,7 +87,7 @@ const AlertWithThreeButtons = () => {
87
87
< View style = { styles . button } >
88
88
< Text > Tap to view alert</ Text >
89
89
</ View >
90
- </ TouchableHighlight >
90
+ </ Pressable >
91
91
< Log message = { message } />
92
92
</ View >
93
93
) ;
@@ -102,7 +102,7 @@ const AlertWithManyButtons = () => {
102
102
103
103
return (
104
104
< View >
105
- < TouchableHighlight
105
+ < Pressable
106
106
style = { styles . wrapper }
107
107
onPress = { ( ) =>
108
108
Alert . alert (
@@ -117,7 +117,7 @@ const AlertWithManyButtons = () => {
117
117
< View style = { styles . button } >
118
118
< Text > Tap to view alert</ Text >
119
119
</ View >
120
- </ TouchableHighlight >
120
+ </ Pressable >
121
121
< Log message = { message } />
122
122
</ View >
123
123
) ;
@@ -130,7 +130,7 @@ const AlertWithCancelableTrue = () => {
130
130
131
131
return (
132
132
< View >
133
- < TouchableHighlight
133
+ < Pressable
134
134
style = { styles . wrapper }
135
135
onPress = { ( ) =>
136
136
Alert . alert (
@@ -149,7 +149,7 @@ const AlertWithCancelableTrue = () => {
149
149
< View style = { styles . button } >
150
150
< Text > Tap to view alert</ Text >
151
151
</ View >
152
- </ TouchableHighlight >
152
+ </ Pressable >
153
153
< Log message = { message } />
154
154
</ View >
155
155
) ;
@@ -162,7 +162,7 @@ const AlertWithStyles = () => {
162
162
163
163
return (
164
164
< View >
165
- < TouchableHighlight
165
+ < Pressable
166
166
style = { styles . wrapper }
167
167
onPress = { ( ) =>
168
168
Alert . alert ( 'Styled Buttons!' , alertMessage , [
@@ -186,7 +186,7 @@ const AlertWithStyles = () => {
186
186
< View style = { styles . button } >
187
187
< Text > Tap to view alert</ Text >
188
188
</ View >
189
- </ TouchableHighlight >
189
+ </ Pressable >
190
190
< Log message = { message } />
191
191
</ View >
192
192
) ;
@@ -200,7 +200,7 @@ const AlertWithStylesPreferred = () => {
200
200
201
201
return (
202
202
< View >
203
- < TouchableHighlight
203
+ < Pressable
204
204
style = { styles . wrapper }
205
205
onPress = { ( ) =>
206
206
Alert . alert ( 'Foo Title' , alertMessage , [
@@ -219,12 +219,132 @@ const AlertWithStylesPreferred = () => {
219
219
< View style = { styles . button } >
220
220
< Text > Tap to view alert</ Text >
221
221
</ View >
222
- </ TouchableHighlight >
222
+ </ Pressable >
223
223
< Log message = { message } />
224
224
</ View >
225
225
) ;
226
226
} ;
227
227
228
+ const PromptOptions = ( ) => {
229
+ const [ promptValue , setPromptValue ] = React . useState < string > ( '' ) ;
230
+
231
+ const customButtons = [
232
+ {
233
+ text : 'Custom OK' ,
234
+ onPress : setPromptValue ,
235
+ } ,
236
+ {
237
+ text : 'Custom Cancel' ,
238
+ style : 'cancel' ,
239
+ } ,
240
+ ] ;
241
+
242
+ return (
243
+ < View >
244
+ < Text style = { styles . promptValue } >
245
+ < Text style = { styles . bold } > Prompt value:</ Text > { promptValue }
246
+ </ Text >
247
+
248
+ < Pressable
249
+ style = { styles . wrapper }
250
+ onPress = { ( ) => Alert . prompt ( 'Type a value' , null , setPromptValue ) } >
251
+ < View style = { styles . button } >
252
+ < Text > prompt with title & callback </ Text >
253
+ </ View >
254
+ </ Pressable >
255
+
256
+ < Pressable
257
+ style = { styles . wrapper }
258
+ onPress = { ( ) => Alert . prompt ( 'Type a value' , null , customButtons ) } >
259
+ < View style = { styles . button } >
260
+ < Text > prompt with title & custom buttons </ Text >
261
+ </ View >
262
+ </ Pressable >
263
+
264
+ < Pressable
265
+ style = { styles . wrapper }
266
+ onPress = { ( ) =>
267
+ Alert . prompt (
268
+ 'Type a phone number' ,
269
+ null ,
270
+ null ,
271
+ 'plain-text' ,
272
+ undefined ,
273
+ 'phone-pad' ,
274
+ )
275
+ } >
276
+ < View style = { styles . button } >
277
+ < Text > prompt with title & custom keyboard </ Text >
278
+ </ View >
279
+ </ Pressable >
280
+
281
+ < Pressable
282
+ style = { styles . wrapper }
283
+ onPress = { ( ) =>
284
+ Alert . prompt (
285
+ 'Type a value' ,
286
+ null ,
287
+ setPromptValue ,
288
+ undefined ,
289
+ 'Default value' ,
290
+ )
291
+ } >
292
+ < View style = { styles . button } >
293
+ < Text > prompt with title, callback & default value </ Text >
294
+ </ View >
295
+ </ Pressable >
296
+
297
+ < Pressable
298
+ style = { styles . wrapper }
299
+ onPress = { ( ) =>
300
+ Alert . prompt (
301
+ 'Type a value' ,
302
+ null ,
303
+ customButtons ,
304
+ 'login-password' ,
305
+
306
+ )
307
+ } >
308
+ < View style = { styles . button } >
309
+ < Text >
310
+ prompt with title, custom buttons, login/password & default value
311
+ </ Text >
312
+ </ View >
313
+ </ Pressable >
314
+ </ View >
315
+ ) ;
316
+ } ;
317
+
318
+ const PromptTypes = ( ) => {
319
+ return (
320
+ < View >
321
+ < Pressable
322
+ style = { styles . wrapper }
323
+ onPress = { ( ) => Alert . prompt ( 'Plain Text Entry' ) } >
324
+ < View style = { styles . button } >
325
+ < Text > plain-text</ Text >
326
+ </ View >
327
+ </ Pressable >
328
+ < Pressable
329
+ style = { styles . wrapper }
330
+ onPress = { ( ) => Alert . prompt ( 'Secure Text' , null , null , 'secure-text' ) } >
331
+ < View style = { styles . button } >
332
+ < Text > secure-text</ Text >
333
+ </ View >
334
+ </ Pressable >
335
+ < Pressable
336
+ style = { styles . wrapper }
337
+ onPress = { ( ) =>
338
+ Alert . prompt ( 'Login & Password' , null , null , 'login-password' )
339
+ } >
340
+ < View style = { styles . button } >
341
+ < Text > login-password</ Text >
342
+ </ View >
343
+ </ Pressable >
344
+ </ View >
345
+ ) ;
346
+ } ;
347
+
228
348
const styles = StyleSheet . create ( {
229
349
wrapper : {
230
350
borderRadius : 5 ,
@@ -241,6 +361,9 @@ const styles = StyleSheet.create({
241
361
bold : {
242
362
fontWeight : 'bold' ,
243
363
} ,
364
+ promptValue : {
365
+ marginBottom : 10 ,
366
+ } ,
244
367
} ) ;
245
368
246
369
export const examples = [
@@ -301,6 +424,20 @@ export const examples = [
301
424
return < AlertWithStylesPreferred /> ;
302
425
} ,
303
426
} ,
427
+ {
428
+ title : 'Prompt Options' ,
429
+ platform : 'ios' ,
430
+ render ( ) : React . Node {
431
+ return < PromptOptions /> ;
432
+ } ,
433
+ } ,
434
+ {
435
+ title : 'Prompt Types' ,
436
+ platform : 'ios' ,
437
+ render ( ) : React . Node {
438
+ return < PromptTypes /> ;
439
+ } ,
440
+ } ,
304
441
] ;
305
442
306
443
export default ( {
@@ -310,6 +447,5 @@ export default ({
310
447
documentationURL : 'https://reactnative.dev/docs/alert' ,
311
448
description :
312
449
'Alerts display a concise and informative message and prompt the user to make a decision.' ,
313
- showIndividualExamples : true ,
314
450
examples,
315
451
} : RNTesterModule ) ;
0 commit comments