@@ -3,10 +3,10 @@ import styled, { css } from 'styled-components'
33
44import { useDebounce } from '../hooks'
55import { CommonStyle } from '../styles'
6- import { Item } from '../types'
6+ import { Customize , Item } from '../types'
77import { $width } from '../vars'
88
9- const ItemStyle = styled ( CommonStyle ) < { hasSubitems ?: boolean } > `
9+ export const ItemStyle = styled ( CommonStyle ) < { hasSubitems ?: boolean } > `
1010 ${ props => props . hasSubitems && css `&:after {
1111 content: '►';
1212 position: absolute;
@@ -16,39 +16,48 @@ const ItemStyle = styled(CommonStyle)<{ hasSubitems?: boolean }>`
1616 }` }
1717`
1818
19- const SubitemStyles = styled . div `
19+ export const SubitemStyles = styled . div `
2020 position: absolute;
2121 top: 0;
2222 left: 100%;
2323 width: ${ $width } px;
2424`
2525
26- export function ItemElement ( props : { onClick ( ) : void , delay : number , hide ( ) : void , subitems ?: Item [ ] , children : React . ReactNode } ) {
26+ type Props = {
27+ data : Item
28+ delay : number
29+ hide ( ) : void
30+ children : React . ReactNode
31+ components ?: Pick < Customize , 'item' | 'subitems' >
32+ }
33+
34+ export function ItemElement ( props : Props ) {
2735 const [ visibleSubitems , setVisibleSubitems ] = React . useState ( false )
2836 const setInvisibile = React . useCallback ( ( ) => setVisibleSubitems ( false ) , [ setVisibleSubitems ] )
2937 const [ hide , cancelHide ] = useDebounce ( setInvisibile , props . delay )
38+ const Component = props . components ?. item ?.( props . data ) || ItemStyle
39+ const Subitems = props . components ?. subitems ?.( props . data ) || SubitemStyles
3040
31- return < ItemStyle
32- onClick = { e => { e . stopPropagation ( ) ; props . onClick ( ) ; props . hide ( ) } }
33- hasSubitems = { Boolean ( props . subitems ) }
41+ return < Component
42+ onClick = { e => { e . stopPropagation ( ) ; props . data . handler ( ) ; props . hide ( ) } }
43+ hasSubitems = { Boolean ( props . data . subitems ) }
3444 onPointerDown = { e => e . stopPropagation ( ) }
3545 onPointerOver = { ( ) => { cancelHide ( ) ; setVisibleSubitems ( true ) } }
3646 onPointerLeave = { ( ) => hide && hide ( ) }
3747 data-testid = "context-menu-item"
3848 >
3949 { props . children }
40- { props . subitems && visibleSubitems && (
41- < SubitemStyles >
42- { props . subitems . map ( item => (
50+ { props . data . subitems && visibleSubitems && (
51+ < Subitems >
52+ { props . data . subitems . map ( item => (
4353 < ItemElement
4454 key = { item . key }
45- onClick = { item . handler }
55+ data = { item }
4656 delay = { props . delay }
4757 hide = { props . hide }
48- subitems = { item . subitems }
4958 > { item . label } </ ItemElement >
5059 ) ) }
51- </ SubitemStyles >
60+ </ Subitems >
5261 ) }
53- </ ItemStyle >
62+ </ Component >
5463}
0 commit comments