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

fix: refactor code to use getDerivedStateFromProps #735

Open
wants to merge 2 commits 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
204 changes: 118 additions & 86 deletions src/Autosuggest.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,51 @@ const defaultShouldRenderSuggestions = (value) => value.trim().length > 0;
const defaultRenderSuggestionsContainer = ({ containerProps, children }) => (
<div {...containerProps}>{children}</div>
);
const updateHighlightedSuggestionAux = (
state,
props,
sectionIndex,
suggestionIndex,
prevValue
) => {
let { valueBeforeUpDown } = state;
const { getSectionSuggestions, suggestions, multiSection } = props;

if (suggestionIndex === null) {
valueBeforeUpDown = null;
} else if (valueBeforeUpDown === null && typeof prevValue !== 'undefined') {
valueBeforeUpDown = prevValue;
}

return {
highlightedSectionIndex: multiSection ? sectionIndex : null,
highlightedSuggestionIndex: suggestionIndex,
highlightedSuggestion:
suggestionIndex === null
? null
: multiSection
? getSectionSuggestions(suggestions[sectionIndex])[suggestionIndex]
: suggestions[suggestionIndex],
valueBeforeUpDown,
};
};
const willRenderSuggestionsAux = (props) => {
const { suggestions, inputProps, shouldRenderSuggestions } = props;
const { value } = inputProps;

return suggestions.length > 0 && shouldRenderSuggestions(value);
};

const resetHighlightedSuggestionAux = (state, shouldResetValueBeforeUpDown) => {
const { valueBeforeUpDown } = state;

return {
highlightedSectionIndex: null,
highlightedSuggestionIndex: null,
highlightedSuggestion: null,
valueBeforeUpDown: shouldResetValueBeforeUpDown ? null : valueBeforeUpDown,
};
};

