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

fix: Select all issue with "Dynamically search all filter values" in FilterBar #23400

Merged
merged 1 commit into from
Mar 17, 2023
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 @@ -38,6 +38,7 @@ import { Select } from 'src/components';
import { SLOW_DEBOUNCE } from 'src/constants';
import { propertyComparator } from 'src/components/Select/utils';
import { FilterBarOrientation } from 'src/dashboard/types';
import { uniqWith, isEqual } from 'lodash';
import { PluginFilterSelectProps, SelectValue } from './types';
import { FilterPluginStyle, StatusMessage, StyledFormItem } from '../common';
import { getDataRecordFormatter, getSelectExtraFormData } from '../../utils';
Expand Down Expand Up @@ -120,6 +121,7 @@ export default function PluginFilterSelect(props: PluginFilterSelectProps) {
}),
[],
);
const [initialData, setInitialData] = useState<typeof data>([]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we explicitly type it as DataRecord[]?


const updateDataMask = useCallback(
(values: SelectValue) => {
Expand Down Expand Up @@ -165,10 +167,6 @@ export default function PluginFilterSelect(props: PluginFilterSelectProps) {
],
);

useEffect(() => {
updateDataMask(filterState.value);
}, [JSON.stringify(filterState.value)]);

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

Expand Down Expand Up @@ -224,6 +222,47 @@ export default function PluginFilterSelect(props: PluginFilterSelectProps) {
[updateDataMask],
);

const placeholderText =
data.length === 0
? t('No data')
: tn('%s option', '%s options', data.length, data.length);

const formItemExtra = useMemo(() => {
if (filterState.validateMessage) {
return (
<StatusMessage status={filterState.validateStatus}>
{filterState.validateMessage}
</StatusMessage>
);
}
return undefined;
}, [filterState.validateMessage, filterState.validateStatus]);

const options = useMemo(() => {
const allOptions = [...data, ...initialData];
const uniqueOptions = uniqWith(allOptions, isEqual);
const selectOptions: { label: string; value: DataRecordValue }[] = [];
uniqueOptions.forEach(row => {
const [value] = groupby.map(col => row[col]);
selectOptions.push({
label: labelFormatter(value, datatype),
value,
});
});
return selectOptions;
}, [data, initialData, datatype, groupby, labelFormatter]);

const sortComparator = useCallback(
(a: AntdLabeledValue, b: AntdLabeledValue) => {
const labelComparator = propertyComparator('label');
if (formData.sortAscending) {
return labelComparator(a, b);
}
return labelComparator(b, a);
},
[formData.sortAscending],
);

useEffect(() => {
if (defaultToFirstItem && filterState.value === undefined) {
// initialize to first value if set to default to first item
Expand Down Expand Up @@ -254,48 +293,19 @@ export default function PluginFilterSelect(props: PluginFilterSelectProps) {
JSON.stringify(filterState),
]);

useEffect(() => {
updateDataMask(filterState.value);
}, [JSON.stringify(filterState.value)]);

useEffect(() => {
setDataMask(dataMask);
}, [JSON.stringify(dataMask)]);

const placeholderText =
data.length === 0
? t('No data')
: tn('%s option', '%s options', data.length, data.length);

const formItemExtra = useMemo(() => {
if (filterState.validateMessage) {
return (
<StatusMessage status={filterState.validateStatus}>
{filterState.validateMessage}
</StatusMessage>
);
useEffect(() => {
if (data.length && !initialData.length) {
setInitialData(data);
}
return undefined;
}, [filterState.validateMessage, filterState.validateStatus]);

const options = useMemo(() => {
const options: { label: string; value: DataRecordValue }[] = [];
data.forEach(row => {
const [value] = groupby.map(col => row[col]);
options.push({
label: labelFormatter(value, datatype),
value,
});
});
return options;
}, [data, datatype, groupby, labelFormatter]);

const sortComparator = useCallback(
(a: AntdLabeledValue, b: AntdLabeledValue) => {
const labelComparator = propertyComparator('label');
if (formData.sortAscending) {
return labelComparator(a, b);
}
return labelComparator(b, a);
},
[formData.sortAscending],
);
}, [data, initialData.length]);

return (
<FilterPluginStyle height={height} width={width}>
Expand Down