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

feat(native-filters): sort selected values on blur #14842

Merged
merged 2 commits into from
May 27, 2021
Merged
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
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