export default class Autosuggest extends Component {
static propTypes = {
Expand Down Expand Up @@ -95,52 +140,75 @@ export default class Autosuggest extends Component {
id: '1',
};

constructor({ alwaysRenderSuggestions }) {
constructor(props) {
super();

this.state = {
isFocused: false,
isCollapsed: !alwaysRenderSuggestions,
isCollapsed: !props.alwaysRenderSuggestions,
highlightedSectionIndex: null,
highlightedSuggestionIndex: null,
highlightedSuggestion: null,
valueBeforeUpDown: null,
prevSuggestions: props.suggestions,
justPressedUpDown: false,
justMouseEntered: false,
justSelectedSuggestion: false,
};

this.justPressedUpDown = false;
this.justMouseEntered = false;

this.pressedSuggestion = null;
}

componentDidMount() {
document.addEventListener('mousedown', this.onDocumentMouseDown);
document.addEventListener('mouseup', this.onDocumentMouseUp);

this.input = this.autowhatever.input;
this.suggestionsContainer = this.autowhatever.itemsContainer;
}
static getDerivedStateFromProps(nextProps, prevState) {
const {
justPressedUpDown,
justMouseEntered,
justSelectedSuggestion,
prevSuggestions,
isCollapsed,
} = prevState;
const { suggestions, highlightFirstSuggestion } = nextProps;
const derivedState = {
justMouseEntered: false,
justPressedUpDown: false,
prevSuggestions: suggestions,
};

// eslint-disable-next-line camelcase, react/sort-comp
UNSAFE_componentWillReceiveProps(nextProps) {
if (shallowEqualArrays(nextProps.suggestions, this.props.suggestions)) {
if (shallowEqualArrays(suggestions, prevSuggestions)) {
if (
nextProps.highlightFirstSuggestion &&
nextProps.suggestions.length > 0 &&
this.justPressedUpDown === false &&
this.justMouseEntered === false
suggestions.length > 0 &&
highlightFirstSuggestion &&
justMouseEntered === false &&
justPressedUpDown === false
) {
this.highlightFirstSuggestion();
return {
...derivedState,
...updateHighlightedSuggestionAux(prevState, nextProps, 0, 0),
};
}
} else {
if (this.willRenderSuggestions(nextProps)) {
if (this.state.isCollapsed && !this.justSelectedSuggestion) {
this.revealSuggestions();
}
if (
willRenderSuggestionsAux(nextProps) &&
isCollapsed &&
!justSelectedSuggestion
) {
return { ...derivedState, ...{ isCollapsed: false } };
} else {
this.resetHighlightedSuggestion();
return {
...derivedState,
...resetHighlightedSuggestionAux(prevState, null),
};
}
}
return null;
}

componentDidMount() {
document.addEventListener('mousedown', this.onDocumentMouseDown);
document.addEventListener('mouseup', this.onDocumentMouseUp);

this.input = this.autowhatever.input;
this.suggestionsContainer = this.autowhatever.itemsContainer;
}

componentDidUpdate(prevProps, prevState) {
Expand Down Expand Up @@ -178,41 +246,19 @@ export default class Autosuggest extends Component {

updateHighlightedSuggestion(sectionIndex, suggestionIndex, prevValue) {
this.setState((state) => {
let { valueBeforeUpDown } = state;

if (suggestionIndex === null) {
valueBeforeUpDown = null;
} else if (
valueBeforeUpDown === null &&
typeof prevValue !== 'undefined'
) {
valueBeforeUpDown = prevValue;
}

return {
highlightedSectionIndex: sectionIndex,
highlightedSuggestionIndex: suggestionIndex,
highlightedSuggestion:
suggestionIndex === null
? null
: this.getSuggestion(sectionIndex, suggestionIndex),
valueBeforeUpDown,
};
return updateHighlightedSuggestionAux(
state,
this.props,
sectionIndex,
suggestionIndex,
prevValue
);
});
}

resetHighlightedSuggestion(shouldResetValueBeforeUpDown = true) {
this.setState((state) => {
const { valueBeforeUpDown } = state;

return {
highlightedSectionIndex: null,
highlightedSuggestionIndex: null,
highlightedSuggestion: null,
valueBeforeUpDown: shouldResetValueBeforeUpDown
? null
: valueBeforeUpDown,
};
return resetHighlightedSuggestionAux(state, shouldResetValueBeforeUpDown);
});
}

Expand Down Expand Up @@ -329,10 +375,7 @@ export default class Autosuggest extends Component {
}

willRenderSuggestions(props) {
const { suggestions, inputProps, shouldRenderSuggestions } = props;
const { value } = inputProps;

return suggestions.length > 0 && shouldRenderSuggestions(value);
return willRenderSuggestionsAux(props);
}

storeAutowhateverRef = (autowhatever) => {
Expand All @@ -343,24 +386,21 @@ export default class Autosuggest extends Component {

onSuggestionMouseEnter = (event, { sectionIndex, itemIndex }) => {
this.updateHighlightedSuggestion(sectionIndex, itemIndex);
let { justSelectedSuggestion } = this.state;

if (event.target === this.pressedSuggestion) {
this.justSelectedSuggestion = true;
justSelectedSuggestion = true;
}

this.justMouseEntered = true;

setTimeout(() => {
this.justMouseEntered = false;
});
this.setState({ justMouseEntered: true, justSelectedSuggestion });
};

highlightFirstSuggestion = () => {
this.updateHighlightedSuggestion(this.props.multiSection ? 0 : null, 0);
};

onDocumentMouseUp = () => {
if (this.pressedSuggestion && !this.justSelectedSuggestion) {
if (this.pressedSuggestion && !this.state.justSelectedSuggestion) {
this.input.focus();
}
this.pressedSuggestion = null;
Expand All @@ -369,9 +409,9 @@ export default class Autosuggest extends Component {
onSuggestionMouseDown = (event) => {
// Checking if this.justSelectedSuggestion is already true to not duplicate touch events in chrome
// See: https://github.com/facebook/react/issues/9809#issuecomment-413978405
if (!this.justSelectedSuggestion) {
this.justSelectedSuggestion = true;
if (!this.state.justSelectedSuggestion) {
this.pressedSuggestion = event.target;
this.setState({ justSelectedSuggestion: true });
}
};

Expand Down Expand Up @@ -431,8 +471,8 @@ export default class Autosuggest extends Component {
this.onBlur();
}

setTimeout(() => {
this.justSelectedSuggestion = false;
this.setState({
justSelectedSuggestion: false,
});
};

Expand All @@ -458,21 +498,21 @@ export default class Autosuggest extends Component {
this.resetHighlightedSuggestion(false); // shouldResetValueBeforeUpDown

if (
this.justSelectedSuggestion &&
this.state.justSelectedSuggestion &&
event.target === this.pressedSuggestion
) {
this.justSelectedSuggestion = false;
this.setState({ justSelectedSuggestion: false });
}
};

onSuggestionTouchStart = () => {
this.justSelectedSuggestion = true;
this.setState({ justSelectedSuggestion: true });
// todo: event.preventDefault when https://github.com/facebook/react/issues/2043
// todo: gets released so onSuggestionMouseDown won't fire in chrome
};

onSuggestionTouchMove = () => {
this.justSelectedSuggestion = false;
this.setState({ justSelectedSuggestion: false });
this.pressedSuggestion = null;
this.input.focus();
};
Expand Down Expand Up @@ -544,7 +584,7 @@ export default class Autosuggest extends Component {
...inputProps,
onFocus: (event) => {
if (
!this.justSelectedSuggestion &&
!this.state.justSelectedSuggestion &&
!this.justClickedOnSuggestionsContainer
) {
const shouldRender = shouldRenderSuggestions(value);
Expand All @@ -569,7 +609,7 @@ export default class Autosuggest extends Component {

this.blurEvent = event;

if (!this.justSelectedSuggestion) {
if (!this.state.justSelectedSuggestion) {
this.onBlur();
this.onSuggestionsClearRequested();
}
Expand Down Expand Up @@ -651,11 +691,7 @@ export default class Autosuggest extends Component {

event.preventDefault(); // Prevents the cursor from moving

this.justPressedUpDown = true;

setTimeout(() => {
this.justPressedUpDown = false;
});
this.setState({ justPressedUpDown: true });

break;

Expand Down Expand Up @@ -685,11 +721,7 @@ export default class Autosuggest extends Component {
method: 'enter',
});

this.justSelectedSuggestion = true;

setTimeout(() => {
this.justSelectedSuggestion = false;
});
this.setState({ justSelectedSuggestion: true });
}

break;
Expand Down