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): Add default first value to select filter #13726

Merged
merged 22 commits into from
Apr 12, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
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 @@ -66,12 +66,16 @@ export default function PluginFilterSelect(props: PluginFilterSelectProps) {
currentValue,
inverseSelection,
inputRef,
defaultToFirstItem,
} = formData;

const groupby = ensureIsArray<string>(formData.groupby);
const initValues = defaultValue ?? [];
const firstItem: (string | number)[] = data[0]
? (groupby.map(col => data[0][col]) as string[]) ?? initValues
: initValues;
const [values, setValues] = useState<(string | number | boolean)[]>(
defaultValue ?? [],
defaultToFirstItem ? firstItem : initValues,
);
const groupby = ensureIsArray<string>(formData.groupby);

const [col] = groupby;
const datatype: GenericDataType = coltypeMap[col];
Expand Down Expand Up @@ -118,18 +122,22 @@ export default function PluginFilterSelect(props: PluginFilterSelectProps) {
handleChange(currentValue ?? []);
}, [
JSON.stringify(currentValue),
defaultToFirstItem,
amitmiran137 marked this conversation as resolved.
Show resolved Hide resolved
multiSelect,
enableEmptyFilter,
inverseSelection,
]);

useEffect(() => {
handleChange(defaultValue ?? []);
handleChange(defaultToFirstItem ? firstItem : initValues);
}, [
JSON.stringify(defaultValue),
JSON.stringify(firstItem),
defaultToFirstItem,
multiSelect,
enableEmptyFilter,
inverseSelection,
defaultToFirstItem,
simcha90 marked this conversation as resolved.
Show resolved Hide resolved
]);

const placeholderText =
Expand Down
14 changes: 14 additions & 0 deletions superset-frontend/src/filters/components/Select/controlPanel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const {
enableEmptyFilter,
inverseSelection,
multiSelect,
defaultToFirstItem,
sortAscending,
} = DEFAULT_FORM_DATA;

Expand Down Expand Up @@ -79,6 +80,19 @@ const config: ControlPanelConfig = {
},
},
],
[
{
name: 'defaultToFirstItem',
config: {
type: 'CheckboxControl',
label: t('Default to first item'),
default: defaultToFirstItem,
resetConfig: true,
renderTrigger: true,
description: t('Select first item by default'),
},
},
],
[
{
name: 'inverseSelection',
Expand Down
2 changes: 2 additions & 0 deletions superset-frontend/src/filters/components/Select/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ interface PluginFilterSelectCustomizeProps {
enableEmptyFilter: boolean;
inverseSelection: boolean;
multiSelect: boolean;
defaultToFirstItem: boolean;
inputRef?: RefObject<HTMLInputElement>;
sortAscending: boolean;
}
Expand All @@ -59,6 +60,7 @@ export const DEFAULT_FORM_DATA: PluginFilterSelectCustomizeProps = {
currentValue: null,
enableEmptyFilter: false,
inverseSelection: false,
defaultToFirstItem: false,
multiSelect: true,
sortAscending: true,
};