Skip to content

Commit 27319ec

Browse files
committed
feat: Update Menu support deep child
1 parent 5660399 commit 27319ec

File tree

4 files changed

+58
-14
lines changed

4 files changed

+58
-14
lines changed

docs/src/routes/Components/Menu/SimpleExample.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as React from "react";
22
import * as PropTypes from "prop-types";
33

44
import Menu, { MenuItem } from "react-uwp/Menu";
5+
import Separator from "react-uwp/Separator";
56

67
const baseStyle: React.CSSProperties = {
78
margin: "10px 0",
@@ -15,7 +16,7 @@ export default class SimpleExample extends React.Component {
1516
render() {
1617
return (
1718
<div style={baseStyle}>
18-
<Menu>
19+
<Menu menuItemHeight={36} expandedMethod="hover">
1920
<MenuItem
2021
icon="Share"
2122
label="Share"
@@ -28,10 +29,15 @@ export default class SimpleExample extends React.Component {
2829
icon="Delete"
2930
label="Delete"
3031
/>
32+
<Separator />
3133
<MenuItem label="Rename" />
3234
<MenuItem label="Select" />
3335
<MenuItem label="Child Menu">
34-
<MenuItem label="Rename" />
36+
<MenuItem label="Rename">
37+
<MenuItem
38+
label="Test"
39+
/>
40+
</MenuItem>
3541
<MenuItem
3642
icon="Delete"
3743
label="Delete"

src/Menu/MenuItem.tsx

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ export interface DataProps {
3535
* Set Menu Item hovered style.
3636
*/
3737
hoverStyle?: React.CSSProperties;
38+
/**
39+
* Set Menu Item expanded method.
40+
*/
41+
expandedMethod?: "active" | "hover";
3842
}
3943

4044
export interface AnyAttributes {
@@ -50,7 +54,8 @@ export interface MenuItemState {
5054
export class MenuItem extends React.Component<MenuItemProps, MenuItemState> {
5155
static defaultProps: MenuItemProps = {
5256
itemWidth: 240,
53-
itemHeight: 44
57+
itemHeight: 44,
58+
expandedMethod: "hover"
5459
};
5560

5661
state: MenuItemState = {
@@ -118,6 +123,7 @@ export class MenuItem extends React.Component<MenuItemProps, MenuItemState> {
118123
defaultExpanded,
119124
className,
120125
hoverStyle,
126+
expandedMethod,
121127
...attributes
122128
} = this.props;
123129
const { theme } = this.context;
@@ -127,6 +133,7 @@ export class MenuItem extends React.Component<MenuItemProps, MenuItemState> {
127133
className: "menu",
128134
styles: inlineStyles
129135
});
136+
const isHoverToggled = expandedMethod === "hover";
130137

131138
const iconProps = {
132139
size: itemHeight,
@@ -138,6 +145,8 @@ export class MenuItem extends React.Component<MenuItemProps, MenuItemState> {
138145
{...attributes}
139146
className={theme.classNames(styles.root.className, className)}
140147
style={styles.root.style}
148+
onMouseEnter={isHoverToggled ? () => this.toggleExpanded(true) : void 0}
149+
onMouseLeave={isHoverToggled ? () => this.toggleExpanded(false) : void 0}
141150
>
142151
<div ref={rootElm => this.rootElm = rootElm}>
143152
<Icon {...iconProps}>
@@ -152,14 +161,21 @@ export class MenuItem extends React.Component<MenuItemProps, MenuItemState> {
152161
cursor: "pointer",
153162
pointerEvents: "all"
154163
}}
155-
onClick={this.toggleExpanded}
164+
onClick={isHoverToggled ? void 0 : this.toggleExpanded}
156165
>
157166
ScrollChevronRightLegacy
158167
</Icon>
159168
)}
160169
{children && (
161170
<div {...styles.child}>
162-
{children}
171+
{children && React.Children.map(children, (child: any, index) => {
172+
return child.type === MenuItem ? React.cloneElement(child, {
173+
itemWidth,
174+
itemHeight,
175+
hoverStyle,
176+
expandedMethod
177+
}) : child;
178+
})}
163179
</div>
164180
)}
165181
</div>
@@ -215,8 +231,8 @@ function getStyles(menuItem: MenuItem): {
215231
pointerEvents: expanded ? "all" : "none",
216232
transition: "all .25s",
217233
position: "absolute",
218-
top: 0,
219-
left: "98%",
234+
top: -1,
235+
left: "100%",
220236
width: "100%",
221237
background: theme.useFluentDesign ? theme.acrylicTexture60.background : theme.chromeLow,
222238
border: `1px solid ${theme.listLow}`

src/Menu/index.doc.json

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@
5555
"documentation": "Set Menu Item hovered style.",
5656
"isRequired": false,
5757
"type": "CSSProperties"
58+
},
59+
{
60+
"name": "expandedMethod",
61+
"documentation": "Set Menu Item expanded method.",
62+
"isRequired": false,
63+
"type": "\"active\" | \"hover\""
5864
}
5965
]
6066
},
@@ -102,6 +108,12 @@
102108
"documentation": "Set Menu Item hovered style.",
103109
"isRequired": false,
104110
"type": "CSSProperties"
111+
},
112+
{
113+
"name": "expandedMethod",
114+
"documentation": "Set Menu Item expanded method.",
115+
"isRequired": false,
116+
"type": "\"active\" | \"hover\""
105117
}
106118
]
107119
},
@@ -130,7 +142,7 @@
130142
"exports": [
131143
{
132144
"name": "prototype",
133-
"type": "prototype"
145+
"type": "any"
134146
},
135147
{
136148
"name": "defaultProps",
@@ -167,5 +179,5 @@
167179
"type": "(menu: Menu) => { root?: CSSProperties; }"
168180
}
169181
],
170-
"type": "typeof \"D:/react-uwp/src/Menu/index\""
182+
"type": "typeof \"C:/[Work]/react-uwp/src/Menu/index\""
171183
}

src/Menu/index.tsx

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ export interface DataProps {
2121
* Set Menu Item hovered style.
2222
*/
2323
menuItemHoverStyle?: React.CSSProperties;
24+
/**
25+
* Set Menu Item expanded method.
26+
*/
27+
expandedMethod?: "active" | "hover";
2428
}
2529

2630
export interface MenuItemProps {
@@ -52,6 +56,10 @@ export interface MenuItemProps {
5256
* Set Menu Item hovered style.
5357
*/
5458
hoverStyle?: React.CSSProperties;
59+
/**
60+
* Set Menu Item expanded method.
61+
*/
62+
expandedMethod?: "active" | "hover";
5563
}
5664

5765
export interface AnyAttributes {
@@ -80,6 +88,7 @@ export class Menu extends React.Component<MenuProps, MenuState> {
8088
menuItemHoverStyle,
8189
children,
8290
className,
91+
expandedMethod,
8392
...attributes
8493
} = this.props;
8594
const { theme } = this.context;
@@ -95,13 +104,14 @@ export class Menu extends React.Component<MenuProps, MenuState> {
95104
{...attributes}
96105
{...styleClasses}
97106
>
98-
{children && React.Children.map(children, (child: any, index) => (
99-
child.type === Menu ? React.cloneElement(child, {
107+
{children && React.Children.map(children, (child: any, index) => {
108+
return child.type === MenuItem ? React.cloneElement(child, {
100109
itemWidth: menuItemWidth,
101110
itemHeight: menuItemHeight,
102-
hoverStyle: menuItemHoverStyle
103-
}) : child
104-
))}
111+
hoverStyle: menuItemHoverStyle,
112+
expandedMethod: expandedMethod
113+
}) : child;
114+
})}
105115
</div>
106116
);
107117
}

0 commit comments

Comments
 (0)