-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathedit.native.js
486 lines (439 loc) · 12 KB
/
edit.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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
/**
* External dependencies
*/
import { View, AccessibilityInfo } from 'react-native';
import classnames from 'classnames';
/**
* WordPress dependencies
*/
import {
RichText,
PlainText,
useBlockProps,
InspectorControls,
} from '@wordpress/block-editor';
import {
PanelBody,
SelectControl,
ToggleControl,
Icon,
} from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { search } from '@wordpress/icons';
import { useRef, useEffect, useState } from '@wordpress/element';
import { usePreferredColorSchemeStyle } from '@wordpress/compose';
/**
* Internal dependencies
*/
import styles from './style.scss';
/**
* Constants
*/
const MIN_BUTTON_WIDTH = 75;
const MARGINS =
styles.widthMargin?.marginLeft + styles.widthMargin?.paddingLeft;
const BUTTON_OPTIONS = [
{ value: 'button-inside', label: __( 'Button inside' ) },
{ value: 'button-outside', label: __( 'Button outside' ) },
{ value: 'no-button', label: __( 'No button' ) },
];
function useIsScreenReaderEnabled() {
const [ isScreenReaderEnabled, setIsScreenReaderEnabled ] =
useState( false );
useEffect( () => {
let mounted = true;
const changeListener = AccessibilityInfo.addEventListener(
'screenReaderChanged',
( enabled ) => setIsScreenReaderEnabled( enabled )
);
AccessibilityInfo.isScreenReaderEnabled().then(
( screenReaderEnabled ) => {
if ( mounted && screenReaderEnabled ) {
setIsScreenReaderEnabled( screenReaderEnabled );
}
}
);
return () => {
mounted = false;
changeListener.remove();
};
}, [] );
return isScreenReaderEnabled;
}
export default function SearchEdit( {
onFocus,
isSelected,
attributes,
setAttributes,
className,
blockWidth,
style,
} ) {
const [ isButtonSelected, setIsButtonSelected ] = useState( false );
const [ isLabelSelected, setIsLabelSelected ] = useState( false );
const [ isPlaceholderSelected, setIsPlaceholderSelected ] =
useState( false );
const [ isLongButton, setIsLongButton ] = useState( false );
const [ buttonWidth, setButtonWidth ] = useState( MIN_BUTTON_WIDTH );
const isScreenReaderEnabled = useIsScreenReaderEnabled();
const textInputRef = useRef( null );
const {
label,
showLabel,
buttonPosition,
buttonUseIcon,
placeholder,
buttonText,
} = attributes;
/*
* Called when the value of isSelected changes. Blurs the PlainText component
* used by the placeholder when this block loses focus.
*/
useEffect( () => {
if ( hasTextInput() && isPlaceholderSelected && ! isSelected ) {
textInputRef.current.blur();
}
}, [ isSelected ] );
useEffect( () => {
const maxButtonWidth = Math.floor( blockWidth / 2 - MARGINS );
const tempIsLongButton = buttonWidth > maxButtonWidth;
// Update this value only if it has changed to avoid flickering.
if ( isLongButton !== tempIsLongButton ) {
setIsLongButton( tempIsLongButton );
}
}, [ blockWidth, buttonWidth ] );
const hasTextInput = () => {
return textInputRef && textInputRef.current;
};
const onLayoutButton = ( { nativeEvent } ) => {
const { width } = nativeEvent?.layout;
if ( width ) {
setButtonWidth( width );
}
};
const getBlockClassNames = () => {
return classnames(
className,
'button-inside' === buttonPosition
? 'wp-block-search__button-inside'
: undefined,
'button-outside' === buttonPosition
? 'wp-block-search__button-outside'
: undefined,
'no-button' === buttonPosition
? 'wp-block-search__no-button'
: undefined,
'button-only' === buttonPosition
? 'wp-block-search__button-only'
: undefined,
! buttonUseIcon && 'no-button' !== buttonPosition
? 'wp-block-search__text-button'
: undefined,
buttonUseIcon && 'no-button' !== buttonPosition
? 'wp-block-search__icon-button'
: undefined
);
};
const getSelectedButtonPositionLabel = ( option ) => {
switch ( option ) {
case 'button-inside':
return __( 'Inside' );
case 'button-outside':
return __( 'Outside' );
case 'no-button':
return __( 'No button' );
}
};
const blockProps = useBlockProps( {
className: getBlockClassNames(),
} );
const controls = (
<InspectorControls>
<PanelBody title={ __( 'Search settings' ) }>
<ToggleControl
label={ __( 'Hide search heading' ) }
checked={ ! showLabel }
onChange={ () => {
setAttributes( {
showLabel: ! showLabel,
} );
} }
/>
<SelectControl
label={ __( 'Button position' ) }
value={ getSelectedButtonPositionLabel( buttonPosition ) }
onChange={ ( position ) => {
setAttributes( {
buttonPosition: position,
} );
} }
options={ BUTTON_OPTIONS }
hideCancelButton={ true }
/>
{ buttonPosition !== 'no-button' && (
<ToggleControl
label={ __( 'Use icon button' ) }
checked={ buttonUseIcon }
onChange={ () => {
setAttributes( {
buttonUseIcon: ! buttonUseIcon,
} );
} }
/>
) }
</PanelBody>
</InspectorControls>
);
const isButtonInside = buttonPosition === 'button-inside';
const borderStyle = usePreferredColorSchemeStyle(
styles.border,
styles.borderDark
);
const inputStyle = [
! isButtonInside && borderStyle,
usePreferredColorSchemeStyle(
styles.plainTextInput,
styles.plainTextInputDark
),
style?.baseColors?.color && { color: style?.baseColors?.color?.text },
];
const placeholderStyle = {
...usePreferredColorSchemeStyle(
styles.plainTextPlaceholder,
styles.plainTextPlaceholderDark
),
...( style?.baseColors?.color && {
color: style?.baseColors?.color?.text,
} ),
};
const searchBarStyle = [
styles.searchBarContainer,
isButtonInside && borderStyle,
isLongButton && { flexDirection: 'column' },
];
/**
* If a screenreader is enabled, create a descriptive label for this field. If
* not, return a label that is used during automated UI tests.
*
* @return {string} The accessibilityLabel for the Search Button
*/
const getAccessibilityLabelForButton = () => {
if ( ! isScreenReaderEnabled ) {
return 'search-block-button';
}
return `${ __(
'Search button. Current button text is'
) } ${ buttonText }`;
};
/**
* If a screenreader is enabled, create a descriptive label for this field. If
* not, return a label that is used during automated UI tests.
*
* @return {string} The accessibilityLabel for the Search Input
* placeholder field.
*/
const getAccessibilityLabelForPlaceholder = () => {
if ( ! isScreenReaderEnabled ) {
return 'search-block-input';
}
const title = __( 'Search input field.' );
const description = placeholder
? `${ __( 'Current placeholder text is' ) } ${ placeholder }`
: __( 'No custom placeholder set' );
return `${ title } ${ description }`;
};
/**
* If a screenreader is enabled, create a descriptive label for this field. If
* not, return a label that is used during automated UI tests.
*
* @return {string} The accessibilityLabel for the Search Label field
*/
const getAccessibilityLabelForLabel = () => {
if ( ! isScreenReaderEnabled ) {
return 'search-block-label';
}
return `${ __( 'Search block label. Current text is' ) } ${ label }`;
};
const renderTextField = () => {
return (
<View
style={ styles.searchInputContainer }
accessible={ true }
accessibilityRole="none"
accessibilityHint={
isScreenReaderEnabled
? __( 'Double tap to edit placeholder text' )
: undefined
}
accessibilityLabel={ getAccessibilityLabelForPlaceholder() }
>
<PlainText
ref={ textInputRef }
isSelected={ isPlaceholderSelected }
className="wp-block-search__input"
style={ inputStyle }
numberOfLines={ 1 }
ellipsizeMode="tail" // Currently only works on ios.
label={ null }
value={ placeholder }
placeholder={
placeholder ? undefined : __( 'Optional placeholder…' )
}
onChange={ ( newVal ) =>
setAttributes( { placeholder: newVal } )
}
onFocus={ () => {
setIsPlaceholderSelected( true );
onFocus();
} }
onBlur={ () => setIsPlaceholderSelected( false ) }
placeholderTextColor={ placeholderStyle?.color }
/>
</View>
);
};
// To achieve proper expanding and shrinking `RichText` on Android, there is a need to set
// a `placeholder` as an empty string when `RichText` is focused,
// because `AztecView` is calculating a `minWidth` based on placeholder text.
const buttonPlaceholderText =
isButtonSelected ||
( ! isButtonSelected && buttonText && buttonText !== '' )
? ''
: __( 'Add button text' );
const baseButtonStyles = {
...style?.baseColors?.blocks?.[ 'core/button' ]?.color,
...attributes?.style?.color,
...( style?.color && { text: style.color } ),
};
const richTextButtonContainerStyle = [
styles.buttonContainer,
isLongButton && styles.buttonContainerWide,
baseButtonStyles?.background && {
backgroundColor: baseButtonStyles.background,
borderWidth: 0,
},
style?.backgroundColor && {
backgroundColor: style.backgroundColor,
borderWidth: 0,
},
];
const richTextButtonStyle = {
...styles.richTextButton,
...( baseButtonStyles?.text && {
color: baseButtonStyles.text,
placeholderColor: baseButtonStyles.text,
} ),
};
const iconStyles = {
...styles.icon,
...( baseButtonStyles?.text && { fill: baseButtonStyles.text } ),
};
const renderButton = () => {
return (
<View style={ richTextButtonContainerStyle }>
{ buttonUseIcon && (
<Icon
icon={ search }
{ ...iconStyles }
onLayout={ onLayoutButton }
/>
) }
{ ! buttonUseIcon && (
<View
accessible={ true }
accessibilityRole="none"
accessibilityHint={
isScreenReaderEnabled
? __( 'Double tap to edit button text' )
: undefined
}
accessibilityLabel={ getAccessibilityLabelForButton() }
onLayout={ onLayoutButton }
>
<RichText
className="wp-block-search__button"
identifier="text"
tagName="p"
style={ richTextButtonStyle }
placeholder={ buttonPlaceholderText }
value={ buttonText }
withoutInteractiveFormatting
onChange={ ( html ) =>
setAttributes( { buttonText: html } )
}
minWidth={ MIN_BUTTON_WIDTH }
maxWidth={ blockWidth - MARGINS }
textAlign="center"
isSelected={ isButtonSelected }
__unstableMobileNoFocusOnMount={ ! isSelected }
unstableOnFocus={ () => {
setIsButtonSelected( true );
} }
onBlur={ () => {
setIsButtonSelected( false );
} }
selectionColor={
styles.richTextButtonCursor?.color
}
/>
</View>
) }
</View>
);
};
return (
<View
{ ...blockProps }
style={ styles.searchBlockContainer }
importantForAccessibility={
isSelected ? 'yes' : 'no-hide-descendants'
}
accessibilityElementsHidden={ isSelected ? false : true }
>
{ isSelected && controls }
{ showLabel && (
<View
accessible={ true }
accessibilityRole="none"
accessibilityHint={
isScreenReaderEnabled
? __( 'Double tap to edit label text' )
: undefined
}
accessibilityLabel={ getAccessibilityLabelForLabel() }
>
<RichText
className="wp-block-search__label"
identifier="text"
tagName="p"
style={ styles.richTextLabel }
placeholder={ __( 'Add label…' ) }
withoutInteractiveFormatting
value={ label }
onChange={ ( html ) =>
setAttributes( { label: html } )
}
isSelected={ isLabelSelected }
__unstableMobileNoFocusOnMount={ ! isSelected }
unstableOnFocus={ () => {
setIsLabelSelected( true );
} }
onBlur={ () => {
setIsLabelSelected( false );
} }
selectionColor={ styles.richTextButtonCursor?.color }
/>
</View>
) }
{ ( 'button-inside' === buttonPosition ||
'button-outside' === buttonPosition ) && (
<View style={ searchBarStyle }>
{ renderTextField() }
{ renderButton() }
</View>
) }
{ 'button-only' === buttonPosition && renderButton() }
{ 'no-button' === buttonPosition && renderTextField() }
</View>
);
}