@defra/interactive-map 0.0.41-alpha → 0.0.42-fmp-2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/css/index.css +1 -1
- package/dist/esm/im-core.js +1 -1
- package/dist/umd/im-core.js +1 -1
- package/dist/umd/index.js +1 -1
- package/docs/api/button-definition.md +7 -8
- package/docs/api/control-definition.md +8 -0
- package/docs/api/panel-definition.md +5 -0
- package/docs/api/slots.md +24 -1
- package/docs/examples/add-polygons.mdx +8 -2
- package/docs/examples/add-symbols.mdx +8 -2
- package/docs/plugins/datasets.md +23 -15
- package/docs/plugins/map-key.md +147 -0
- package/docs/plugins.md +5 -1
- package/package.json +4 -2
- package/plugins/beta/draw-ol/dist/css/index.css +1 -13
- package/plugins/beta/frame/dist/css/index.css +1 -11
- package/plugins/beta/map-styles/dist/css/index.css +1 -1
- package/plugins/beta/map-styles/dist/umd/im-map-styles-plugin.js +1 -1
- package/plugins/beta/map-styles/dist/umd/index.js +1 -1
- package/plugins/beta/scale-bar/dist/css/index.css +1 -31
- package/plugins/beta/use-location/dist/esm/im-use-location-plugin.js +1 -1
- package/plugins/beta/use-location/dist/umd/im-use-location-plugin.js +1 -1
- package/plugins/beta/use-location/src/manifest.js +1 -1
- package/plugins/datasets/dist/css/5648.index.css +1 -5
- package/plugins/datasets/dist/css/index.css +1 -1
- package/plugins/draw/dist/esm/im-draw-plugin.js +1 -1
- package/plugins/draw/dist/umd/im-draw-plugin.js +1 -1
- package/plugins/draw/dist/umd/index.js +1 -1
- package/plugins/draw/src/api/createNewShape.js +41 -0
- package/plugins/draw/src/api/createNewShape.test.js +63 -0
- package/plugins/draw/src/api/newLine.js +2 -40
- package/plugins/draw/src/api/newPolygon.js +2 -40
- package/plugins/interact/dist/esm/im-interact-plugin.js +1 -1
- package/plugins/interact/dist/umd/im-interact-plugin.js +1 -1
- package/plugins/interact/dist/umd/index.js +1 -1
- package/plugins/interact/src/manifest.js +1 -1
- package/plugins/map-key/dist/css/index.css +1 -62
- package/plugins/map-key/dist/esm/im-map-key-plugin.js +1 -1
- package/plugins/map-key/dist/umd/im-map-key-plugin.js +1 -1
- package/plugins/map-key/src/components/Key/Key.jsx +5 -3
- package/plugins/map-key/src/components/Key/Key.module.scss +6 -0
- package/plugins/map-key/src/components/Key/KeyItem.jsx +5 -8
- package/plugins/map-key/src/components/Key/KeySvg.jsx +21 -22
- package/plugins/map-key/src/components/Key/KeySvgLine.jsx +3 -9
- package/plugins/map-key/src/components/Key/KeySvgPattern.jsx +2 -7
- package/plugins/map-key/src/components/Key/KeySvgPattern.test.jsx +6 -0
- package/plugins/map-key/src/components/Key/KeySvgRect.jsx +4 -11
- package/plugins/map-key/src/components/Key/KeySvgSymbol.jsx +5 -11
- package/plugins/map-key/src/components/Key/KeySvgSymbol.test.jsx +12 -0
- package/plugins/map-key/src/components/Key/MapKey.jsx +5 -2
- package/plugins/map-key/src/components/Key/MapKey.test.jsx +28 -11
- package/plugins/menu/dist/css/index.css +1 -80
- package/plugins/search/dist/css/index.css +1 -1
- package/providers/beta/esri/dist/css/index.css +1 -34
- package/rollup.esm.mjs +2 -1
- package/scripts/publish-package.sh +8 -0
- package/src/App/components/KeyboardHelp/KeyboardHelp.jsx +25 -23
- package/src/App/components/KeyboardHelp/KeyboardHelp.test.jsx +22 -5
- package/src/App/components/Panel/Panel.jsx +50 -8
- package/src/App/components/Panel/Panel.module.scss +31 -7
- package/src/App/components/Panel/Panel.test.jsx +81 -2
- package/src/App/components/Tabs/Tabs.jsx +43 -17
- package/src/App/components/Tabs/Tabs.module.scss +32 -5
- package/src/App/components/Tabs/Tabs.test.jsx +40 -10
- package/src/App/renderer/groupByKey.js +28 -0
- package/src/App/renderer/groupByKey.test.js +39 -0
- package/src/App/renderer/groupIntoTabs.js +42 -0
- package/src/App/renderer/groupIntoTabs.test.js +95 -0
- package/src/App/renderer/mapButtons.js +74 -122
- package/src/App/renderer/mapButtons.test.js +51 -80
- package/src/App/renderer/mapControls.js +1 -0
- package/src/App/renderer/mapControls.test.js +16 -0
- package/src/App/renderer/mapPanels.js +26 -19
- package/src/App/renderer/mapPanels.test.js +44 -0
- package/src/config/appConfig.js +2 -2
- package/src/types.js +20 -2
- package/webpack.umd.mjs +6 -1
|
@@ -1,11 +1,18 @@
|
|
|
1
1
|
import React from 'react'
|
|
2
2
|
import { render, screen, fireEvent } from '@testing-library/react'
|
|
3
3
|
import { Tabs } from './Tabs'
|
|
4
|
+
import { useConfig } from '../../store/configContext'
|
|
5
|
+
|
|
6
|
+
jest.mock('../../store/configContext', () => ({ useConfig: jest.fn() }))
|
|
4
7
|
|
|
5
8
|
const TAB_A = { name: 'Alpha', content: <p>Alpha content</p> }
|
|
6
9
|
const TAB_B = { name: 'Beta', content: <p>Beta content</p> }
|
|
7
10
|
const TAB_C = { name: 'Gamma', content: <p>Gamma content</p> }
|
|
8
11
|
|
|
12
|
+
beforeEach(() => {
|
|
13
|
+
useConfig.mockReturnValue({ id: 'test-app' })
|
|
14
|
+
})
|
|
15
|
+
|
|
9
16
|
// ─── rendering ───────────────────────────────────────────────────────────────
|
|
10
17
|
|
|
11
18
|
describe('Tabs — rendering', () => {
|
|
@@ -15,17 +22,17 @@ describe('Tabs — rendering', () => {
|
|
|
15
22
|
expect(screen.getByRole('tab', { name: 'Beta' })).toBeInTheDocument()
|
|
16
23
|
})
|
|
17
24
|
|
|
18
|
-
it('shows content for the active tab only', () => {
|
|
25
|
+
it('shows content for the active tab only — the inactive one stays in the DOM (so its aria-controls target exists) but hidden', () => {
|
|
19
26
|
render(<Tabs tabs={[TAB_A, TAB_B]} />)
|
|
20
|
-
expect(screen.getByText('Alpha content')).
|
|
21
|
-
expect(screen.
|
|
27
|
+
expect(screen.getByText('Alpha content')).toBeVisible()
|
|
28
|
+
expect(screen.getByText('Beta content')).not.toBeVisible()
|
|
22
29
|
})
|
|
23
30
|
|
|
24
31
|
it('uses defaultTab to set the initial active tab', () => {
|
|
25
32
|
render(<Tabs tabs={[TAB_A, TAB_B]} defaultTab='Beta' />)
|
|
26
33
|
expect(screen.getByRole('tab', { name: 'Beta' })).toHaveAttribute('aria-selected', 'true')
|
|
27
|
-
expect(screen.getByText('Beta content')).
|
|
28
|
-
expect(screen.
|
|
34
|
+
expect(screen.getByText('Beta content')).toBeVisible()
|
|
35
|
+
expect(screen.getByText('Alpha content')).not.toBeVisible()
|
|
29
36
|
})
|
|
30
37
|
|
|
31
38
|
it('falls back to first tab when defaultTab is not provided', () => {
|
|
@@ -41,8 +48,8 @@ describe('Tabs — click behaviour', () => {
|
|
|
41
48
|
render(<Tabs tabs={[TAB_A, TAB_B]} />)
|
|
42
49
|
fireEvent.click(screen.getByRole('tab', { name: 'Beta' }))
|
|
43
50
|
expect(screen.getByRole('tab', { name: 'Beta' })).toHaveAttribute('aria-selected', 'true')
|
|
44
|
-
expect(screen.getByText('Beta content')).
|
|
45
|
-
expect(screen.
|
|
51
|
+
expect(screen.getByText('Beta content')).toBeVisible()
|
|
52
|
+
expect(screen.getByText('Alpha content')).not.toBeVisible()
|
|
46
53
|
})
|
|
47
54
|
})
|
|
48
55
|
|
|
@@ -55,10 +62,33 @@ describe('Tabs — WCAG attributes', () => {
|
|
|
55
62
|
expect(screen.getByRole('tab', { name: 'Beta' })).toHaveAttribute('tabindex', '-1')
|
|
56
63
|
})
|
|
57
64
|
|
|
58
|
-
it('
|
|
65
|
+
it('every tab has aria-controls pointing to a real element — including inactive tabs, since their panel stays in the DOM (hidden) rather than being unmounted', () => {
|
|
66
|
+
render(<Tabs tabs={[TAB_A, TAB_B]} />)
|
|
67
|
+
const activeTab = screen.getByRole('tab', { name: 'Alpha' })
|
|
68
|
+
const inactiveTab = screen.getByRole('tab', { name: 'Beta' })
|
|
69
|
+
expect(document.getElementById(activeTab.getAttribute('aria-controls'))).toBeInTheDocument()
|
|
70
|
+
expect(document.getElementById(inactiveTab.getAttribute('aria-controls'))).toBeInTheDocument()
|
|
71
|
+
})
|
|
72
|
+
|
|
73
|
+
it('gives every tabpanel its own stable id, distinct per tab, not just the active one', () => {
|
|
74
|
+
render(<Tabs tabs={[TAB_A, TAB_B]} />)
|
|
75
|
+
const activeTab = screen.getByRole('tab', { name: 'Alpha' })
|
|
76
|
+
const inactiveTab = screen.getByRole('tab', { name: 'Beta' })
|
|
77
|
+
expect(activeTab.getAttribute('aria-controls')).not.toBe(inactiveTab.getAttribute('aria-controls'))
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
it('hides the inactive tabpanel via the hidden attribute rather than unmounting it', () => {
|
|
81
|
+
render(<Tabs tabs={[TAB_A, TAB_B]} />)
|
|
82
|
+
const inactiveTab = screen.getByRole('tab', { name: 'Beta' })
|
|
83
|
+
const inactivePanel = document.getElementById(inactiveTab.getAttribute('aria-controls'))
|
|
84
|
+
expect(inactivePanel).toHaveAttribute('hidden')
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
it("labels every tabpanel with its own tab, not just the active one's", () => {
|
|
59
88
|
render(<Tabs tabs={[TAB_A, TAB_B]} />)
|
|
60
|
-
const
|
|
61
|
-
|
|
89
|
+
const inactiveTab = screen.getByRole('tab', { name: 'Beta' })
|
|
90
|
+
const inactivePanel = document.getElementById(inactiveTab.getAttribute('aria-controls'))
|
|
91
|
+
expect(inactivePanel.getAttribute('aria-labelledby')).toBe(inactiveTab.id)
|
|
62
92
|
})
|
|
63
93
|
|
|
64
94
|
it('panel has aria-labelledby pointing to the active tab', () => {
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { stringToKebab } from '../../utils/stringToKebab.js'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Partitions a list of items into buckets, keyed by `keyFn(item)` kebab-cased — two items
|
|
5
|
+
* share a bucket by producing the same key (so 'Map Size' and 'map size' merge). Items for
|
|
6
|
+
* which `keyFn` returns a falsy value all share one `null`-keyed bucket. Bucket iteration
|
|
7
|
+
* order follows first-encountered order, same as `Map`.
|
|
8
|
+
*
|
|
9
|
+
* Shared by `groupIntoTabs` (panel/control tabs) and `mapButtons` (button groups) — the two
|
|
10
|
+
* callers differ only in what happens *after* bucketing (how a bucket's own order/label is
|
|
11
|
+
* derived), not in how items get partitioned.
|
|
12
|
+
*
|
|
13
|
+
* @returns {Map<string|null, object[]>}
|
|
14
|
+
*/
|
|
15
|
+
export function groupByKey ({ items, keyFn }) {
|
|
16
|
+
const buckets = new Map()
|
|
17
|
+
|
|
18
|
+
for (const item of items) {
|
|
19
|
+
const raw = keyFn(item)
|
|
20
|
+
const key = raw ? stringToKebab(raw) : null
|
|
21
|
+
if (!buckets.has(key)) {
|
|
22
|
+
buckets.set(key, [])
|
|
23
|
+
}
|
|
24
|
+
buckets.get(key).push(item)
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return buckets
|
|
28
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { groupByKey } from './groupByKey.js'
|
|
2
|
+
|
|
3
|
+
describe('groupByKey', () => {
|
|
4
|
+
it('returns one bucket per distinct key', () => {
|
|
5
|
+
const items = [{ id: 'a', k: 'Styles' }, { id: 'b', k: 'Sizes' }]
|
|
6
|
+
const buckets = groupByKey({ items, keyFn: item => item.k })
|
|
7
|
+
expect([...buckets.keys()].sort()).toEqual(['sizes', 'styles'])
|
|
8
|
+
})
|
|
9
|
+
|
|
10
|
+
it('lowercases the key, hyphenating only camelCase-style boundaries (stringToKebab)', () => {
|
|
11
|
+
const items = [{ id: 'a', k: 'mapSize' }]
|
|
12
|
+
const buckets = groupByKey({ items, keyFn: item => item.k })
|
|
13
|
+
expect([...buckets.keys()]).toEqual(['map-size'])
|
|
14
|
+
})
|
|
15
|
+
|
|
16
|
+
it('merges items whose key differs only by case/whitespace into one bucket', () => {
|
|
17
|
+
const items = [{ id: 'a', k: 'Map Size' }, { id: 'b', k: 'map size' }]
|
|
18
|
+
const buckets = groupByKey({ items, keyFn: item => item.k })
|
|
19
|
+
expect(buckets.size).toBe(1)
|
|
20
|
+
expect([...buckets.values()][0]).toHaveLength(2)
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
it('puts items with no key into one null-keyed bucket', () => {
|
|
24
|
+
const items = [{ id: 'a' }, { id: 'b' }]
|
|
25
|
+
const buckets = groupByKey({ items, keyFn: () => null })
|
|
26
|
+
expect(buckets.size).toBe(1)
|
|
27
|
+
expect(buckets.get(null)).toHaveLength(2)
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
it('keeps items within a bucket in their original relative order', () => {
|
|
31
|
+
const items = [{ id: 'a', k: 'g' }, { id: 'b', k: 'g' }, { id: 'c', k: 'g' }]
|
|
32
|
+
const buckets = groupByKey({ items, keyFn: item => item.k })
|
|
33
|
+
expect(buckets.get('g').map(i => i.id)).toEqual(['a', 'b', 'c'])
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
it('returns an empty map for an empty list', () => {
|
|
37
|
+
expect(groupByKey({ items: [], keyFn: () => 'x' }).size).toBe(0)
|
|
38
|
+
})
|
|
39
|
+
})
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { orderItems } from './orderItems.js'
|
|
2
|
+
import { groupByKey } from './groupByKey.js'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Groups a flat list of `{ id, order, tab, element }` items into tabs, when warranted.
|
|
6
|
+
*
|
|
7
|
+
* Items are partitioned by `tab` via `groupByKey` — two items share a tab by producing the
|
|
8
|
+
* same kebab-cased key. Items with no `tab` share one fallback bucket. Returns `null` when
|
|
9
|
+
* there's one bucket or fewer, so the caller can fall back to flat rendering unchanged.
|
|
10
|
+
*
|
|
11
|
+
* Each bucket's members are ordered via `orderItems`; the resulting tab's own `order` and
|
|
12
|
+
* displayed `name` both come from whichever member ends up first in that ordering — the same
|
|
13
|
+
* item that "wins" the position also wins the label, so there's nothing separate to reconcile
|
|
14
|
+
* when members disagree. The tabs themselves are then ordered the same way, via `orderItems`
|
|
15
|
+
* again — no new ordering concept, just the existing one applied twice.
|
|
16
|
+
*
|
|
17
|
+
* This derived-order approach is safe here because tabs are mutually exclusive — only the
|
|
18
|
+
* active tab's content is ever visible, so a "losing" member's own order never has to compete
|
|
19
|
+
* against anything outside its own tab at the same time. `mapButtons`'s button groups don't
|
|
20
|
+
* have that property (every group renders simultaneously), so they don't derive a group's
|
|
21
|
+
* order from its members the same way — see `mapButtons.js`.
|
|
22
|
+
*/
|
|
23
|
+
export function groupIntoTabs ({ items, fallbackLabel }) {
|
|
24
|
+
const buckets = groupByKey({ items, keyFn: item => item.tab })
|
|
25
|
+
|
|
26
|
+
if (buckets.size <= 1) {
|
|
27
|
+
return null
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const tabs = Array.from(buckets.entries()).map(([key, members]) => {
|
|
31
|
+
const orderedMembers = orderItems(members)
|
|
32
|
+
const first = orderedMembers[0]
|
|
33
|
+
return {
|
|
34
|
+
id: key ?? 'default',
|
|
35
|
+
order: first.order,
|
|
36
|
+
name: key ? first.tab : fallbackLabel,
|
|
37
|
+
items: orderedMembers
|
|
38
|
+
}
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
return orderItems(tabs)
|
|
42
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { groupIntoTabs } from './groupIntoTabs.js'
|
|
2
|
+
|
|
3
|
+
describe('groupIntoTabs', () => {
|
|
4
|
+
it('returns null when there are no items', () => {
|
|
5
|
+
expect(groupIntoTabs({ items: [], fallbackLabel: 'Panel' })).toBeNull()
|
|
6
|
+
})
|
|
7
|
+
|
|
8
|
+
it('returns null when all items share the same tab', () => {
|
|
9
|
+
const items = [
|
|
10
|
+
{ id: 'a', order: 0, tab: 'Styles' },
|
|
11
|
+
{ id: 'b', order: 0, tab: 'Styles' }
|
|
12
|
+
]
|
|
13
|
+
expect(groupIntoTabs({ items, fallbackLabel: 'Panel' })).toBeNull()
|
|
14
|
+
})
|
|
15
|
+
|
|
16
|
+
it('returns null when no items have a tab', () => {
|
|
17
|
+
const items = [{ id: 'a', order: 0 }, { id: 'b', order: 0 }]
|
|
18
|
+
expect(groupIntoTabs({ items, fallbackLabel: 'Panel' })).toBeNull()
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
it('partitions items into distinct tabs', () => {
|
|
22
|
+
const items = [
|
|
23
|
+
{ id: 'a', order: 0, tab: 'Styles' },
|
|
24
|
+
{ id: 'b', order: 0, tab: 'Sizes' }
|
|
25
|
+
]
|
|
26
|
+
const tabs = groupIntoTabs({ items, fallbackLabel: 'Panel' })
|
|
27
|
+
expect(tabs.map(t => t.name).sort()).toEqual(['Sizes', 'Styles'])
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
it('groups untagged items into one fallback bucket named after fallbackLabel', () => {
|
|
31
|
+
const items = [
|
|
32
|
+
{ id: 'a', order: 0, tab: 'Styles' },
|
|
33
|
+
{ id: 'b', order: 0 },
|
|
34
|
+
{ id: 'c', order: 0 }
|
|
35
|
+
]
|
|
36
|
+
const tabs = groupIntoTabs({ items, fallbackLabel: 'Map styles' })
|
|
37
|
+
const fallback = tabs.find(t => t.name === 'Map styles')
|
|
38
|
+
expect(fallback.items.map(i => i.id).sort()).toEqual(['b', 'c'])
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
it('merges items whose tab differs only by case/whitespace into one bucket', () => {
|
|
42
|
+
const items = [
|
|
43
|
+
{ id: 'a', order: 0, tab: 'Map Size' },
|
|
44
|
+
{ id: 'b', order: 1, tab: 'map size' },
|
|
45
|
+
{ id: 'c', order: 0, tab: 'Styles' }
|
|
46
|
+
]
|
|
47
|
+
const tabs = groupIntoTabs({ items, fallbackLabel: 'Panel' })
|
|
48
|
+
expect(tabs).toHaveLength(2)
|
|
49
|
+
const merged = tabs.find(t => t.items.length === 2)
|
|
50
|
+
expect(merged.items.map(i => i.id)).toEqual(expect.arrayContaining(['a', 'b']))
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
it("uses the winning (lowest-order) member's raw tab string as the displayed name", () => {
|
|
54
|
+
const items = [
|
|
55
|
+
{ id: 'a', order: 2, tab: 'MAP SIZE' },
|
|
56
|
+
{ id: 'b', order: 1, tab: 'map size' }, // lower order wins the label
|
|
57
|
+
{ id: 'c', order: 0, tab: 'Styles' }
|
|
58
|
+
]
|
|
59
|
+
const tabs = groupIntoTabs({ items, fallbackLabel: 'Panel' })
|
|
60
|
+
const merged = tabs.find(t => t.items.length === 2)
|
|
61
|
+
expect(merged.name).toBe('map size')
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
it("derives a tab's order from its first ordered member", () => {
|
|
65
|
+
const items = [
|
|
66
|
+
{ id: 'a', order: 5, tab: 'Styles' },
|
|
67
|
+
{ id: 'b', order: 1, tab: 'Styles' },
|
|
68
|
+
{ id: 'c', order: 0, tab: 'Sizes' }
|
|
69
|
+
]
|
|
70
|
+
const tabs = groupIntoTabs({ items, fallbackLabel: 'Panel' })
|
|
71
|
+
const stylesTab = tabs.find(t => t.name === 'Styles')
|
|
72
|
+
expect(stylesTab.order).toBe(1)
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
it('orders tabs among themselves using the derived order', () => {
|
|
76
|
+
const items = [
|
|
77
|
+
{ id: 'a', order: 2, tab: 'Second' },
|
|
78
|
+
{ id: 'b', order: 1, tab: 'First' }
|
|
79
|
+
]
|
|
80
|
+
const tabs = groupIntoTabs({ items, fallbackLabel: 'Panel' })
|
|
81
|
+
expect(tabs.map(t => t.name)).toEqual(['First', 'Second'])
|
|
82
|
+
})
|
|
83
|
+
|
|
84
|
+
it('orders items within a tab using orderItems', () => {
|
|
85
|
+
const items = [
|
|
86
|
+
{ id: 'a', order: 0, tab: 'Styles' },
|
|
87
|
+
{ id: 'b', order: 1, tab: 'Styles' },
|
|
88
|
+
{ id: 'c', order: 0, tab: 'Sizes' }
|
|
89
|
+
]
|
|
90
|
+
const tabs = groupIntoTabs({ items, fallbackLabel: 'Panel' })
|
|
91
|
+
const stylesTab = tabs.find(t => t.name === 'Styles')
|
|
92
|
+
// b (order 1) is spliced ahead of the unordered item a
|
|
93
|
+
expect(stylesTab.items.map(i => i.id)).toEqual(['b', 'a'])
|
|
94
|
+
})
|
|
95
|
+
})
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
// src/core/renderers/mapButtons.js
|
|
2
2
|
import { MapButton } from '../components/MapButton/MapButton.jsx'
|
|
3
3
|
import { allowedSlots } from './slots.js'
|
|
4
|
+
import { groupByKey } from './groupByKey.js'
|
|
5
|
+
import { orderItems } from './orderItems.js'
|
|
4
6
|
import { logger } from '../../services/logger.js'
|
|
5
7
|
|
|
6
8
|
function getMatchingButtons ({ appState, buttonConfig, slot, evaluateProp }) {
|
|
@@ -74,47 +76,6 @@ function createButtonClickHandler (config, appState, evaluateProp) {
|
|
|
74
76
|
}
|
|
75
77
|
}
|
|
76
78
|
|
|
77
|
-
/**
|
|
78
|
-
* Resolves the group name from a button config's group property.
|
|
79
|
-
* Accepts either the new object form `{ name, label?, order? }` or a deprecated plain string.
|
|
80
|
-
* @param {string|{name: string, label?: string, order?: number}|null|undefined} group
|
|
81
|
-
* @returns {string|null}
|
|
82
|
-
*/
|
|
83
|
-
function resolveGroupName (group) {
|
|
84
|
-
if (group == null) {
|
|
85
|
-
return null
|
|
86
|
-
}
|
|
87
|
-
return typeof group === 'string' ? group : (group.name ?? null)
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
/**
|
|
91
|
-
* Resolves the accessible label for a group.
|
|
92
|
-
* Uses `label` if provided, otherwise falls back to `name`.
|
|
93
|
-
* @param {string|{name: string, label?: string, order?: number}|null|undefined} group
|
|
94
|
-
* @returns {string}
|
|
95
|
-
*/
|
|
96
|
-
function resolveGroupLabel (group) {
|
|
97
|
-
if (!group) {
|
|
98
|
-
return ''
|
|
99
|
-
}
|
|
100
|
-
if (typeof group === 'string') {
|
|
101
|
-
return group
|
|
102
|
-
}
|
|
103
|
-
return group.label ?? group.name ?? ''
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
/**
|
|
107
|
-
* Resolves the slot-level order for a group.
|
|
108
|
-
* @param {string|{name: string, label?: string, order?: number}|null|undefined} group
|
|
109
|
-
* @returns {number}
|
|
110
|
-
*/
|
|
111
|
-
function resolveGroupOrder (group) {
|
|
112
|
-
if (!group || typeof group === 'string') {
|
|
113
|
-
return 0
|
|
114
|
-
}
|
|
115
|
-
return group.order ?? 0
|
|
116
|
-
}
|
|
117
|
-
|
|
118
79
|
function applySlotExclusivity (matching, appState) {
|
|
119
80
|
let exclusivePluginId = null
|
|
120
81
|
|
|
@@ -181,6 +142,69 @@ function SlotButton ({ buttonId, config, appState, appConfig, evaluateProp }) {
|
|
|
181
142
|
)
|
|
182
143
|
}
|
|
183
144
|
|
|
145
|
+
/**
|
|
146
|
+
* Ungrouped buttons — one result item per button, ordered by its own breakpoint-level
|
|
147
|
+
* slot position.
|
|
148
|
+
*/
|
|
149
|
+
function buildUngroupedItems (members, ctx) {
|
|
150
|
+
return members.map(([buttonId, config]) => ({
|
|
151
|
+
id: buttonId,
|
|
152
|
+
type: 'button',
|
|
153
|
+
order: config[ctx.breakpoint]?.order ?? 0,
|
|
154
|
+
element: <SlotButton key={buttonId} {...slotButtonProps({ buttonId, config, ...ctx })} />
|
|
155
|
+
}))
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* A named group's own slot order/label come from whichever member is encountered first —
|
|
160
|
+
* unlike panel tabs (see groupIntoTabs.js), this is never derived from members' own order:
|
|
161
|
+
* groups render simultaneously (not one-at-a-time like tabs), so an unspecified order
|
|
162
|
+
* defaults to 0, same as every other slot item, rather than borrowing a member's position.
|
|
163
|
+
* A single-member group still degrades to a plain button (no wrapping container), but keeps
|
|
164
|
+
* the group's own slot order rather than falling back to its own breakpoint-level order.
|
|
165
|
+
*/
|
|
166
|
+
function buildGroupItem (key, members, ctx) {
|
|
167
|
+
const [, firstConfig] = members[0]
|
|
168
|
+
const order = firstConfig.group.slotOrder ?? 0
|
|
169
|
+
|
|
170
|
+
/* istanbul ignore next */
|
|
171
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
172
|
+
const distinctOrders = new Set(members.map(([, config]) => config.group?.slotOrder ?? 0))
|
|
173
|
+
if (distinctOrders.size > 1) {
|
|
174
|
+
logger.warn(`Button group "${firstConfig.group.label}" has inconsistent slotOrder values (${[...distinctOrders].join(', ')}) across its members — using ${order} (the first member's).`)
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
if (members.length < 2) {
|
|
179
|
+
const [buttonId, config] = members[0]
|
|
180
|
+
return {
|
|
181
|
+
id: buttonId,
|
|
182
|
+
type: 'button',
|
|
183
|
+
order,
|
|
184
|
+
element: <SlotButton key={buttonId} {...slotButtonProps({ buttonId, config, ...ctx })} />
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
// Order members within the group the same way panel/control content orders within a tab
|
|
189
|
+
const sorted = orderItems(members.map(([buttonId, config]) => ({
|
|
190
|
+
id: buttonId,
|
|
191
|
+
order: config[ctx.breakpoint]?.order ?? 0,
|
|
192
|
+
buttonId,
|
|
193
|
+
config
|
|
194
|
+
})))
|
|
195
|
+
|
|
196
|
+
return {
|
|
197
|
+
id: `group-${key}`,
|
|
198
|
+
type: 'group',
|
|
199
|
+
order,
|
|
200
|
+
element: (
|
|
201
|
+
<div key={`group-${key}`} role='group' aria-label={firstConfig.group.label} className='im-c-button-group'>{/* NOSONAR - div with role="group" is correct for a button group */}
|
|
202
|
+
{sorted.map(({ buttonId, config }) => <SlotButton key={buttonId} {...slotButtonProps({ buttonId, config, ...ctx })} />)}
|
|
203
|
+
</div>
|
|
204
|
+
)
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
184
208
|
function mapButtons ({ slot, appState, appConfig, evaluateProp }) {
|
|
185
209
|
const { buttonConfig, breakpoint } = appState
|
|
186
210
|
|
|
@@ -191,86 +215,17 @@ function mapButtons ({ slot, appState, appConfig, evaluateProp }) {
|
|
|
191
215
|
return []
|
|
192
216
|
}
|
|
193
217
|
|
|
194
|
-
// Partition
|
|
195
|
-
const
|
|
196
|
-
const
|
|
197
|
-
|
|
198
|
-
matching.forEach(([buttonId, config]) => {
|
|
199
|
-
const { group } = config
|
|
200
|
-
|
|
201
|
-
if (group == null) {
|
|
202
|
-
singletons.push([buttonId, config])
|
|
203
|
-
return
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
/* istanbul ignore next */
|
|
207
|
-
if (process.env.NODE_ENV !== 'production' && typeof group === 'string') {
|
|
208
|
-
logger.warn(`Button "${buttonId}": group should be an object { name, label?, order? } — string groups are deprecated.`)
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
const name = resolveGroupName(group)
|
|
212
|
-
const label = resolveGroupLabel(group)
|
|
213
|
-
const order = resolveGroupOrder(group)
|
|
214
|
-
|
|
215
|
-
if (groupMap.has(name)) {
|
|
216
|
-
const existing = groupMap.get(name)
|
|
217
|
-
/* istanbul ignore next */
|
|
218
|
-
if (process.env.NODE_ENV !== 'production' && existing.order !== order) {
|
|
219
|
-
logger.warn(`Group "${name}" has inconsistent order values (${existing.order} vs ${order}). Using the lower value.`)
|
|
220
|
-
existing.order = Math.min(existing.order, order)
|
|
221
|
-
}
|
|
222
|
-
} else {
|
|
223
|
-
groupMap.set(name, { label, order, members: [] })
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
groupMap.get(name).members.push([buttonId, config])
|
|
227
|
-
})
|
|
218
|
+
// Partition into named groups (keyed by kebab-cased group.label) plus one ungrouped bucket
|
|
219
|
+
const buckets = groupByKey({ items: matching, keyFn: ([, config]) => config.group?.label })
|
|
220
|
+
const ctx = { breakpoint, appState, appConfig, evaluateProp }
|
|
228
221
|
|
|
229
222
|
const result = []
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
result.push({
|
|
236
|
-
id: buttonId,
|
|
237
|
-
type: 'button',
|
|
238
|
-
order,
|
|
239
|
-
element: <SlotButton key={buttonId} {...slotButtonProps({ buttonId, config, appState, appConfig, evaluateProp })} />
|
|
240
|
-
})
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
for (const [groupName, { label, order: groupOrder, members }] of groupMap) {
|
|
244
|
-
if (members.length < 2) {
|
|
245
|
-
// Singleton group: degrade to a regular button using the group's slot order
|
|
246
|
-
const [buttonId, config] = members[0]
|
|
247
|
-
const order = groupOrder || config[breakpoint]?.order || 0
|
|
248
|
-
result.push({
|
|
249
|
-
id: buttonId,
|
|
250
|
-
type: 'button',
|
|
251
|
-
order,
|
|
252
|
-
element: <SlotButton key={buttonId} {...slotButtonProps({ buttonId, config, appState, appConfig, evaluateProp })} />
|
|
253
|
-
})
|
|
254
|
-
continue
|
|
223
|
+
for (const [key, members] of buckets) {
|
|
224
|
+
if (key === null) {
|
|
225
|
+
result.push(...buildUngroupedItems(members, ctx))
|
|
226
|
+
} else {
|
|
227
|
+
result.push(buildGroupItem(key, members, ctx))
|
|
255
228
|
}
|
|
256
|
-
|
|
257
|
-
// Sort group members by their intra-group order (breakpoint-level order prop)
|
|
258
|
-
const sorted = [...members].sort((a, b) => {
|
|
259
|
-
const orderA = a[1][breakpoint]?.order ?? 0
|
|
260
|
-
const orderB = b[1][breakpoint]?.order ?? 0
|
|
261
|
-
return orderA - orderB
|
|
262
|
-
})
|
|
263
|
-
|
|
264
|
-
result.push({
|
|
265
|
-
id: `group-${groupName}`,
|
|
266
|
-
type: 'group',
|
|
267
|
-
order: groupOrder,
|
|
268
|
-
element: (
|
|
269
|
-
<div key={`group-${groupName}`} role='group' aria-label={label} className='im-c-button-group'>{/* NOSONAR - div with role="group" is correct for a button group */}
|
|
270
|
-
{sorted.map(([buttonId, config]) => <SlotButton key={buttonId} {...slotButtonProps({ buttonId, config, appState, appConfig, evaluateProp })} />)}
|
|
271
|
-
</div>
|
|
272
|
-
)
|
|
273
|
-
})
|
|
274
229
|
}
|
|
275
230
|
|
|
276
231
|
return result
|
|
@@ -280,8 +235,5 @@ export {
|
|
|
280
235
|
mapButtons,
|
|
281
236
|
getMatchingButtons,
|
|
282
237
|
applySlotExclusivity,
|
|
283
|
-
SlotButton
|
|
284
|
-
resolveGroupName,
|
|
285
|
-
resolveGroupLabel,
|
|
286
|
-
resolveGroupOrder
|
|
238
|
+
SlotButton
|
|
287
239
|
}
|