package.src.helpers.GenerateId.GenerateId.ts Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of react-core Show documentation
Show all versions of react-core Show documentation
This library provides a set of common React components for use with the PatternFly reference implementation.
The newest version!
/** This Component can be used to wrap a functional component in order to generate a random ID
* Example of how to use this component
*
* const Component = ({id}: {id: string}) => (
* {randomId => (
*
* div with random ID
*
* )}
*
* );
*/
import * as React from 'react';
let currentId = 0;
interface GenerateIdProps {
/** String to prefix the random id with */
prefix?: string;
/** Component to be rendered with the generated id */
children(id: string): React.ReactNode;
}
class GenerateId extends React.Component {
static displayName = 'GenerateId';
static defaultProps = {
prefix: 'pf-random-id-'
};
id = `${this.props.prefix}${currentId++}`;
render() {
return this.props.children(this.id);
}
}
export { GenerateId };