-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
cell.native.js
434 lines (405 loc) · 10.6 KB
/
cell.native.js
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
/**
* External dependencies
*/
import {
TouchableOpacity,
Text,
View,
TextInput,
I18nManager,
AccessibilityInfo,
Platform,
} from 'react-native';
import { isEmpty, get } from 'lodash';
/**
* WordPress dependencies
*/
import { Icon } from '@wordpress/components';
import { check } from '@wordpress/icons';
import { Component } from '@wordpress/element';
import { __, _x, sprintf } from '@wordpress/i18n';
import { withPreferredColorScheme } from '@wordpress/compose';
/**
* Internal dependencies
*/
import styles from './styles.scss';
import platformStyles from './cellStyles.scss';
import TouchableRipple from './ripple';
const isIOS = Platform.OS === 'ios';
class BottomSheetCell extends Component {
constructor( props ) {
super( ...arguments );
this.state = {
isEditingValue: props.autoFocus || false,
isScreenReaderEnabled: false,
};
this.handleScreenReaderToggled = this.handleScreenReaderToggled.bind(
this
);
this.isCurrent = false;
}
componentDidUpdate( prevProps, prevState ) {
if ( ! prevState.isEditingValue && this.state.isEditingValue ) {
this._valueTextInput.focus();
}
}
componentDidMount() {
this.isCurrent = true;
AccessibilityInfo.addEventListener(
'screenReaderChanged',
this.handleScreenReaderToggled
);
AccessibilityInfo.isScreenReaderEnabled().then(
( isScreenReaderEnabled ) => {
if ( this.isCurrent ) {
this.setState( { isScreenReaderEnabled } );
}
}
);
}
componentWillUnmount() {
this.isCurrent = false;
AccessibilityInfo.removeEventListener(
'screenReaderChanged',
this.handleScreenReaderToggled
);
}
handleScreenReaderToggled( isScreenReaderEnabled ) {
this.setState( { isScreenReaderEnabled } );
}
typeToKeyboardType( type, step ) {
let keyboardType = `default`;
if ( type === `number` ) {
if ( step && Math.abs( step ) < 1 ) {
keyboardType = `decimal-pad`;
} else {
keyboardType = `number-pad`;
}
}
return keyboardType;
}
render() {
const {
accessible,
accessibilityLabel,
accessibilityHint,
accessibilityRole,
disabled = false,
activeOpacity,
onPress,
onLongPress,
label,
subLabel,
value,
valuePlaceholder = '',
icon,
leftAlign,
iconStyle = {},
labelStyle = {},
valueStyle = {},
cellContainerStyle = {},
cellRowContainerStyle = {},
onChangeValue,
onSubmit,
children,
editable = true,
isSelected = false,
separatorType,
style = {},
getStylesFromColorScheme,
customActionButton,
type,
step,
borderless,
help,
...valueProps
} = this.props;
const showValue = value !== undefined;
const isValueEditable = editable && onChangeValue !== undefined;
const cellLabelStyle = getStylesFromColorScheme(
styles.cellLabel,
styles.cellTextDark
);
const cellLabelCenteredStyle = getStylesFromColorScheme(
styles.cellLabelCentered,
styles.cellTextDark
);
const cellLabelLeftAlignNoIconStyle = getStylesFromColorScheme(
styles.cellLabelLeftAlignNoIcon,
styles.cellTextDark
);
const defaultMissingIconAndValue = leftAlign
? cellLabelLeftAlignNoIconStyle
: cellLabelCenteredStyle;
const defaultLabelStyle =
showValue || customActionButton || icon
? cellLabelStyle
: defaultMissingIconAndValue;
const defaultSubLabelStyleText = getStylesFromColorScheme(
styles.cellSubLabelText,
styles.cellSubLabelTextDark
);
const drawSeparator =
( separatorType && separatorType !== 'none' ) ||
separatorStyle === undefined;
const drawTopSeparator =
drawSeparator && separatorType === 'topFullWidth';
const cellContainerStyles = [
styles.cellContainer,
cellContainerStyle,
];
const rowContainerStyles = [
styles.cellRowContainer,
cellRowContainerStyle,
];
const isInteractive =
isValueEditable ||
onPress !== undefined ||
onLongPress !== undefined;
const onCellPress = () => {
if ( isValueEditable ) {
startEditing();
} else if ( onPress !== undefined ) {
onPress();
}
};
const finishEditing = () => {
this.setState( { isEditingValue: false } );
};
const startEditing = () => {
if ( this.state.isEditingValue === false ) {
this.setState( { isEditingValue: true } );
}
};
const separatorStyle = () => {
//eslint-disable-next-line @wordpress/no-unused-vars-before-return
const defaultSeparatorStyle = this.props.getStylesFromColorScheme(
styles.separator,
styles.separatorDark
);
const cellSeparatorStyle = this.props.getStylesFromColorScheme(
styles.cellSeparator,
styles.cellSeparatorDark
);
const leftMarginStyle = {
...cellSeparatorStyle,
...platformStyles.separatorMarginLeft,
};
switch ( separatorType ) {
case 'leftMargin':
return leftMarginStyle;
case 'fullWidth':
case 'topFullWidth':
return defaultSeparatorStyle;
case 'none':
return undefined;
case undefined:
if ( showValue && icon ) {
return leftMarginStyle;
}
return defaultSeparatorStyle;
}
};
const getValueComponent = () => {
const styleRTL = I18nManager.isRTL && styles.cellValueRTL;
const cellValueStyle = this.props.getStylesFromColorScheme(
styles.cellValue,
styles.cellTextDark
);
const finalStyle = {
...cellValueStyle,
...valueStyle,
...styleRTL,
};
// To be able to show the `middle` ellipsizeMode on editable cells
// we show the TextInput just when the user wants to edit the value,
// and the Text component to display it.
// We also show the TextInput to display placeholder.
const shouldShowPlaceholder = isValueEditable && value === '';
return this.state.isEditingValue || shouldShowPlaceholder ? (
<TextInput
ref={ ( c ) => ( this._valueTextInput = c ) }
numberOfLines={ 1 }
style={ finalStyle }
value={ value }
placeholder={ valuePlaceholder }
placeholderTextColor={ '#87a6bc' }
onChangeText={ onChangeValue }
editable={ isValueEditable }
pointerEvents={
this.state.isEditingValue ? 'auto' : 'none'
}
onFocus={ startEditing }
onBlur={ finishEditing }
onSubmitEditing={ onSubmit }
keyboardType={ this.typeToKeyboardType( type, step ) }
{ ...valueProps }
/>
) : (
<Text
style={ { ...cellValueStyle, ...valueStyle } }
numberOfLines={ 1 }
ellipsizeMode={ 'middle' }
>
{ value }
</Text>
);
};
const getAccessibilityLabel = () => {
if ( accessible === false ) {
return;
}
if ( accessibilityLabel || ! showValue ) {
return accessibilityLabel || label;
}
if ( isEmpty( value ) ) {
return isEmpty( help )
? sprintf(
/* translators: accessibility text. Empty state of a inline textinput cell. %s: The cell's title */
_x( '%s. Empty', 'inline textinput cell' ),
label
)
: // Separating by ',' is necessary to make a pause on urls (non-capitalized text)
sprintf(
/* translators: accessibility text. Empty state of a inline textinput cell. %1: Cell title, %2: cell help. */
_x( '%1$s, %2$s. Empty', 'inline textinput cell' ),
label,
help
);
}
return isEmpty( help )
? sprintf(
/* translators: accessibility text. Inline textinput title and value.%1: Cell title, %2: cell value. */
_x( '%1$s, %2$s', 'inline textinput cell' ),
label,
value
) // Separating by ',' is necessary to make a pause on urls (non-capitalized text)
: sprintf(
/* translators: accessibility text. Inline textinput title, value and help text.%1: Cell title, %2: cell value, , %3: cell help. */
_x( '%1$s, %2$s, %3$s', 'inline textinput cell' ),
label,
value,
help
);
};
const iconStyleBase = getStylesFromColorScheme(
styles.icon,
styles.iconDark
);
const resetButtonStyle = getStylesFromColorScheme(
styles.resetButton,
styles.resetButtonDark
);
const cellHelpStyle = [
styles.cellHelpLabel,
isIOS && styles.cellHelpLabelIOS,
];
const containerPointerEvents =
this.state.isScreenReaderEnabled && accessible ? 'none' : 'auto';
const { title, handler } = customActionButton || {};
const opacity =
activeOpacity !== undefined
? activeOpacity
: get( platformStyles, 'activeOpacity.opacity' );
return (
<TouchableRipple
accessible={
accessible !== undefined
? accessible
: ! this.state.isEditingValue
}
accessibilityLabel={ getAccessibilityLabel() }
accessibilityRole={ accessibilityRole || 'button' }
accessibilityHint={
isValueEditable
? /* translators: accessibility text */
__( 'Double tap to edit this value' )
: accessibilityHint
}
disabled={ disabled || ! isInteractive }
activeOpacity={ opacity }
onPress={ onCellPress }
onLongPress={ onLongPress }
style={ [ styles.clipToBounds, style ] }
borderless={ borderless }
>
{ drawTopSeparator && <View style={ separatorStyle() } /> }
<View
style={ cellContainerStyles }
pointerEvents={ containerPointerEvents }
>
<View style={ rowContainerStyles }>
<View style={ styles.cellRowContainer }>
{ icon && (
<View style={ styles.cellRowContainer }>
<Icon
icon={ icon }
size={ 24 }
fill={
iconStyle.color ||
iconStyleBase.color
}
style={ iconStyle }
isPressed={ false }
/>
<View
style={
platformStyles.labelIconSeparator
}
/>
</View>
) }
{ subLabel && label && (
<View>
<Text
style={ [
defaultLabelStyle,
labelStyle,
] }
>
{ label }
</Text>
<Text style={ defaultSubLabelStyleText }>
{ subLabel }
</Text>
</View>
) }
{ ! subLabel && label && (
<Text
style={ [ defaultLabelStyle, labelStyle ] }
>
{ label }
</Text>
) }
</View>
{ customActionButton && (
<TouchableOpacity
onPress={ handler }
accessibilityRole={ 'button' }
>
<Text style={ resetButtonStyle }>
{ title }
</Text>
</TouchableOpacity>
) }
</View>
{ isSelected && (
<Icon
icon={ check }
fill={ platformStyles.isSelected.color }
/>
) }
{ showValue && getValueComponent() }
{ children }
</View>
{ help && (
<Text style={ [ cellHelpStyle, styles.placeholderColor ] }>
{ help }
</Text>
) }
{ ! drawTopSeparator && <View style={ separatorStyle() } /> }
</TouchableRipple>
);
}
}
export default withPreferredColorScheme( BottomSheetCell );