Skip to content

Commit

Permalink
feat(native-filters): sort selected values on blur (apache#14842)
Browse files Browse the repository at this point in the history
* feat(native-filters): sort selected values on blur

* remove new icons
  • Loading branch information
villebro authored May 27, 2021
1 parent 8ce89b6 commit 2c46c3f
Showing 1 changed file with 34 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import {
AppSection,
DataMask,
DataRecord,
ensureIsArray,
ExtraFormData,
GenericDataType,
Expand All @@ -27,7 +28,13 @@ import {
t,
tn,
} from '@superset-ui/core';
import React, { useCallback, useEffect, useReducer, useState } from 'react';
import React, {
useCallback,
useEffect,
useMemo,
useReducer,
useState,
} from 'react';
import { Select } from 'src/common/components';
import debounce from 'lodash/debounce';
import { SLOW_DEBOUNCE } from 'src/constants';
Expand Down Expand Up @@ -101,6 +108,23 @@ export default function PluginFilterSelect(props: PluginFilterSelectProps) {
} = formData;
const groupby = ensureIsArray<string>(formData.groupby);
const [col] = groupby;
const [selectedValues, setSelectedValues] = useState<SelectValue>(
filterState.value,
);
const sortedData = useMemo(() => {
const firstData: DataRecord[] = [];
const restData: DataRecord[] = [];
data.forEach(row => {
// @ts-ignore
if (selectedValues?.includes(row[col])) {
firstData.push(row);
} else {
restData.push(row);
}
});
return [...firstData, ...restData];
}, [col, selectedValues, data]);
const [isDropdownVisible, setIsDropdownVisible] = useState(false);
const [currentSuggestionSearch, setCurrentSuggestionSearch] = useState('');
const [dataMask, dispatchDataMask] = useReducer<DataMaskReducer>(reducer, {
filterState,
Expand All @@ -126,6 +150,12 @@ export default function PluginFilterSelect(props: PluginFilterSelectProps) {
});
};

useEffect(() => {
if (!isDropdownVisible) {
setSelectedValues(filterState.value);
}
}, [JSON.stringify(filterState.value)]);

const isDisabled =
appSection === AppSection.FILTER_CONFIG_MODAL && defaultToFirstItem;

Expand Down Expand Up @@ -163,6 +193,7 @@ export default function PluginFilterSelect(props: PluginFilterSelectProps) {
const handleBlur = () => {
clearSuggestionSearch();
unsetFocusedFilter();
setSelectedValues(filterState.value);
};

const datatype: GenericDataType = coltypeMap[col];
Expand Down Expand Up @@ -222,14 +253,15 @@ export default function PluginFilterSelect(props: PluginFilterSelectProps) {
onSearch={searchWrapper}
onSelect={clearSuggestionSearch}
onBlur={handleBlur}
onDropdownVisibleChange={setIsDropdownVisible}
onFocus={setFocusedFilter}
// @ts-ignore
onChange={handleChange}
ref={inputRef}
loading={isRefreshing}
maxTagCount={5}
>
{data.map(row => {
{sortedData.map(row => {
const [value] = groupby.map(col => row[col]);
return (
// @ts-ignore
Expand Down

0 comments on commit 2c46c3f

Please sign in to comment.