1- import React , { useMemo } from "react" ;
1+ import React , { useEffect , useMemo , useRef } from "react" ;
22import { EventEmitter , createDecorators } from "@next-core/element" ;
33import { ReactNextElement , wrapBrick } from "@next-core/react-element" ;
44import "@next-core/theme" ;
@@ -39,15 +39,43 @@ interface SubMenuItemComProps {
3939 index : number ;
4040 action : SubMenuAction ;
4141 checkedKeys : ( string | number ) [ ] | undefined ;
42+ activeKeys : ( string | number ) [ ] | undefined ;
4243 onSubMenuClick : ( action : SimpleAction ) => void ;
4344}
4445
4546function SubMenuItemCom ( {
4647 index,
4748 action,
4849 checkedKeys,
50+ activeKeys,
4951 onSubMenuClick,
5052} : SubMenuItemComProps ) {
53+ const popoverRef = useRef < Popover > ( null ) ;
54+ const [ checked , opened ] = useMemo ( ( ) => {
55+ let keyIndex = - 1 ;
56+ if ( activeKeys && action . key ) {
57+ for ( let i = 0 ; i < activeKeys . length ; i ++ ) {
58+ if ( activeKeys [ i ] === action . key ) {
59+ keyIndex = i ;
60+ break ;
61+ }
62+ }
63+ }
64+ if ( keyIndex === - 1 ) {
65+ return [ false , false ] ;
66+ }
67+ if ( keyIndex === activeKeys ! . length - 1 ) {
68+ return [ true , false ] ;
69+ }
70+ return [ true , true ] ;
71+ } , [ action . key , activeKeys ] ) ;
72+
73+ useEffect ( ( ) => {
74+ if ( popoverRef . current ) {
75+ popoverRef . current . active = opened ;
76+ }
77+ } , [ opened ] ) ;
78+
5179 return (
5280 < WrappedPopover
5381 data-index = { index }
@@ -58,27 +86,46 @@ function SubMenuItemCom({
5886 distance = { 4 }
5987 anchorDisplay = "block"
6088 strategy = "fixed"
89+ ref = { popoverRef }
6190 >
6291 < WrappedMenuItem
6392 slot = "anchor"
6493 className = { classnames ( "sub-menu-item-label" , {
6594 "menu-item-danger" : action . danger ,
95+ "menu-item-active" : checked ,
6696 } ) }
6797 icon = { action . icon }
6898 disabled = { action . disabled }
6999 >
70100 { action . text }
71101 </ WrappedMenuItem >
72102 < div className = "sub-menu-wrapper" >
73- { action ?. items . map ( ( innerItem : SimpleAction , innerIndex : number ) => {
74- const menuItem = (
103+ { action ?. items . map ( ( innerItem , innerIndex : number , list ) => {
104+ if ( isDivider ( innerItem ) ) {
105+ if ( innerIndex === 0 || innerIndex === list . length - 1 ) {
106+ return null ;
107+ }
108+ return < div key = { innerIndex } className = "menu-item-divider" /> ;
109+ }
110+ const menuItem = ( innerItem as SubMenuAction ) ?. items ?. length ? (
111+ < SubMenuItemCom
112+ index = { innerIndex }
113+ action = { innerItem as SubMenuAction }
114+ checkedKeys = { checkedKeys }
115+ activeKeys = { activeKeys }
116+ onSubMenuClick = { onSubMenuClick }
117+ />
118+ ) : (
75119 < React . Fragment >
76120 < WrappedMenuItem
77121 className = { classnames ( {
78122 "menu-item-danger" : innerItem . danger ,
79123 "menu-item-selected" :
80124 ! isNil ( innerItem . key ) &&
81125 checkedKeys ?. includes ( innerItem . key ) ,
126+ "menu-item-active" :
127+ ! isNil ( innerItem . key ) &&
128+ activeKeys ?. includes ( innerItem . key ) ,
82129 } ) }
83130 icon = { innerItem . icon }
84131 disabled = { innerItem . disabled }
@@ -141,7 +188,7 @@ export interface SimpleAction {
141188}
142189
143190export interface SubMenuAction extends SimpleAction {
144- items : SimpleAction [ ] ;
191+ items : Action [ ] ;
145192 placement ?: Placement ;
146193}
147194
@@ -155,6 +202,7 @@ export interface ActionsProps {
155202 actions ?: Action [ ] ;
156203 itemDraggable ?: boolean ;
157204 checkedKeys ?: ( string | number ) [ ] ;
205+ activeKeys ?: ( string | number ) [ ] ;
158206 themeVariant ?: "default" | "elevo" ;
159207}
160208
@@ -189,13 +237,21 @@ class EoActions extends ReactNextElement implements ActionsProps {
189237 accessor actions : Action [ ] | undefined ;
190238
191239 /**
192- * actions选中项配置
240+ * actions 选中项配置
193241 */
194242 @property ( {
195243 attribute : false ,
196244 } )
197245 accessor checkedKeys : ( string | number ) [ ] = [ ] ;
198246
247+ /**
248+ * actions 激活项配置,用于菜单项的选择和展开,因按菜单层级顺序依次列出当前激活的菜单项
249+ */
250+ @property ( {
251+ attribute : false ,
252+ } )
253+ accessor activeKeys : ( string | number ) [ ] = [ ] ;
254+
199255 /**
200256 * action中的菜单项是否可拖拽
201257 */
@@ -253,6 +309,7 @@ class EoActions extends ReactNextElement implements ActionsProps {
253309 onItemDragStart = { this . #handleItemDragStart}
254310 onItemDragEnd = { this . #handleItemDragEnd}
255311 checkedKeys = { this . checkedKeys }
312+ activeKeys = { this . activeKeys }
256313 />
257314 ) ;
258315 }
@@ -268,6 +325,7 @@ export interface ActionsComponentProps extends ActionsProps {
268325export function EoActionsComponent ( {
269326 actions,
270327 checkedKeys,
328+ activeKeys,
271329 onActionClick,
272330 itemDraggable,
273331 onItemDragStart,
@@ -293,16 +351,19 @@ export function EoActionsComponent({
293351 index = { index }
294352 action = { action as SubMenuAction }
295353 checkedKeys = { checkedKeys }
354+ activeKeys = { activeKeys }
296355 onSubMenuClick = { ( action : SimpleAction ) => {
297356 onActionClick ?.( action ) ;
298357 } }
299- > </ SubMenuItemCom >
358+ / >
300359 ) : (
301360 < WrappedMenuItem
302361 className = { classnames ( {
303362 "menu-item-danger" : action . danger ,
304363 "menu-item-selected" :
305364 ! isNil ( action . key ) && checkedKeys ?. includes ( action . key ) ,
365+ "menu-item-active" :
366+ ! isNil ( action . key ) && activeKeys ?. includes ( action . key ) ,
306367 } ) }
307368 draggable = { itemDraggable }
308369 icon = { action . icon }
0 commit comments