UNPKG

@wordpress/components

Version:
114 lines (103 loc) 2.96 kB
import _extends from "@babel/runtime/helpers/esm/extends"; import { createElement } from "@wordpress/element"; /** * External dependencies */ import classnames from 'classnames'; /** * WordPress dependencies */ import { useInstanceId, useMergeRefs } from '@wordpress/compose'; import { __ } from '@wordpress/i18n'; import { Icon, search, closeSmall } from '@wordpress/icons'; import { forwardRef, useRef } from '@wordpress/element'; /** * Internal dependencies */ import Button from '../button'; import BaseControl from '../base-control'; function UnforwardedSearchControl(_ref, forwardedRef) { let { __nextHasNoMarginBottom, className, onChange, onKeyDown, value, label, placeholder = __('Search'), hideLabelFromVision = true, help, onClose, ...restProps } = _ref; const searchRef = useRef(); const instanceId = useInstanceId(SearchControl); const id = `components-search-control-${instanceId}`; const renderRightButton = () => { if (onClose) { return createElement(Button, { icon: closeSmall, label: __('Close search'), onClick: onClose }); } if (!!value) { return createElement(Button, { icon: closeSmall, label: __('Reset search'), onClick: () => { var _searchRef$current; onChange(''); (_searchRef$current = searchRef.current) === null || _searchRef$current === void 0 ? void 0 : _searchRef$current.focus(); } }); } return createElement(Icon, { icon: search }); }; return createElement(BaseControl, { __nextHasNoMarginBottom: __nextHasNoMarginBottom, label: label, id: id, hideLabelFromVision: hideLabelFromVision, help: help, className: classnames(className, 'components-search-control') }, createElement("div", { className: "components-search-control__input-wrapper" }, createElement("input", _extends({}, restProps, { ref: useMergeRefs([searchRef, forwardedRef]), className: "components-search-control__input", id: id, type: "search", placeholder: placeholder, onChange: event => onChange(event.target.value), onKeyDown: onKeyDown, autoComplete: "off", value: value || '' })), createElement("div", { className: "components-search-control__icon" }, renderRightButton()))); } /** * SearchControl components let users display a search control. * * ```jsx * import { SearchControl } from '@wordpress/components'; * import { useState } from '@wordpress/element'; * * function MySearchControl( { className, setState } ) { * const [ searchInput, setSearchInput ] = useState( '' ); * * return ( * <SearchControl * value={ searchInput } * onChange={ setSearchInput } * /> * ); * } * ``` */ export const SearchControl = forwardRef(UnforwardedSearchControl); export default SearchControl; //# sourceMappingURL=index.js.map