diff --git a/app/src/ui/lib/filter-list.tsx b/app/src/ui/lib/filter-list.tsx index 5de539a785..da8d0b78d3 100644 --- a/app/src/ui/lib/filter-list.tsx +++ b/app/src/ui/lib/filter-list.tsx @@ -194,28 +194,31 @@ export class FilterList extends React.Component< IFilterListProps, IFilterListState > { + public static getDerivedStateFromProps( + props: IFilterListProps, + state: IFilterListState + ) { + return createStateUpdate(props, state) + } + + public state: IFilterListState = { + rows: new Array>(), + selectedRow: -1, + filterValue: '', + filterValueChanged: false, + } + private list: List | null = null private filterTextBox: TextBox | null = null public constructor(props: IFilterListProps) { super(props) - this.state = { - filterValueChanged: false, - ...createStateUpdate(props), + if (props.filterTextBox !== undefined) { + this.filterTextBox = props.filterTextBox } } - public componentWillMount() { - if (this.props.filterTextBox !== undefined) { - this.filterTextBox = this.props.filterTextBox - } - } - - public componentWillReceiveProps(nextProps: IFilterListProps) { - this.setState(createStateUpdate(nextProps)) - } - public componentDidUpdate( prevProps: IFilterListProps, prevState: IFilterListState @@ -596,7 +599,8 @@ export function getText( } function createStateUpdate( - props: IFilterListProps + props: IFilterListProps, + state: IFilterListState ) { const flattenedRows = new Array>() const filter = (props.filterText || '').toLowerCase() @@ -637,7 +641,18 @@ function createStateUpdate( selectedRow = flattenedRows.findIndex(i => i.kind === 'item') } - return { rows: flattenedRows, selectedRow, filterValue: filter } + let filterChanged = state.filterValueChanged + + if (!filterChanged && filter.length) { + filterChanged = true + } + + return { + rows: flattenedRows, + selectedRow, + filterValue: filter, + filterValueChanged: filterChanged, + } } function getItemFromRowIndex( diff --git a/app/src/ui/lib/section-filter-list.tsx b/app/src/ui/lib/section-filter-list.tsx index a4a684fbb1..641324d928 100644 --- a/app/src/ui/lib/section-filter-list.tsx +++ b/app/src/ui/lib/section-filter-list.tsx @@ -175,28 +175,32 @@ interface IFilterListState { export class SectionFilterList< T extends IFilterListItem > extends React.Component, IFilterListState> { + public static getDerivedStateFromProps( + props: ISectionFilterListProps, + state: IFilterListState + ) { + return createStateUpdate(props, state) + } + + public state: IFilterListState = { + rows: new Array>>(), + selectedRow: InvalidRowIndexPath, + filterValue: '', + filterValueChanged: false, + groups: [], + } + private list: SectionList | null = null private filterTextBox: TextBox | null = null public constructor(props: ISectionFilterListProps) { super(props) - this.state = { - filterValueChanged: false, - ...createStateUpdate(props), + if (props.filterTextBox !== undefined) { + this.filterTextBox = props.filterTextBox } } - public componentWillMount() { - if (this.props.filterTextBox !== undefined) { - this.filterTextBox = this.props.filterTextBox - } - } - - public componentWillReceiveProps(nextProps: ISectionFilterListProps) { - this.setState(createStateUpdate(nextProps)) - } - public componentDidUpdate( prevProps: ISectionFilterListProps, prevState: IFilterListState @@ -633,7 +637,8 @@ function getFirstVisibleRow( } function createStateUpdate( - props: ISectionFilterListProps + props: ISectionFilterListProps, + state: IFilterListState ) { const rows = new Array>>() const filter = (props.filterText || '').toLowerCase() @@ -683,7 +688,19 @@ function createStateUpdate( selectedRow = getFirstVisibleRow(rows) } - return { rows: rows, selectedRow, filterValue: filter, groups: groupIndices } + let filterChanged = state.filterValueChanged + + if (!filterChanged && filter.length) { + filterChanged = true + } + + return { + rows: rows, + selectedRow, + filterValue: filter, + filterValueChanged: filterChanged, + groups: groupIndices, + } } function getItemFromRowIndex(