1
- import React , { type FC , useContext , useEffect , useMemo , useState } from 'react' ;
1
+ import React , { type FC , useContext , useEffect , useState } from 'react' ;
2
2
3
3
import { cloneDeep , isEmpty , isEqual , map , size } from 'lodash' ;
4
4
import { useTranslation } from 'react-i18next' ;
@@ -13,6 +13,7 @@ import type {
13
13
SpeedSectionEntity ,
14
14
} from 'applications/editor/tools/rangeEdition/types' ;
15
15
import type { ExtendedEditorContextType } from 'applications/editor/types' ;
16
+ import SelectImprovedSNCF from 'common/BootstrapSNCF/SelectImprovedSNCF' ;
16
17
17
18
import SpeedInput from './SpeedInput' ;
18
19
@@ -29,7 +30,9 @@ const getNewSpeedLimitTag = (
29
30
return newSpeedLimitTag ;
30
31
} ;
31
32
32
- const SpeedSectionMetadataForm : FC = ( ) => {
33
+ type SpeedSectionMetadataFormProps = { speedLimitTags : string [ ] } ;
34
+
35
+ const SpeedSectionMetadataForm : FC < SpeedSectionMetadataFormProps > = ( { speedLimitTags } ) => {
33
36
const { t } = useTranslation ( ) ;
34
37
const {
35
38
state : { entity, error } ,
@@ -40,15 +43,6 @@ const SpeedSectionMetadataForm: FC = () => {
40
43
{ id : string ; tag : string ; value ?: number } [ ]
41
44
> ( map ( entity . properties . speed_limit_by_tag , ( value , tag ) => ( { tag, value, id : nextId ( ) } ) ) ) ;
42
45
43
- const tagCounts = useMemo (
44
- ( ) =>
45
- tagSpeedLimits . reduce (
46
- ( iter : Record < string , number > , { tag } ) => ( { ...iter , [ tag ] : ( iter [ tag ] || 0 ) + 1 } ) ,
47
- { }
48
- ) ,
49
- [ tagSpeedLimits ]
50
- ) ;
51
-
52
46
useEffect ( ( ) => {
53
47
const newState : Partial < RangeEditionState < SpeedSectionEntity > > = { } ;
54
48
const newSpeedLimitByTags = tagSpeedLimits . reduce (
@@ -103,24 +97,24 @@ const SpeedSectionMetadataForm: FC = () => {
103
97
) }
104
98
{ tagSpeedLimits . map ( ( { id, tag, value } , currentIndex ) => (
105
99
< div key = { id } className = "form-group field field-string" >
106
- < div className = "d-flex flex-row align-items-center" >
107
- < input
108
- required
109
- className = "form-control flex-grow-2 flex-shrink-1 mr-2 px-2"
110
- placeholder = ""
111
- type = "text"
112
- value = { tag }
113
- pattern = {
114
- // Insert an invalid pattern to force the error appearance when tag is redundant
115
- tagCounts [ tag ] && tagCounts [ tag ] > 1 ? `not ${ tag } ` : undefined
116
- }
117
- onChange = { ( e ) => {
118
- const newKey = e . target . value ;
119
- setTagSpeedLimits ( ( state ) =>
120
- state . map ( ( pair , i ) => ( i === currentIndex ? { ... pair , tag : newKey } : pair ) )
121
- ) ;
122
- } }
123
- / >
100
+ < div className = "d-flex flex-row align-items-center my-1 " >
101
+ < div className = "flex-grow-1 mr-2" >
102
+ < SelectImprovedSNCF
103
+ options = { speedLimitTags }
104
+ value = { tag }
105
+ onChange = { ( tagSelected ) => {
106
+ setTagSpeedLimits ( ( state ) =>
107
+ state . map ( ( pair , i ) =>
108
+ i === currentIndex ? { ... pair , tag : tagSelected } : pair
109
+ )
110
+ ) ;
111
+ } }
112
+ withSearch
113
+ withNewValueInput
114
+ addButtonTitle = { t ( 'Editor.tools.speed-edition.add-new- tag' ) }
115
+ bgWhite
116
+ />
117
+ </ div >
124
118
< SpeedInput
125
119
className = "form-control flex-shrink-0 px-2"
126
120
style = { { width : '5em' } }
0 commit comments