Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
@@ -0,0 +1,82 @@
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { DropdownMenu } from '@wordpress/components';

/**
* Internal dependencies
*/
import { buttonOutside, buttonInside, noButton } from './icons';

const BUTTON_OPTIONS = [
'button-inside',
'button-outside',
'no-button',
'button-only',
];

/**
* Creates a custom dropdown menu for the button position options.
*
* @param {Object} Component Component properties.
* @param {string} Component.selectedOption The selected option.
* @param {Function} Component.onChange Function to handle a change
* in the selected option.
*
* @return {DropdownMenu} Dropdown menu for use inside {ToolbarGroup}
*/
export default function ButtonPositionDropdown( { selectedOption, onChange } ) {
const getButtonIcon = ( option ) => {
switch ( option ) {
case 'button-inside':
return buttonInside;
case 'button-outside':
return buttonOutside;
case 'no-button':
return noButton;
}
};

const getButtonTitle = ( option ) => {
switch ( option ) {
case 'button-inside':
return __( 'Button Inside' );
case 'button-outside':
return __( 'Button Outside' );
case 'no-button':
return __( 'No Button' );
}
};

const createOptionControl = (
targetOption,
activeOption,
title,
onChangeCallback
) => {
const isActive = targetOption === activeOption;

return {
icon: getButtonIcon( targetOption ),
title,
isActive,
onClick: () => onChangeCallback( targetOption ),
};
};

return (
<DropdownMenu
icon={ getButtonIcon( selectedOption ) }
controls={ BUTTON_OPTIONS.map( ( option ) =>
createOptionControl(
option,
selectedOption,
getButtonTitle( option ),
onChange
)
) }
label={ __( 'Change button position' ) }
/>
);
}
68 changes: 65 additions & 3 deletions packages/block-library/src/search/edit.native.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,81 @@
/**
* External dependencies
*/
import { View } from 'react-native';
import { View, Alert } from 'react-native';

/**
* WordPress dependencies
*/
import { RichText } from '@wordpress/block-editor';
import { RichText, BlockControls } from '@wordpress/block-editor';
import { ToolbarGroup, ToolbarButton } from '@wordpress/components';
import { __ } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import { buttonWithIcon, toggleLabel } from './icons';
import ButtonPositionDropdown from './button-position-dropdown.native';

export default function SearchEdit( { attributes, setAttributes } ) {
const { label, showLabel } = attributes;
const { label, showLabel, buttonPosition, buttonUseIcon } = attributes;

// Temporary. Will be removed when styling is implemented
// in a future PR.
const alert = ( message ) => {
Alert.alert( '', message, [ { text: 'OK' } ], { cancelable: true } );
};

const controls = (
<BlockControls>
<ToolbarGroup>
<ToolbarButton
title={ __( 'Toggle search label' ) }
icon={ toggleLabel }
onClick={ () => {
setAttributes( {
showLabel: ! showLabel,
} );
} }
isActive={ showLabel }
/>

<ButtonPositionDropdown
selectedOption={ buttonPosition }
onChange={ ( position ) => {
setAttributes( {
buttonPosition: position,
} );

// Temporary. Will be removed when styling is implemented
// in a future PR.
alert( `Button position: ${ position }` );
} }
/>

{ 'no-button' !== buttonPosition && (
<ToolbarButton
title={ __( 'Use button with icon' ) }
icon={ buttonWithIcon }
onClick={ () => {
setAttributes( {
buttonUseIcon: ! buttonUseIcon,
} );

// Temporary. Will be removed when styling is implemented
// in a future PR.
alert( `Icon only button: ${ ! buttonUseIcon }` );
} }
isActive={ buttonUseIcon }
/>
) }
</ToolbarGroup>
</BlockControls>
);

return (
<View>
{ controls }

{ showLabel && (
<RichText
aria-label={ __( 'Label text' ) }
Expand Down