diff --git a/UNRELEASED.md b/UNRELEASED.md index 8d14e8526f7..1194311b7fb 100644 --- a/UNRELEASED.md +++ b/UNRELEASED.md @@ -20,6 +20,9 @@ Use [the changelog guidelines](https://git.io/polaris-changelog-guidelines) to f ### Code quality +- Migrated `FilterValueSelector` to use hooks instead of withAppProvider ([#2156](https://github.com/Shopify/polaris-react/pull/2156)) +- Added `useIsMountedRef` hook to use while building components ([#2167](https://github.com/Shopify/polaris-react/pull/2167)) + ### Deprecations ### Development workflow diff --git a/src/components/ResourceList/components/FilterControl/components/FilterValueSelector/FilterValueSelector.tsx b/src/components/ResourceList/components/FilterControl/components/FilterValueSelector/FilterValueSelector.tsx index db22b8a3395..0f4c38b0ff2 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.current) { + 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} +