Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RNMobile]: Fix search block focus issues #29500

Merged
merged 44 commits into from
Mar 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
928c0cc
Add search block to inserter
AmandaRiu Feb 13, 2021
6267582
Add label and icon toggle toolbar buttons
AmandaRiu Feb 13, 2021
bcbce62
Add button position dropdown menu
AmandaRiu Feb 13, 2021
17dce2c
Add temporary alert to make toolbar testing easier
AmandaRiu Feb 13, 2021
5e71172
Add initial UI and basic styling
AmandaRiu Feb 16, 2021
9250fa8
Move text button to separate file
AmandaRiu Feb 16, 2021
9e47d94
Add basic button style
AmandaRiu Feb 16, 2021
4031bea
Add classnames and consolidate button code
AmandaRiu Feb 16, 2021
be7c748
Remove iconButtonOnly alert message
AmandaRiu Feb 16, 2021
d0486e4
Add platform-specific styles and basic styles
AmandaRiu Feb 16, 2021
d6a1f28
Remove flex settings for label
AmandaRiu Feb 16, 2021
2ca55bc
Add button position styles; remove temp alert
AmandaRiu Feb 16, 2021
58ba580
Clean up styles
AmandaRiu Feb 17, 2021
4ade08e
Merge branch 'rnmobile/3073-search-block-basic-ui' into rnmobile/3073…
AmandaRiu Feb 17, 2021
7fc1536
Consolidate border styling, fix auto-sizing
AmandaRiu Feb 17, 2021
6b808f4
Fix broken button style
AmandaRiu Feb 17, 2021
1c68106
Merge branch 'master' into rnmobile/3073-search-block-basic-ui
AmandaRiu Feb 23, 2021
4c0b598
Merge branch 'rnmobile/3073-search-block-basic-ui' into rnmobile/3073…
AmandaRiu Feb 23, 2021
269c81b
Move CSS_UNITs to shared utils file
AmandaRiu Feb 23, 2021
49ff276
Add basic settings panel with UnitControl
AmandaRiu Feb 23, 2021
514850e
Add event stubs and UnitControl
AmandaRiu Feb 23, 2021
275155e
Remove unused code and set defaults
AmandaRiu Feb 23, 2021
59c1668
Finish changeUnit handler and remove debugging
AmandaRiu Feb 23, 2021
5f29b89
Configure defaults, min and max widths
AmandaRiu Feb 23, 2021
254fad5
Merge branch 'trunk' into rnmobile/3073-search-block-button-position
AmandaRiu Mar 2, 2021
8eccd5c
Merge trunk into branch
AmandaRiu Mar 2, 2021
0f67f02
Merge branch 'trunk' into rnmobile/3073-search-block-settings
AmandaRiu Mar 2, 2021
3b45e81
Merge branch 'rnmobile/3073-search-block-button-position' into rnmobi…
AmandaRiu Mar 2, 2021
6dafd66
Merge branch 'trunk' into rnmobile/3073-search-block-settings
AmandaRiu Mar 2, 2021
ea22b5b
Fix focus issues on Search Block
AmandaRiu Mar 3, 2021
21b4764
Track the state of which text is selected
AmandaRiu Mar 6, 2021
a5eeb08
Only draw toolbar controls if block is selected
AmandaRiu Mar 6, 2021
becfd54
Set RichText identifiers and tagNames
AmandaRiu Mar 6, 2021
7fb83f6
Clear selection state when the block is unselected
AmandaRiu Mar 6, 2021
81d6c71
Merge branch 'master' into rnmobile/3073-search-block-basic-ui
AmandaRiu Mar 9, 2021
452356a
Make text lose focus when block loses focus
AmandaRiu Mar 11, 2021
0114540
Add check for textInput reference existance before manipulation
AmandaRiu Mar 15, 2021
120bf10
Rename textInput reference for clarity
AmandaRiu Mar 15, 2021
0639176
Ensure textInput loses focus when block loses focus
AmandaRiu Mar 15, 2021
ca49b6b
Update code comment to remove duplicate wording
AmandaRiu Mar 15, 2021
e2136b9
Merge branch 'trunk' into rnmobile/search-block-text-focus-bugfixes
AmandaRiu Mar 16, 2021
976d48e
Add blur method to the PlainText component
AmandaRiu Mar 16, 2021
38e65d9
Use PlainText instead of TextInput for placeholder
AmandaRiu Mar 16, 2021
d37bbdd
Focus the placeholder text when block first added
AmandaRiu Mar 16, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ export default class PlainText extends Component {
this._input.focus();
}

