1- import { LegacyRef , Ref } from 'react'
1+ import { LegacyRef , MouseEvent , Ref } from 'react'
22import { Link } from 'react-router-dom'
33
44import { Tooltip } from '@Common/Tooltip'
5+ import { ComponentSizeType } from '@Shared/constants'
56
7+ import { Button , ButtonProps , ButtonVariantType } from '../Button'
68import { Icon } from '../Icon'
9+ import { NumbersCount } from '../NumbersCount'
710import { getTooltipProps } from '../SelectPicker/common'
8- import { ActionMenuItemProps } from './types'
11+ import { DTSwitch , DTSwitchProps } from '../Switch'
12+ import { ActionMenuItemProps , ActionMenuItemType } from './types'
913
10- const COMMON_ACTION_MENU_ITEM_CLASS = 'flex-grow-1 flex left top dc__gap-8 py-6 px-8'
14+ const COMMON_ACTION_MENU_ITEM_CLASS = 'w-100 flex left top dc__gap-8 py-6 px-8'
1115
1216export const ActionMenuItem = < T extends string | number > ( {
1317 item,
@@ -22,7 +26,7 @@ export const ActionMenuItem = <T extends string | number>({
2226 description,
2327 label,
2428 startIcon,
25- endIcon ,
29+ trailingItem ,
2630 tooltipProps,
2731 type = 'neutral' ,
2832 isDisabled,
@@ -40,22 +44,83 @@ export const ActionMenuItem = <T extends string | number>({
4044 const isNegativeType = type === 'negative'
4145
4246 // HANDLERS
43- const handleClick = ( ) => {
44- onClick ( item )
47+ const handleClick = ( e : MouseEvent < HTMLAnchorElement > | MouseEvent < HTMLButtonElement > ) => {
48+ onClick ( item , e )
4549 }
4650
51+ const handleTrailingSwitchChange =
52+ ( { type : trailingItemType , config } : ActionMenuItemType < T > [ 'trailingItem' ] ) : DTSwitchProps [ 'onChange' ] =>
53+ ( e ) => {
54+ if ( trailingItemType === 'switch' ) {
55+ e . stopPropagation ( )
56+ config . onChange ( e )
57+ }
58+ }
59+
60+ const handleTrailingButtonClick =
61+ ( { type : trailingItemType , config } : ActionMenuItemType < T > [ 'trailingItem' ] ) : ButtonProps [ 'onClick' ] =>
62+ ( e ) => {
63+ e . stopPropagation ( )
64+ if ( trailingItemType === 'button' && config . onClick ) {
65+ config . onClick ( e )
66+ }
67+ }
68+
4769 // RENDERERS
4870 const renderIcon = ( iconProps : typeof startIcon ) =>
4971 iconProps && (
50- < div className = "mt-2 flex dc__no-shrink" >
72+ < span className = "mt-2 flex dc__no-shrink" >
5173 < Icon { ...iconProps } color = { iconProps . color || ( isNegativeType ? 'R500' : 'N800' ) } />
52- </ div >
74+ </ span >
5375 )
5476
77+ const renderTrailingItem = ( ) => {
78+ if ( ! trailingItem ) {
79+ return null
80+ }
81+
82+ const { type : trailingItemType , config } = trailingItem
83+
84+ switch ( trailingItemType ) {
85+ case 'icon' :
86+ return renderIcon ( config )
87+ case 'text' : {
88+ const { value, icon } = config
89+ return (
90+ < span className = "flex dc__gap-2 mt-2" >
91+ < span className = "fs-12 lh-1-5 fw-4 cn-7" > { value } </ span >
92+ { icon && < Icon name = { icon . name } color = { icon . color || ( isNegativeType ? 'R500' : 'N700' ) } /> }
93+ </ span >
94+ )
95+ }
96+ case 'counter' :
97+ return < NumbersCount count = { config . value } />
98+ case 'switch' :
99+ return (
100+ < DTSwitch
101+ { ...config }
102+ onChange = { handleTrailingSwitchChange ( trailingItem ) }
103+ size = { ComponentSizeType . small }
104+ />
105+ )
106+ case 'button' :
107+ return (
108+ < Button
109+ { ...( config as ButtonProps ) }
110+ onClick = { handleTrailingButtonClick ( trailingItem ) }
111+ variant = { ButtonVariantType . borderLess }
112+ size = { ComponentSizeType . xxs }
113+ />
114+ )
115+ default :
116+ return null
117+ }
118+ }
119+
55120 const renderContent = ( ) => (
56121 < >
57122 { renderIcon ( startIcon ) }
58- < span >
123+ < span className = "flex-grow-1" >
59124 < Tooltip content = { label } placement = "right" >
60125 < span className = { `m-0 fs-13 fw-4 lh-20 dc__truncate ${ isNegativeType ? 'cr-5' : 'cn-9' } ` } >
61126 { label }
@@ -72,7 +137,7 @@ export const ActionMenuItem = <T extends string | number>({
72137 description
73138 ) ) }
74139 </ span >
75- { renderIcon ( endIcon ) }
140+ { renderTrailingItem ( ) }
76141 </ >
77142 )
78143
@@ -86,6 +151,7 @@ export const ActionMenuItem = <T extends string | number>({
86151 href = { item . href }
87152 target = "_blank"
88153 rel = "noreferrer"
154+ onClick = { handleClick }
89155 >
90156 { renderContent ( ) }
91157 </ a >
@@ -96,6 +162,7 @@ export const ActionMenuItem = <T extends string | number>({
96162 ref = { itemRef as Ref < HTMLAnchorElement > }
97163 className = { COMMON_ACTION_MENU_ITEM_CLASS }
98164 to = { item . to }
165+ onClick = { handleClick }
99166 >
100167 { renderContent ( ) }
101168 </ Link >
@@ -107,6 +174,7 @@ export const ActionMenuItem = <T extends string | number>({
107174 ref = { itemRef as LegacyRef < HTMLButtonElement > }
108175 type = "button"
109176 className = { `dc__transparent ${ COMMON_ACTION_MENU_ITEM_CLASS } ` }
177+ onClick = { handleClick }
110178 >
111179 { renderContent ( ) }
112180 </ button >
@@ -124,7 +192,6 @@ export const ActionMenuItem = <T extends string | number>({
124192 tabIndex = { - 1 }
125193 // Intentionally added margin to the left and right to have the gap on the edges of the options
126194 className = { `action-menu__option br-4 mr-4 ml-4 ${ isDisabled ? 'dc__disabled' : 'cursor' } ${ isNegativeType ? 'dc__hover-r50' : 'dc__hover-n50' } ${ isFocused ? `action-menu__option--focused${ isNegativeType ? '-negative' : '' } ` : '' } ` }
127- onClick = { ! isDisabled ? handleClick : undefined }
128195 aria-disabled = { isDisabled }
129196 >
130197 { renderComponent ( ) }
0 commit comments