File tree Expand file tree Collapse file tree 3 files changed +60
-0
lines changed
packages/patternfly-4/react-core/src/internal/GenerateId Expand file tree Collapse file tree 3 files changed +60
-0
lines changed Original file line number Diff line number Diff line change 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 ;
Original file line number Diff line number Diff line change 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+ } ) ;
Original file line number Diff line number Diff line change 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+ ` ;
You can’t perform that action at this time.
0 commit comments