@@ -23,7 +23,7 @@ const useStyles = makeStyles(styles);
23
23
type Props = {
24
24
region : Region ;
25
25
editing ?: boolean ;
26
- allowedClasses ?: Array < string > ;
26
+ allowedClasses ?: Array < string > | Array < { id : string ; label : string } > ;
27
27
allowedTags ?: Array < string > ;
28
28
cls ?: string ;
29
29
tags ?: Array < string > ;
@@ -57,6 +57,21 @@ export const RegionLabel = ({
57
57
if ( commentInput ) return commentInput . focus ( ) ;
58
58
} ;
59
59
60
+ const isCreatableAllowedClasses = typeof allowedClasses ?. [ 0 ] === "string" ;
61
+ const selectedRegionClass =
62
+ allowedClasses ?. find ( ( c ) => typeof c === "object" && c . id === region . cls ) ||
63
+ region . cls ;
64
+ const selectedLabel =
65
+ selectedRegionClass && typeof selectedRegionClass === "object"
66
+ ? selectedRegionClass . label
67
+ : region . cls ;
68
+ const selectedValue =
69
+ selectedRegionClass && typeof selectedRegionClass === "object"
70
+ ? { label : selectedRegionClass . label , value : selectedRegionClass . id }
71
+ : region . cls
72
+ ? { label : region . cls , value : region . cls }
73
+ : null ;
74
+
60
75
return (
61
76
< ThemeProvider theme = { theme } >
62
77
< Paper
@@ -73,7 +88,7 @@ export const RegionLabel = ({
73
88
className = "circle"
74
89
style = { { backgroundColor : region . color } }
75
90
/>
76
- { region . cls }
91
+ { selectedLabel }
77
92
</ div >
78
93
) }
79
94
{ region . tags && (
@@ -116,36 +131,59 @@ export const RegionLabel = ({
116
131
</ div >
117
132
{ ( allowedClasses || [ ] ) . length > 0 && (
118
133
< div style = { { marginTop : 6 } } >
119
- < CreatableSelect
120
- placeholder = "Classification"
121
- onChange = { ( o , actionMeta ) => {
122
- if ( ! o ) return ;
123
- if (
124
- actionMeta . action == "create-option" &&
125
- onRegionClassAdded
126
- ) {
127
- onRegionClassAdded ( o . value ) ;
128
- }
129
- // TODO: check if this is correct after update source
130
- onChange ( {
131
- ...region ,
132
- cls : o . value ,
133
- } ) ;
134
- } }
135
- value = {
136
- region . cls ? { label : region . cls , value : region . cls } : null
137
- }
138
- options = { asMutable (
139
- allowedClasses ?. map ( ( c ) => ( { value : c , label : c } ) )
140
- ) }
141
- />
134
+ { isCreatableAllowedClasses ? (
135
+ < CreatableSelect
136
+ placeholder = "Classification"
137
+ onChange = { ( o , actionMeta ) => {
138
+ if ( ! o ) return ;
139
+ if (
140
+ actionMeta . action == "create-option" &&
141
+ onRegionClassAdded
142
+ ) {
143
+ onRegionClassAdded ( o . value ) ;
144
+ }
145
+ onChange ( {
146
+ ...region ,
147
+ cls : o . value ,
148
+ } ) ;
149
+ } }
150
+ value = { selectedValue }
151
+ options = { asMutable (
152
+ allowedClasses ?. map ( ( c ) => {
153
+ if ( typeof c === "string" ) {
154
+ return { value : c , label : c } ;
155
+ }
156
+ return { value : c . id , label : c . label } ;
157
+ } )
158
+ ) }
159
+ />
160
+ ) : (
161
+ < Select
162
+ placeholder = "Classification"
163
+ onChange = { ( o ) => {
164
+ if ( ! o ) return ;
165
+ onChange ( {
166
+ ...region ,
167
+ cls : o . value ,
168
+ } ) ;
169
+ } }
170
+ value = { selectedValue }
171
+ options = { asMutable (
172
+ allowedClasses ?. map ( ( c ) => {
173
+ if ( typeof c === "string" ) {
174
+ return { value : c , label : c } ;
175
+ }
176
+ return { value : c . id , label : c . label } ;
177
+ } )
178
+ ) }
179
+ />
180
+ ) }
142
181
</ div >
143
182
) }
144
183
{ ( allowedTags || [ ] ) . length > 0 && (
145
184
< div style = { { marginTop : 4 } } >
146
185
< Select
147
186
onChange = { ( newTags ) =>
148
- // TODO: check if this is correct after update source
149
187
onChange ( {
150
188
...region ,
151
189
tags : newTags . map ( ( t ) => t . value ) ,
0 commit comments