@@ -69,10 +69,10 @@ function Swatch({ item }: { item: LegendItem }) {
6969 ) ;
7070}
7171
72- function groupByKind ( items : LegendItem [ ] ) {
72+ function groupByField ( items : LegendItem [ ] ) {
7373 const map = new Map < string , LegendItem [ ] > ( ) ;
7474 for ( const it of items ) {
75- const key = it . kind ;
75+ const key = it . fieldLabel || "All fields" ;
7676 if ( ! map . has ( key ) ) map . set ( key , [ ] ) ;
7777 map . get ( key ) ! . push ( it ) ;
7878 }
@@ -160,26 +160,37 @@ export default function SldLegend({
160160 const rawItems = useMemo ( ( ) => {
161161 if ( ! styler ) return [ ] ;
162162 const base = legendFromStyler ( styler , layerNameOrIndex ) ;
163-
164- // If labels are useless (e.g. "All features" repeated), replace by rule titles in order.
165- const allDefault = base . length > 0 && base . every ( ( it ) => isDefaultLegendLabel ( it . label ) ) ;
166- if ( ! allDefault ) return base ;
167-
168- return base . map ( ( it , idx ) => ( {
169- ...it ,
170- label : ruleTitles [ idx ] ?? it . label ?? t ( "Map.sldLegend.untitled" ) ,
171- } ) ) ;
163+ const titlesMatchRuleCount = ruleTitles . length >= base . length ;
164+
165+ return base . map ( ( it , idx ) => {
166+ const fallbackLabel = it . label ?? t ( "Map.sldLegend.untitled" ) ;
167+ const label =
168+ titlesMatchRuleCount && ruleTitles [ idx ]
169+ ? ruleTitles [ idx ]
170+ : isDefaultLegendLabel ( fallbackLabel )
171+ ? ruleTitles [ idx ] ?? fallbackLabel
172+ : fallbackLabel ;
173+
174+ return {
175+ ...it ,
176+ label,
177+ } ;
178+ } ) ;
172179 } , [ styler , layerNameOrIndex , ruleTitles , t ] ) ;
173180
174181 const [ q , setQ ] = useState ( "" ) ;
175182
176183 const items = useMemo ( ( ) => {
177184 const query = q . trim ( ) . toLowerCase ( ) ;
178185 if ( ! query ) return rawItems ;
179- return rawItems . filter ( ( it ) => ( it . label ?? "" ) . toLowerCase ( ) . includes ( query ) ) ;
186+ return rawItems . filter ( ( it ) => {
187+ const label = ( it . label ?? "" ) . toLowerCase ( ) ;
188+ const field = ( it . fieldLabel ?? "" ) . toLowerCase ( ) ;
189+ return label . includes ( query ) || field . includes ( query ) ;
190+ } ) ;
180191 } , [ rawItems , q ] ) ;
181192
182- const groups = useMemo ( ( ) => groupByKind ( items ) , [ items ] ) ;
193+ const groups = useMemo ( ( ) => groupByField ( items ) , [ items ] ) ;
183194
184195 if ( rawItems . length === 0 ) return null ;
185196
@@ -223,26 +234,31 @@ export default function SldLegend({
223234
224235 < CardContent className = "pt-2" >
225236 < div className = "max-h-[40vh] space-y-4 overflow-y-auto pr-1 sm:max-h-[260px]" >
226- { groups . map ( ( [ kind , groupItems ] ) => (
227- < div key = { kind } className = "space-y-2" >
237+ { groups . map ( ( [ field , groupItems ] ) => (
238+ < div key = { field } className = "space-y-2" >
228239 < div className = "text-xs font-medium text-muted-foreground" >
229- { kind === "polygon"
230- ? t ( "Map.sldLegend.groups.areas" )
231- : kind === "line"
232- ? t ( "Map.sldLegend.groups.lines" )
233- : t ( "Map.sldLegend.groups.points" ) }
240+ { field }
234241 </ div >
235242
236243 < div className = "space-y-1.5" >
237244 { groupItems . map ( ( it , idx ) => (
238245 < div
239- key = { `${ it . kind } -${ it . label } -${ idx } ` }
246+ key = { `${ field } - ${ it . kind } -${ it . label } -${ idx } ` }
240247 className = "flex items-center gap-3 rounded-lg border px-2.5 py-2 bg-card/50 hover:bg-card transition-colors"
241248 style = { { borderColor : "rgba(0,0,0,0.08)" } }
242249 >
243250 < Swatch item = { it } />
244- < div className = "text-sm flex-1" >
245- { it . label || t ( "Map.sldLegend.untitled" ) }
251+ < div className = "flex-1" >
252+ < div className = "text-sm" >
253+ { it . label || t ( "Map.sldLegend.untitled" ) }
254+ </ div >
255+ < div className = "text-xs text-muted-foreground" >
256+ { it . kind === "polygon"
257+ ? t ( "Map.sldLegend.groups.areas" )
258+ : it . kind === "line"
259+ ? t ( "Map.sldLegend.groups.lines" )
260+ : t ( "Map.sldLegend.groups.points" ) }
261+ </ div >
246262 </ div >
247263 </ div >
248264 ) ) }
0 commit comments