2019-07-29 12:00:26 +00:00
|
|
|
import { Component } from 'react';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* HOC that gives a component a unique ID.
|
|
|
|
*
|
2019-09-07 12:04:34 +00:00
|
|
|
* This is an alternative for withInstanceId from @wordpress/compose to avoid
|
|
|
|
* using that dependency on the frontend.
|
2019-07-29 12:00:26 +00:00
|
|
|
*/
|
|
|
|
const withComponentId = ( OriginalComponent ) => {
|
2019-09-07 12:04:34 +00:00
|
|
|
let instances = 0;
|
2019-07-29 12:00:26 +00:00
|
|
|
|
2019-09-07 12:04:34 +00:00
|
|
|
class WrappedComponent extends Component {
|
|
|
|
instanceId = instances++;
|
2019-07-29 12:00:26 +00:00
|
|
|
|
|
|
|
render() {
|
2019-09-05 15:09:31 +00:00
|
|
|
return (
|
2019-09-07 12:04:34 +00:00
|
|
|
<OriginalComponent
|
|
|
|
{ ...this.props }
|
|
|
|
componentId={ this.instanceId }
|
|
|
|
/>
|
2019-09-05 15:09:31 +00:00
|
|
|
);
|
2019-07-29 12:00:26 +00:00
|
|
|
}
|
2019-09-07 12:04:34 +00:00
|
|
|
}
|
|
|
|
WrappedComponent.displayName = 'withComponentId';
|
|
|
|
return WrappedComponent;
|
2019-07-29 12:00:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default withComponentId;
|