1
- import React , { useRef , useState } from 'react' ;
1
+ import React , { useState } from 'react' ;
2
2
3
3
import '@osrd-project/ui-core/dist/theme.css' ;
4
4
import '@osrd-project/ui-manchette/dist/theme.css' ;
5
5
import { EyeClosed , Telescope } from '@osrd-project/ui-icons' ;
6
6
import type { Meta , StoryObj } from '@storybook/react' ;
7
7
8
8
import { SAMPLE_PATH_PROPERTIES_DATA } from './assets/sampleData' ;
9
- import Menu , { type MenuItem } from './components/Menu' ;
10
9
import Manchette from '../components/Manchette' ;
11
- import { type StyledOperationalPointType } from '../types' ;
10
+ import type { WaypointMenuItem , StyledOperationalPointType } from '../types' ;
12
11
13
- const OperationalPointListData : StyledOperationalPointType [ ] =
12
+ const operationalPointListData : StyledOperationalPointType [ ] =
14
13
SAMPLE_PATH_PROPERTIES_DATA . operational_points ?. map ( ( op ) => ( { ...op , display : true } ) ) ?? [ ] ;
15
14
16
15
const meta : Meta < typeof Manchette > = {
@@ -33,59 +32,47 @@ const meta: Meta<typeof Manchette> = {
33
32
} ;
34
33
35
34
const ManchetteWithWaypointMenu = ( ) => {
36
- const [ menuPosition , setMenuPosition ] = useState < { top : number ; left : number } | null > ( null ) ;
37
35
const [ activeOperationalPointId , setActiveOperationalPointId ] = useState < string > ( ) ;
38
36
39
- const menuRef = useRef < HTMLDivElement > ( null ) ;
40
-
41
- const menuItems : MenuItem [ ] = [
37
+ const menuItems : WaypointMenuItem [ ] = [
42
38
{
43
39
title : 'Action 1' ,
44
40
icon : < EyeClosed /> ,
45
- onClick : ( ) => {
46
- setMenuPosition ( null ) ;
41
+ onClick : ( e : React . MouseEvent ) => {
42
+ e . stopPropagation ( ) ;
47
43
setActiveOperationalPointId ( undefined ) ;
48
44
} ,
49
45
} ,
50
46
{
51
47
title : 'Action 2' ,
52
48
icon : < Telescope /> ,
53
- onClick : ( ) => {
54
- setMenuPosition ( null ) ;
49
+ onClick : ( e : React . MouseEvent ) => {
50
+ e . stopPropagation ( ) ;
55
51
setActiveOperationalPointId ( undefined ) ;
56
52
} ,
57
53
} ,
58
54
] ;
59
55
60
- const handleWaypointClick = ( id : string , ref : HTMLDivElement | null ) => {
61
- if ( ! ref ) return ;
62
- const position = ref . getBoundingClientRect ( ) ;
63
- setMenuPosition ( { top : position . bottom - 2 , left : position . left } ) ;
56
+ const handleWaypointClick = ( id : string ) => {
64
57
setActiveOperationalPointId ( id ) ;
65
58
} ;
66
59
67
60
return (
68
61
< Manchette
69
- operationalPoints = { OperationalPointListData . map ( ( op ) => ( {
62
+ operationalPoints = { operationalPointListData . map ( ( op ) => ( {
70
63
...op ,
71
- onClick : ( id , ref ) => {
72
- handleWaypointClick ( id , ref ) ;
73
- } ,
64
+ onClick : handleWaypointClick ,
74
65
} ) ) }
75
66
zoomYIn = { ( ) => { } }
76
67
zoomYOut = { ( ) => { } }
77
68
resetZoom = { ( ) => { } }
78
69
toggleMode = { ( ) => { } }
79
- activeOperationalPointId = { activeOperationalPointId }
80
- >
81
- { menuPosition && (
82
- < Menu
83
- menuRef = { menuRef }
84
- items = { menuItems }
85
- style = { { width : '305px' , top : menuPosition . top , left : menuPosition . left } }
86
- />
87
- ) }
88
- </ Manchette >
70
+ waypointMenuData = { {
71
+ activeOperationalPointId,
72
+ waypointMenuItems : menuItems ,
73
+ waypointMenuClassName : 'menu' ,
74
+ } }
75
+ />
89
76
) ;
90
77
} ;
91
78
@@ -94,7 +81,7 @@ type Story = StoryObj<typeof ManchetteWithWaypointMenu>;
94
81
95
82
export const Default : Story = {
96
83
args : {
97
- operationalPoints : OperationalPointListData ,
84
+ operationalPoints : operationalPointListData ,
98
85
} ,
99
86
} ;
100
87
0 commit comments