| 1 |
/** |
| 2 |
* WordPress dependencies. |
| 3 |
*/ |
| 4 |
import { createPortal, createRef, WPElement } from '@wordpress/element'; |
| 5 |
import { closeSmall, Icon } from '@wordpress/icons'; |
| 6 |
import { __, sprintf } from '@wordpress/i18n'; |
| 7 |
|
| 8 |
/** |
| 9 |
* Internal dependencies. |
| 10 |
*/ |
| 11 |
import SmallButton from '../common/small-button'; |
| 12 |
|
| 13 |
/** |
| 14 |
* Create ref for portal. |
| 15 |
*/ |
| 16 |
const ref = createRef(); |
| 17 |
|
| 18 |
/** |
| 19 |
* Active filter component. |
| 20 |
* |
| 21 |
* @param {object} props Props. |
| 22 |
* @param {string} props.label Constraint label. |
| 23 |
* @param {Function} props.onClick Click handler. |
| 24 |
* @returns {WPElement} Element. |
| 25 |
*/ |
| 26 |
export const ActiveContraint = ({ label, onClick }) => { |
| 27 |
if (!ref.current) { |
| 28 |
return null; |
| 29 |
} |
| 30 |
|
| 31 |
return createPortal( |
| 32 |
<SmallButton |
| 33 |
aria-label={sprintf( |
| 34 |
/* translators: %s: Constraint label. */ |
| 35 |
__('Remove filter: %s', 'elasticpress'), |
| 36 |
label, |
| 37 |
)} |
| 38 |
className="ep-search-icon-button" |
| 39 |
onClick={onClick} |
| 40 |
> |
| 41 |
<Icon icon={closeSmall} /> |
| 42 |
{label} |
| 43 |
</SmallButton>, |
| 44 |
ref.current, |
| 45 |
); |
| 46 |
}; |
| 47 |
|
| 48 |
/** |
| 49 |
* Active constraints component. |
| 50 |
* |
| 51 |
* @returns {WPElement} Element. |
| 52 |
*/ |
| 53 |
export default () => { |
| 54 |
return <div className="ep-search-tokens" ref={ref} />; |
| 55 |
}; |
| 56 |
|