import React from 'react';
import PropTypes from 'prop-types';
import uniqueId from 'lodash/uniqueId';
import { Consumer as AccordionConsumer } from './AccordionContext';

const withAccordionSet = (Component) => {
  const WrappedAccordion = (props) => {
    const trackingId = uniqueId('accordion_');
    return (
      <AccordionConsumer>
        {status => {
          if (status) {
            const uId = props.id || trackingId;
            return (<Component
              accordionSet={status.set}
              open={status.expanded[uId]}
              id={uId}
              onToggle={status.set.handleToggle}
              toggleRef={status.set.registerAccordion}
              toggleKeyHandlers={status.set.toggleKeyHandlers}
              toggleKeyMap={status.set.toggleKeyMap}
              {...props}
            />);
          }
          return <Component {...props} />;
        }
        }
      </AccordionConsumer>
    );
  };

  WrappedAccordion.propTypes = {
    id: PropTypes.string,
  };

  return WrappedAccordion;
};

export default withAccordionSet;

import React from 'react';
import PropTypes from 'prop-types';
import uniqueId from 'lodash/uniqueId';
import { Consumer as AccordionConsumer } from './AccordionContext';

const withAccordionSet = (Component) => {
  class WrappedAccordion extends Component {
    static propTypes = {
      id: PropTypes.string,
    }

    constructor(props) {
      super(props);
      this.trackingId = uniqueId('accordion_');
    }

    render() {
      return (
        <AccordionConsumer>
          {status => {
            if (status) {
              const uId = this.props.id || this.trackingId;
              return (<Component
                accordionSet={status.set}
                open={status.expanded[uId]}
                id={uId}
                onToggle={status.set.handleToggle}
                toggleRef={status.set.registerAccordion}
                toggleKeyHandlers={status.set.toggleKeyHandlers}
                toggleKeyMap={status.set.toggleKeyMap}
                {...this.props}
              />);
            }
            return <Component {...this.props} />;
          }
          }
        </AccordionConsumer>
      );
    }
  }

  return WrappedAccordion;
};

export default withAccordionSet;

Revert "Revert "Converted wrappedComponent into a class to be able to place autogeneration of ID's in the constructor, preventing it from generating new ID's on every re-render (#881)" (#885)" (#886)