Skip to content

Commit 159798e

Browse files
tlabajjschuler
authored andcommitted
chore(patternfly-4/react-core): Added a Wrapper component that will g… (#832)
Added a Wrapper component that will generate a random id for the chi
1 parent 42a64a4 commit 159798e

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/** This Component can be used to wrap a functional component in order to generate a random ID
2+
* Example of how to use this component
3+
*
4+
* const Component = ({id}) => (
5+
* <GenerateId>{randomId => (
6+
* <div id={id || randomId}>
7+
* div with random ID
8+
* </div>
9+
* )}
10+
* </GenerateId>
11+
* );
12+
*
13+
* Component.propTypes = {
14+
* id: PropTypes.string
15+
* }
16+
*/
17+
18+
import React from 'react';
19+
import PropTypes from 'prop-types';
20+
21+
let currentId = 0;
22+
23+
const propTypes = {
24+
/** String to prefix the random id with */
25+
prefix: PropTypes.string,
26+
/** Component to be rendered with the generated id */
27+
children: PropTypes.func.isRequired
28+
};
29+
30+
class GenerateId extends React.Component {
31+
static defaultProps = {
32+
prefix: 'pf-random-id-'
33+
};
34+
static propTypes = propTypes;
35+
id = `${this.props.prefix}${currentId++}`;
36+
37+
render() {
38+
return this.props.children(this.id);
39+
}
40+
}
41+
42+
export default GenerateId;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import React from 'react';
2+
import { shallow } from 'enzyme';
3+
import GenerateId from './GenerateId';
4+
5+
test('generates id', () => {
6+
const view = shallow(<GenerateId>{id => <div id={id}>div with random ID</div>}</GenerateId>);
7+
8+
expect(view).toMatchSnapshot();
9+
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`generates id 1`] = `
4+
<div
5+
id="pf-random-id-0"
6+
>
7+
div with random ID
8+
</div>
9+
`;

0 commit comments

Comments
 (0)