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

Removes deprecated lifecyle method componentWillReceiveProps #51

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
37 changes: 17 additions & 20 deletions src/Autowhatever.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { Component } from 'react';
import React, { PureComponent } from 'react';
import PropTypes from 'prop-types';
import createSectionIterator from 'section-iterator';
import themeable from 'react-themeable';
Expand Down Expand Up @@ -26,7 +26,7 @@ const defaultTheme = {
sectionTitle: 'react-autowhatever__section-title'
};

export default class Autowhatever extends Component {
export default class Autowhatever extends PureComponent {
static propTypes = {
id: PropTypes.string, // Used in aria-* attributes. If multiple Autowhatever's are rendered on a page, they must have unique ids.
multiSection: PropTypes.bool, // Indicates whether a multi section layout should be rendered.
Expand Down Expand Up @@ -82,51 +82,45 @@ export default class Autowhatever extends Component {
this.state = {
isInputFocused: false
};

this.setSectionsItems(props);
this.setSectionIterator(props);
this.setTheme(props);
}

componentDidMount() {
this.ensureHighlightedItemIsVisible();
}

componentWillReceiveProps(nextProps) {
if (nextProps.items !== this.props.items) {
this.setSectionsItems(nextProps);
}

if (nextProps.items !== this.props.items || nextProps.multiSection !== this.props.multiSection) {
this.setSectionIterator(nextProps);
}

if (nextProps.theme !== this.props.theme) {
this.setTheme(nextProps);
}
}

componentDidUpdate() {
this.ensureHighlightedItemIsVisible();
}

setSectionsItems(props) {
if (props.items === this.latestItems) {
return;
}
if (props.multiSection) {
this.sectionsItems = props.items.map(section => props.getSectionItems(section));
this.sectionsLengths = this.sectionsItems.map(items => items.length);
this.allSectionsAreEmpty = this.sectionsLengths.every(itemsCount => itemsCount === 0);
}
this.latestItems = props.items;
}

setSectionIterator(props) {
if (this.latestItems === props.items && this.latestMultiSection === props.multiSection) {
return;
}
this.sectionIterator = createSectionIterator({
multiSection: props.multiSection,
data: props.multiSection ? this.sectionsLengths : props.items.length
});
this.latestMultiSection = props.multiSection;
}

setTheme(props) {
if (props.theme === this.latestTheme) {
return;
}
this.theme = themeable(props.theme);
this.latestTheme = props.theme;
}

storeInputReference = input => {
Expand Down Expand Up @@ -297,6 +291,9 @@ export default class Autowhatever extends Component {
}

render() {
this.setSectionsItems(this.props);
this.setSectionIterator(this.props);
this.setTheme(this.props);
const { theme } = this;
const {
id, multiSection, renderInputComponent, renderItemsContainer,
Expand Down