Skip to content

Commit ec270e2

Browse files
authored
Stringify ids in child components (AccordionItem, Tabs) (dbc-team#937)
1 parent e2e644c commit ec270e2

File tree

3 files changed

+25
-5
lines changed

3 files changed

+25
-5
lines changed

src/components/accordion/Accordion.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ import PropTypes from 'prop-types';
33
import {omit} from 'ramda';
44
import RBAccordion from 'react-bootstrap/Accordion';
55

6-
import {parseChildrenToArray, resolveChildProps} from '../../private/util';
6+
import {
7+
parseChildrenToArray,
8+
resolveChildProps,
9+
stringifyId
10+
} from '../../private/util';
711

812
/**
913
* A self contained Accordion component. Build up the children using the
@@ -59,17 +63,18 @@ const Accordion = props => {
5963
children.map((child, idx) => {
6064
const childProps = resolveChildProps(child);
6165
const {
62-
children,
6366
title,
6467
item_id,
6568
loading_state,
6669
class_name,
6770
className,
71+
id,
6872
...otherProps
6973
} = childProps;
7074
const itemID = item_id || 'item-' + idx;
7175
return (
7276
<RBAccordion.Item
77+
id={stringifyId(id)}
7378
key={itemID}
7479
eventKey={itemID}
7580
className={class_name || className}

src/components/tabs/Tabs.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ import classnames from 'classnames';
55
import RBNav from 'react-bootstrap/Nav';
66
import RBTab from 'react-bootstrap/Tab';
77

8-
import {parseChildrenToArray, resolveChildProps} from '../../private/util';
8+
import {
9+
parseChildrenToArray,
10+
resolveChildProps,
11+
stringifyId
12+
} from '../../private/util';
913

1014
/**
1115
* Create Bootstrap styled tabs. Use the `active_tab` property to set, or get
@@ -52,7 +56,7 @@ const Tabs = props => {
5256
const active = active_tab === tabId;
5357
return (
5458
<RBNav.Item
55-
id={childProps.id}
59+
id={stringifyId(childProps.id)}
5660
key={tabId}
5761
style={
5862
active

src/private/util.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,15 @@ const sanitizeOptions = options => {
6262
return options;
6363
};
6464

65-
export {parseChildrenToArray, resolveChildProps, sanitizeOptions};
65+
const stringifyId = id => {
66+
if (typeof id !== 'object') {
67+
return id;
68+
}
69+
const stringifyVal = v => (v && v.wild) || JSON.stringify(v);
70+
const parts = Object.keys(id)
71+
.sort()
72+
.map(k => JSON.stringify(k) + ':' + stringifyVal(id[k]));
73+
return '{' + parts.join(',') + '}';
74+
};
75+
76+
export {parseChildrenToArray, resolveChildProps, sanitizeOptions, stringifyId};

0 commit comments

Comments
 (0)