1 | import * as React from "react";
|
2 | import { createComponent } from "reakit-system/createComponent";
|
3 | import { createHook } from "reakit-system/createHook";
|
4 | import {
|
5 | CheckboxOptions,
|
6 | useCheckbox,
|
7 | CheckboxHTMLProps,
|
8 | } from "../Checkbox/Checkbox";
|
9 | import { MenuItemOptions, MenuItemHTMLProps, useMenuItem } from "./MenuItem";
|
10 | import { MenuStateReturn } from "./MenuState";
|
11 | import { MENU_ITEM_CHECKBOX_KEYS } from "./__keys";
|
12 |
|
13 | export type MenuItemCheckboxOptions = CheckboxOptions &
|
14 | MenuItemOptions &
|
15 | Pick<MenuStateReturn, "unstable_values" | "unstable_setValue"> & {
|
16 | |
17 |
|
18 |
|
19 | name: string;
|
20 | };
|
21 |
|
22 | export type MenuItemCheckboxHTMLProps = CheckboxHTMLProps & MenuItemHTMLProps;
|
23 |
|
24 | export type MenuItemCheckboxProps = MenuItemCheckboxOptions &
|
25 | MenuItemCheckboxHTMLProps;
|
26 |
|
27 | export const useMenuItemCheckbox = createHook<
|
28 | MenuItemCheckboxOptions,
|
29 | MenuItemCheckboxHTMLProps
|
30 | >({
|
31 | name: "MenuItemCheckbox",
|
32 | compose: [useMenuItem, useCheckbox],
|
33 | keys: MENU_ITEM_CHECKBOX_KEYS,
|
34 |
|
35 | propsAreEqual(prev, next) {
|
36 | if (prev.name !== next.name) {
|
37 | return useMenuItem.unstable_propsAreEqual(prev, next);
|
38 | }
|
39 | const { unstable_values: prevValues, ...prevProps } = prev;
|
40 | const { unstable_values: nextValues, ...nextProps } = next;
|
41 | if (prevValues[next.name] !== nextValues[next.name]) {
|
42 | return false;
|
43 | }
|
44 | return useMenuItem.unstable_propsAreEqual(prevProps, nextProps);
|
45 | },
|
46 |
|
47 | useOptions(options) {
|
48 | const setState = React.useCallback(
|
49 | (value) => options.unstable_setValue(options.name, value),
|
50 | [options.unstable_setValue, options.name]
|
51 | );
|
52 | return {
|
53 | ...options,
|
54 | state: options.unstable_values[options.name],
|
55 | setState,
|
56 | };
|
57 | },
|
58 |
|
59 | useProps(options, htmlProps) {
|
60 | return {
|
61 | role: "menuitemcheckbox",
|
62 | name: options.name,
|
63 | ...htmlProps,
|
64 | };
|
65 | },
|
66 | });
|
67 |
|
68 | export const MenuItemCheckbox = createComponent({
|
69 | as: "button",
|
70 | memo: true,
|
71 | useHook: useMenuItemCheckbox,
|
72 | });
|