-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathButtonExample.windows.js
More file actions
418 lines (400 loc) · 12.6 KB
/
ButtonExample.windows.js
File metadata and controls
418 lines (400 loc) · 12.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
* @flow strict-local
*/
'use strict';
const {RNTesterThemeContext} = require('../../components/RNTesterTheme');
const React = require('react');
const {Alert, Button, StyleSheet, View} = require('react-native');
function onButtonPress(buttonName: string) {
Alert.alert(`Your application has been ${buttonName}!`);
}
exports.displayName = 'ButtonExample';
exports.framework = 'React';
exports.category = 'UI';
exports.title = 'Button';
exports.documentationURL = 'https://reactnative.dev/docs/button';
exports.description = 'Simple React Native button component.';
exports.examples = [
{
title: 'Button with default styling',
render: function (): React.Node {
return (
<Button
onPress={() => onButtonPress('submitted')}
testID="button_default_styling"
title="Submit Application"
accessibilityLabel="Press to submit your application!"
/>
);
},
},
{
title: 'Button with color="red"',
description:
('Note: On iOS, the color prop controls the color of the text. On ' +
'Android, the color adjusts the background color of the button.': string),
render: function (): React.Node {
return (
<RNTesterThemeContext.Consumer>
{theme => {
return (
<Button
onPress={() => onButtonPress('cancelled')}
testID="cancel_button"
color={theme.SystemRedColor}
title="Cancel Application"
accessibilityLabel="Press to cancel your application!"
/>
);
}}
</RNTesterThemeContext.Consumer>
);
},
},
{
title: 'Two Buttons with Flexbox layout',
description:
('Two buttons wrapped inside view with justifyContent: spaceBetween,' +
'This layout strategy lets the title define the width of the button': string),
render: function (): React.Node {
return (
<RNTesterThemeContext.Consumer>
{theme => {
return (
<View
style={styles.container}
testID="two_button_container"
accessible>
<Button
onPress={() => onButtonPress('cancelled')}
testID="two_cancel_button"
color={theme.SystemRedColor}
title="Cancel"
accessibilityLabel="Press to cancel your application!"
/>
<Button
onPress={() => onButtonPress('submitted')}
testID="two_submit_button"
color={theme.SystemGreenColor}
title="Submit"
accessibilityLabel="Press to submit your application!"
/>
</View>
);
}}
</RNTesterThemeContext.Consumer>
);
},
},
{
title: 'Three Buttons with Flexbox layout',
render: function (): React.Node {
return (
<RNTesterThemeContext.Consumer>
{theme => {
return (
<View
style={styles.container}
testID="three_button_container"
accessible>
<Button
onPress={() => onButtonPress('cancelled')}
testID="three_cancel_button"
color={theme.SystemRedColor}
title="Cancel"
accessibilityLabel="Press to cancel your application!"
/>
<Button
onPress={() => onButtonPress('saved')}
testID="three_save_button"
color={theme.LinkColor}
title="Save For Later"
accessibilityLabel="Press to save your application!"
/>
<Button
onPress={() => onButtonPress('submitted')}
testID="three_submit_button"
color={theme.SystemGreenColor}
title="Submit"
accessibilityLabel="Press to submit your application!"
/>
</View>
);
}}
</RNTesterThemeContext.Consumer>
);
},
},
{
title: 'Button with disabled={true}',
description:
'By passing disabled={true} all interactions for the button are disabled.',
render: function (): React.Node {
return (
<RNTesterThemeContext.Consumer>
{theme => {
return (
<Button
disabled
onPress={() => onButtonPress('submitted')}
color={theme.LinkColor}
testID="disabled_button"
title="Submit Application"
accessibilityLabel="Press to submit your application!"
/>
);
}}
</RNTesterThemeContext.Consumer>
);
},
},
{
title: 'Button with accessibilityLabel="label"',
description: ('Note: This prop changes the text that a screen ' +
'reader announces (there are no visual differences).': string),
render: function (): React.Node {
return (
<RNTesterThemeContext.Consumer>
{theme => {
return (
<Button
onPress={() => onButtonPress('submitted')}
testID="accessibilityLabel_button"
color={theme.LinkColor}
title="Submit Application"
accessibilityLabel="Press to submit your application!"
/>
);
}}
</RNTesterThemeContext.Consumer>
);
},
},
{
title: 'Button with accessibilityState={{disabled: true}}',
description:
('Note: This prop will announce on TalkBack that the button is disabled. ' +
'The "disabled" prop has higher precedence on the state of the component': string),
render: function (): React.Node {
return (
<RNTesterThemeContext.Consumer>
{theme => {
return (
<Button
accessibilityState={{disabled: true}}
onPress={() => onButtonPress('submitted')}
testID="accessibilityState_button"
color={theme.LinkColor}
title="Submit Application"
accessibilityLabel="Press to submit your application!"
/>
);
}}
</RNTesterThemeContext.Consumer>
);
},
},
{
title: 'Button with Accessibility Props',
render: function (): React.Node {
return (
<RNTesterThemeContext.Consumer>
{theme => {
return (
<Button
onPress={() => onButtonPress('submitted')}
testID="accessibility_props"
title="Submit Application"
accessibilityLabel="Press to submit your application!"
accessibilityRole="button"
accessibilityHint="Submit Button"
accessibilityPosInSet={1}
accessibilitySetSize={1}
accessibilityLiveRegion="assertive"
accessibilityValue={{Text: 'Submit Application'}}
/>
);
}}
</RNTesterThemeContext.Consumer>
);
},
},
{
title: 'Button with Accessible and Focusable Prop',
render: function (): React.Node {
return (
<RNTesterThemeContext.Consumer>
{theme => {
return (
<View testID="accessible_focusable_button">
<Button
onPress={() => onButtonPress('submitted')}
testID="default_button"
title="Default Button"
accessibilityLabel="Press to submit your application!"
/>
<Button
onPress={() => onButtonPress('submitted')}
testID="accessible_false_button"
title="Button with accessible=false"
accessibilityLabel="Press to submit your application!"
accessible={false}
/>
<Button
onPress={() => onButtonPress('submitted')}
testID="focusable_false_button"
title="Button with focusable=false"
accessibilityLabel="Press to submit your application!"
focusable={false}
/>
<Button
onPress={() => onButtonPress('submitted')}
testID="accessible_focusable_false_button"
title="Button with accessible=false and focusable=false"
accessibilityLabel="Press to submit your application!"
accessible={false}
focusable={false}
/>
</View>
);
}}
</RNTesterThemeContext.Consumer>
);
},
},
{
title: 'Button with dynamic text',
description: 'Button text updates when pressed',
render: function (): React.Node {
return <DynamicTextButton />;
},
},
{
title: 'Button with dynamic color',
description: 'Button color updates when pressed',
render: function (): React.Node {
return <DynamicColorButton />;
},
},
{
title: 'Button with dynamic disabled state',
description: 'Button disabled state toggles when pressed',
render: function (): React.Node {
return <DynamicDisabledButton />;
},
},
{
title: 'Button with dynamic styling on press',
description: 'Button updates styling when pressed',
render: function (): React.Node {
return <DynamicStyleButton />;
},
},
];
// Dynamic Button Components for fast refresh testing
function DynamicTextButton(): React.Node {
const [buttonText, setButtonText] = React.useState('Initial Text');
const [pressCount, setPressCount] = React.useState(0);
const onPress = () => {
const newCount = pressCount + 1;
setPressCount(newCount);
setButtonText(`Pressed ${newCount} times`);
};
return (
<Button
onPress={onPress}
testID="dynamic_text_button"
title={buttonText}
accessibilityLabel="Press to change button text"
/>
);
}
function DynamicColorButton(): React.Node {
const [colorIndex, setColorIndex] = React.useState(0);
const colors = ['#007AFF', '#FF3B30', '#34C759', '#FF9500', '#5856D6'];
const onPress = () => {
setColorIndex((prev) => (prev + 1) % colors.length);
};
return (
<RNTesterThemeContext.Consumer>
{theme => (
<Button
onPress={onPress}
testID="dynamic_color_button"
color={colors[colorIndex]}
title="Change Color"
accessibilityLabel="Press to change button color"
/>
)}
</RNTesterThemeContext.Consumer>
);
}
function DynamicDisabledButton(): React.Node {
const [isDisabled, setIsDisabled] = React.useState(false);
const [toggleText, setToggleText] = React.useState('Disable Me');
const onPress = () => {
if (!isDisabled) {
setIsDisabled(true);
setToggleText('Disabled');
// Re-enable after 2 seconds for testing
setTimeout(() => {
setIsDisabled(false);
setToggleText('Disable Me');
}, 2000);
}
};
return (
<Button
disabled={isDisabled}
onPress={onPress}
testID="dynamic_disabled_button"
title={toggleText}
accessibilityLabel="Press to toggle disabled state"
/>
);
}
function DynamicStyleButton(): React.Node {
const [isPressed, setIsPressed] = React.useState(false);
const [pressCount, setPressCount] = React.useState(0);
const onPress = () => {
setIsPressed(true);
setPressCount(prev => prev + 1);
// Reset pressed state after visual feedback
setTimeout(() => setIsPressed(false), 300);
};
return (
<RNTesterThemeContext.Consumer>
{theme => (
<View style={[styles.dynamicContainer, isPressed && styles.pressedContainer]} testID="dynamic_style_container">
<Button
onPress={onPress}
testID="dynamic_style_button"
color={isPressed ? theme.SystemRedColor : theme.LinkColor}
title={`Style Button (${pressCount})`}
accessibilityLabel="Press to update styling"
/>
</View>
)}
</RNTesterThemeContext.Consumer>
);
}
const styles = StyleSheet.create({
container: {
flexDirection: 'row',
justifyContent: 'space-between',
},
dynamicContainer: {
padding: 10,
borderRadius: 5,
backgroundColor: 'transparent',
},
pressedContainer: {
backgroundColor: '#f0f0f0',
},
});