Skip to content

Commit

Permalink
switch to useMemo in ExpandAllButton
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnC-80 committed Oct 23, 2023
1 parent 9afa4ee commit 4ad1b07
Showing 1 changed file with 4 additions and 7 deletions.
11 changes: 4 additions & 7 deletions lib/Accordion/ExpandAllButton.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
import React from 'react';
import React, { useMemo } from 'react';
import { flushSync } from 'react-dom';
import PropTypes from 'prop-types';
import { FormattedMessage } from 'react-intl';
import isEqual from 'lodash/isEqual';
import memoizeOne from 'memoize-one';
import expandAll from './expandCollapseAll';
import Button from '../Button';
import { withAccordionStatus } from './AccordionStatusContext';

// returns true if more keys in status are set to false than true;
const majorityCollapsed = memoizeOne((status) => {
const majorityCollapsed = (status) => {
const marjority = Object.keys(status).reduce(
(accum, id) => (status[id] ? accum + 1 : accum - 1),
0
);
return marjority < 0;
}, isEqual);
};

const propTypes = {
accordionStatus: PropTypes.object,
Expand Down Expand Up @@ -44,13 +42,12 @@ const ExpandAllButton = ({
onToggle,
setStatus,
}) => {
const func = useMemo(majorityCollapsed(accordionStatus), Object.values(accordionStatus));
const expandLabel = expandLabelProp ||
<FormattedMessage id="stripes-components.expandAll" />;
const collapseLabel = collapseLabelProp ||
<FormattedMessage id="stripes-components.collapseAll" />;

const func = majorityCollapsed(accordionStatus);

return (
<Button
buttonStyle="link bottomMargin0"
Expand Down

0 comments on commit 4ad1b07

Please sign in to comment.