Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore/accordion item title refactor #101

Merged
merged 5 commits into from
Jun 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 10 additions & 12 deletions src/AccordionContainer/AccordionContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,21 @@ class AccordionContainer extends Container<StoreState> {
};
}

setAccordion(accordion: boolean) {
setAccordion = (accordion: boolean) => {
if (accordion !== this.state.accordion) {
return this.setState({ accordion });
}
return null;
}
};

setOnChange(onChange: Function) {
setOnChange = (onChange: Function) => {
if (onChange !== this.state.onChange) {
return this.setState({ onChange });
}
return null;
}
};

addItem(newItem: Item) {
addItem = (newItem: Item) => {
// Need to use callback style otherwise race-conditions are created by concurrent registrations.
this.setState(state => {
let items;
Expand Down Expand Up @@ -67,16 +67,15 @@ class AccordionContainer extends Container<StoreState> {
items,
};
});
}
};

removeItem(key: string | number) {
return this.setState(state => ({
removeItem = (key: string | number) =>
this.setState(state => ({
items: state.items.filter(item => item.uuid !== key),
}));
}

setExpanded(key: string | number, expanded: boolean) {
return this.setState(state => ({
setExpanded = (key: string | number, expanded: boolean) =>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ran into a javascript context issue where this didn't equal what I wanted it to equal, so using arrow functions here to bind the correct context.

this.setState(state => ({
items: state.items.map(item => {
if (item.uuid === key) {
return {
Expand All @@ -103,7 +102,6 @@ class AccordionContainer extends Container<StoreState> {
);
}
});
}
}

export default AccordionContainer;
21 changes: 14 additions & 7 deletions src/AccordionItemTitle/accordion-item-title-wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,20 @@ type AccordionItemTitleWrapperProps = ElementProps<'div'> & {

const AccordionItemTitleWrapper = (props: AccordionItemTitleWrapperProps) => (
<Subscribe to={[AccordionContainer, ItemContainer]}>
{(accordionStore, itemStore) => (
<AccordionItemTitle
{...props}
uuid={itemStore.state.uuid}
accordionStore={accordionStore}
/>
)}
{(accordionStore, itemStore) => {
const { uuid } = itemStore.state;
const { items, accordion } = accordionStore.state;
const item = items.find(stateItem => stateItem.uuid === uuid);

return (
<AccordionItemTitle
{...props}
{...item}
setExpanded={accordionStore.setExpanded}
accordion={accordion}
/>
);
}}
</Subscribe>
);
AccordionItemTitleWrapper.defaultProps = defaultProps;
Expand Down
24 changes: 12 additions & 12 deletions src/AccordionItemTitle/accordion-item-title.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import classNames from 'classnames';

type AccordionItemTitleProps = ElementProps<'div'> & {
hideBodyClassName: string,
expanded: boolean,
uuid: string | number,
disabled: boolean,
accordion: boolean,
setExpanded: (string | number, boolean) => any,
};

type AccordionItemTitleState = {};
Expand All @@ -17,12 +21,9 @@ class AccordionItemTitle extends Component<
static accordionElementName = 'AccordionItemTitle';

handleClick = () => {
const { uuid, accordionStore } = this.props;
const { state } = accordionStore;
const { items } = state;
const foundItem = items.find(item => item.uuid === uuid);
const { uuid, expanded, setExpanded } = this.props;

accordionStore.setExpanded(foundItem.uuid, !foundItem.expanded);
setExpanded(uuid, !expanded);
};

handleKeyPress = (evt: SyntheticKeyboardEvent<HTMLButtonElement>) => {
Expand All @@ -34,18 +35,17 @@ class AccordionItemTitle extends Component<

render() {
const {
state: { items, accordion },
} = this.props.accordionStore;
const {
uuid,
className,
hideBodyClassName,
accordionStore,
item,
accordion,
setExpanded,
expanded,
uuid,
disabled,
...rest
} = this.props;
const foundItem = items.find(item => item.uuid === uuid);

const { expanded, disabled } = foundItem;

const id = `accordion__title-${uuid}`;
const ariaControls = `accordion__body-${uuid}`;
Expand Down