From 7053b5071794bdf78a79447e0d44023f7b767bc6 Mon Sep 17 00:00:00 2001 From: Andrew Musgrave Date: Mon, 30 Sep 2019 16:28:49 -0400 Subject: [PATCH 1/3] Convert filter value selector to a functional component and add useIsMountedRef --- .../FilterValueSelector.tsx | 196 ++++++++---------- .../components/FilterValueSelector/index.ts | 5 +- .../tests/FilterValueSelector.test.tsx | 2 +- .../tests/use-is-mounted-ref.test.tsx | 58 ++++++ src/utilities/use-is-mounted-ref.ts | 19 ++ 5 files changed, 167 insertions(+), 113 deletions(-) create mode 100644 src/utilities/tests/use-is-mounted-ref.test.tsx create mode 100644 src/utilities/use-is-mounted-ref.ts diff --git a/src/components/ResourceList/components/FilterControl/components/FilterValueSelector/FilterValueSelector.tsx b/src/components/ResourceList/components/FilterControl/components/FilterValueSelector/FilterValueSelector.tsx index db22b8a3395..db61c88404c 100644 --- a/src/components/ResourceList/components/FilterControl/components/FilterValueSelector/FilterValueSelector.tsx +++ b/src/components/ResourceList/components/FilterControl/components/FilterValueSelector/FilterValueSelector.tsx @@ -1,13 +1,11 @@ -import React from 'react'; +import React, {useCallback} from 'react'; import {Select} from '../../../../../Select'; import {Stack} from '../../../../../Stack'; import {TextField} from '../../../../../TextField'; -import { - withAppProvider, - WithAppProviderProps, -} from '../../../../../../utilities/with-app-provider'; import {DateSelector} from '../DateSelector'; import {Filter, AppliedFilter, FilterType, Operator} from '../../types'; +import {useI18n} from '../../../../../../utilities/i18n'; +import {useIsMountedRef} from '../../../../../../utilities/use-is-mounted-ref'; export interface FilterValueSelectorProps { filter: Filter; @@ -17,110 +15,94 @@ export interface FilterValueSelectorProps { onFilterKeyChange(filterKey: string): void; } -type CombinedProps = FilterValueSelectorProps & WithAppProviderProps; - -class FilterValueSelector extends React.PureComponent { - componentDidMount() { - const { - filter: {operatorText, type}, - } = this.props; - - if ( - type === FilterType.DateSelector || - !operatorText || - typeof operatorText === 'string' || - operatorText.length === 0 - ) { - return; - } - - this.handleOperatorOptionChange(operatorText[0].key); +export function FilterValueSelector({ + filter, + filterKey, + value, + onChange, + onFilterKeyChange, +}: FilterValueSelectorProps) { + const {translate} = useI18n(); + const isMounted = useIsMountedRef(); + const {operatorText, type, label} = filter; + const showOperatorOptions = + type !== FilterType.DateSelector && + operatorText && + typeof operatorText !== 'string'; + + const handleOperatorOptionChange = useCallback( + (operatorKey: string) => { + onFilterKeyChange(operatorKey); + + if (!value) { + return; + } + + onChange(value); + }, + [onChange, onFilterKeyChange, value], + ); + + if (showOperatorOptions && operatorText!.length !== 0 && !isMounted) { + handleOperatorOptionChange((operatorText as Operator[])[0].key); } - render() { - const { - filter, - filterKey, - value, - onChange, - onFilterKeyChange, - polaris: {intl}, - } = this.props; - - const {operatorText} = filter; - - const showOperatorOptions = - filter.type !== FilterType.DateSelector && - operatorText && - typeof operatorText !== 'string'; - const operatorOptionsMarkup = showOperatorOptions ? ( - - - ); - case FilterType.TextField: - return ( - - {operatorOptionsMarkup} - - - ); - case FilterType.DateSelector: - return ( - + ) : null; + + const selectedFilterLabel = + typeof operatorText === 'string' ? operatorText : ''; + + switch (filter.type) { + case FilterType.Select: + return ( + + {operatorOptionsMarkup} +