Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add BaseControl to SearchControl
  • Loading branch information
youknowriad committed Jul 7, 2021
commit 221c3ed8fbc38ea7cbec36154283825a029476e6
13 changes: 13 additions & 0 deletions packages/components/src/search-control/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,19 @@ A function that receives the value of the input.
- Type: `function`
- Required: Yes

#### help

If this property is added, a help text will be generated using help property as the content.

- Type: `String|WPElement`
- Required: No
### hideLabelFromVision

If true, the label will only be visible to screen readers.

- Type: `Boolean`
- Required: No

## Related components

- To offer users more constrained options for input, use TextControl, SelectControl, RadioControl, CheckboxControl, or RangeControl.
68 changes: 37 additions & 31 deletions packages/components/src/search-control/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,50 +14,56 @@ import { useRef } from '@wordpress/element';
/**
* Internal dependencies
*/
import { VisuallyHidden, Button } from '../';
import { Button } from '../';
import BaseControl from '../base-control';

function SearchControl( {
className,
onChange,
value,
label,
placeholder = __( 'Search' ),
hideLabelFromVision = true,
help,
} ) {
const instanceId = useInstanceId( SearchControl );
const searchInput = useRef();
const id = `components-search-control-${ instanceId }`;

return (
<div className={ classnames( 'components-search-control', className ) }>
<VisuallyHidden
as="label"
htmlFor={ `components-search-control-${ instanceId }` }
>
{ label || placeholder }
</VisuallyHidden>
<input
ref={ searchInput }
className="components-search-control__input"
id={ `components-search-control-${ instanceId }` }
type="search"
placeholder={ placeholder }
onChange={ ( event ) => onChange( event.target.value ) }
autoComplete="off"
value={ value || '' }
/>
<div className="components-search-control__icon">
{ !! value && (
<Button
icon={ closeSmall }
label={ __( 'Reset search' ) }
onClick={ () => {
onChange( '' );
searchInput.current.focus();
} }
/>
) }
{ ! value && <Icon icon={ search } /> }
<BaseControl
label={ label }
id={ id }
hideLabelFromVision={ hideLabelFromVision }
help={ help }
className={ classnames( className, 'components-search-control' ) }
>
<div className="components-search-control__input-wrapper">
<input
ref={ searchInput }
className="components-search-control__input"
id={ id }
type="search"
placeholder={ placeholder }
onChange={ ( event ) => onChange( event.target.value ) }
autoComplete="off"
value={ value || '' }
/>
<div className="components-search-control__icon">
{ !! value && (
<Button
icon={ closeSmall }
label={ __( 'Reset search' ) }
onClick={ () => {
onChange( '' );
searchInput.current.focus();
} }
/>
) }
{ ! value && <Icon icon={ search } /> }
</div>
</div>
</div>
</BaseControl>
);
}

Expand Down
18 changes: 17 additions & 1 deletion packages/components/src/search-control/stories/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* External dependencies
*/
import { boolean, text } from '@storybook/addon-knobs';

/**
* WordPress dependencies
*/
Expand All @@ -15,6 +20,17 @@ export default {

export const _default = () => {
const [ value, setValue ] = useState();
const label = text( 'Label', 'Label Text' );
const hideLabelFromVision = boolean( 'Hide Label From Vision', true );
const help = text( 'Help Text', 'Help text to explain the input.' );

return <SearchControl value={ value } onChange={ setValue } />;
return (
<SearchControl
label={ label }
hideLabelFromVision={ hideLabelFromVision }
help={ help }
value={ value }
onChange={ setValue }
/>
);
};
4 changes: 4 additions & 0 deletions packages/components/src/search-control/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,7 @@
margin: $grid-unit-10 0;
}
}

.components-search-control__input-wrapper {
position: relative;
}