blur() {
this._input.blur();
}

render() {
return (
<TextInput
Expand Down
69 changes: 63 additions & 6 deletions packages/block-library/src/search/edit.native.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
/**
* External dependencies
*/
import { View, TextInput } from 'react-native';
import { View } from 'react-native';
import classnames from 'classnames';

/**
* WordPress dependencies
*/
import {
RichText,
PlainText,
BlockControls,
useBlockProps,
InspectorControls,
Expand All @@ -22,6 +23,7 @@ import {
} from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { search } from '@wordpress/icons';
import { useRef, useEffect, useState } from '@wordpress/element';

/**
* Internal dependencies
Expand All @@ -43,7 +45,21 @@ import {
*/
const MIN_BUTTON_WIDTH = 100;

export default function SearchEdit( { attributes, setAttributes, className } ) {
export default function SearchEdit( {
onFocus,
isSelected,
attributes,
setAttributes,
className,
} ) {
const [ isButtonSelected, setIsButtonSelected ] = useState( false );
const [ isLabelSelected, setIsLabelSelected ] = useState( false );
const [ isPlaceholderSelected, setIsPlaceholderSelected ] = useState(
true
);

const textInputRef = useRef( null );

const {
label,
showLabel,
Expand All @@ -55,6 +71,20 @@ export default function SearchEdit( { attributes, setAttributes, className } ) {
widthUnit = '%',
} = 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 ] );

const hasTextInput = () => {
return textInputRef && textInputRef.current;
};

const onChange = ( nextWidth ) => {
if ( isPercentageUnit( widthUnit ) || ! widthUnit ) {
return;
Expand Down Expand Up @@ -172,7 +202,9 @@ export default function SearchEdit( { attributes, setAttributes, className } ) {
: mergeWithBorderStyle( styles.searchTextInput );

return (
<TextInput
<PlainText
ref={ textInputRef }
isSelected={ isPlaceholderSelected }
className="wp-block-search__input"
style={ inputStyle }
numberOfLines={ 1 }
Expand All @@ -182,9 +214,14 @@ export default function SearchEdit( { attributes, setAttributes, className } ) {
placeholder={
placeholder ? undefined : __( 'Optional placeholder…' )
}
onChangeText={ ( newVal ) =>
onChange={ ( newVal ) =>
setAttributes( { placeholder: newVal } )
}
onFocus={ () => {
setIsPlaceholderSelected( true );
onFocus();
} }
onBlur={ () => setIsPlaceholderSelected( false ) }
/>
);
};
Expand All @@ -196,22 +233,32 @@ export default function SearchEdit( { attributes, setAttributes, className } ) {
<Button
className="wp-block-search__button"
icon={ search }
onFocus={ onFocus }
/>
) }

{ ! buttonUseIcon && (
<RichText
className="wp-block-search__button"
identifier="text"
tagName="p"
style={ richTextStyles.searchButton }
placeholder={ __( 'Add button text' ) }
value={ buttonText }
identifier="text"
withoutInteractiveFormatting
onChange={ ( html ) =>
setAttributes( { buttonText: html } )
}
minWidth={ MIN_BUTTON_WIDTH }
textAlign="center"
isSelected={ isButtonSelected }
__unstableMobileNoFocusOnMount={ ! isSelected }
unstableOnFocus={ () => {
setIsButtonSelected( true );
} }
onBlur={ () => {
setIsButtonSelected( false );
} }
/>
) }
</View>
Expand All @@ -225,11 +272,13 @@ export default function SearchEdit( { attributes, setAttributes, className } ) {

return (
<View { ...blockProps } style={ styles.searchBlockContainer }>
{ controls }
{ isSelected && controls }

{ showLabel && (
<RichText
className="wp-block-search__label"
identifier="text"
tagName="p"
style={ {
...styles.searchLabel,
...richTextStyles.searchLabel,
Expand All @@ -239,6 +288,14 @@ export default function SearchEdit( { attributes, setAttributes, className } ) {
withoutInteractiveFormatting
value={ label }
onChange={ ( html ) => setAttributes( { label: html } ) }
isSelected={ isLabelSelected }
__unstableMobileNoFocusOnMount={ ! isSelected }
unstableOnFocus={ () => {
setIsLabelSelected( true );
} }
onBlur={ () => {
setIsLabelSelected( false );
} }
/>
) }

Expand Down