@@ -23,7 +23,7 @@ const useStyles = makeStyles(styles);
2323type Props = {
2424 region : Region ;
2525 editing ?: boolean ;
26- allowedClasses ?: Array < string > ;
26+ allowedClasses ?: Array < string > | Array < { id : string ; label : string } > ;
2727 allowedTags ?: Array < string > ;
2828 cls ?: string ;
2929 tags ?: Array < string > ;
@@ -57,6 +57,21 @@ export const RegionLabel = ({
5757 if ( commentInput ) return commentInput . focus ( ) ;
5858 } ;
5959
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+
6075 return (
6176 < ThemeProvider theme = { theme } >
6277 < Paper
@@ -73,7 +88,7 @@ export const RegionLabel = ({
7388 className = "circle"
7489 style = { { backgroundColor : region . color } }
7590 />
76- { region . cls }
91+ { selectedLabel }
7792 </ div >
7893 ) }
7994 { region . tags && (
@@ -116,36 +131,59 @@ export const RegionLabel = ({
116131 </ div >
117132 { ( allowedClasses || [ ] ) . length > 0 && (
118133 < 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+ ) }
142181 </ div >
143182 ) }
144183 { ( allowedTags || [ ] ) . length > 0 && (
145184 < div style = { { marginTop : 4 } } >
146185 < Select
147186 onChange = { ( newTags ) =>
148- // TODO: check if this is correct after update source
149187 onChange ( {
150188 ...region ,
151189 tags : newTags . map ( ( t ) => t . value ) ,
0 commit comments