-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
dropdown-menu-helper.js
61 lines (57 loc) · 1.36 KB
/
dropdown-menu-helper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/**
* WordPress dependencies
*/
import {
Icon,
privateApis as componentsPrivateApis,
} from '@wordpress/components';
import { forwardRef } from '@wordpress/element';
import { SVG, Circle } from '@wordpress/primitives';
/**
* Internal dependencies
*/
import { unlock } from './lock-unlock';
const { DropdownMenuItemV2: DropdownMenuItem } = unlock(
componentsPrivateApis
);
const radioCheck = (
<SVG xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
<Circle cx={ 12 } cy={ 12 } r={ 3 }></Circle>
</SVG>
);
/**
* A custom implementation of a radio menu item using the standard menu item
* component, which allows deselecting selected values.
*/
export const DropdownMenuRadioItemCustom = forwardRef(
function DropdownMenuRadioItemCustom(
{ checked, name, value, hideOnClick, onChange, onClick, ...props },
ref
) {
const onClickHandler = ( e ) => {
onClick?.( e );
onChange?.( { ...e, target: { ...e.target, value } } );
};
return (
<DropdownMenuItem
ref={ ref }
role="menuitemradio"
name={ name }
aria-checked={ checked }
hideOnClick={ !! hideOnClick }
prefix={
checked ? (
<Icon icon={ radioCheck } />
) : (
<span
className="dataviews-filters__custom-menu-radio-item-prefix"
aria-hidden="true"
></span>
)
}
onClick={ onClickHandler }
{ ...props }
/>
);
}
);