Skip to content

Commit

Permalink
Clean up code
Browse files Browse the repository at this point in the history
  • Loading branch information
Heenawter committed Nov 22, 2022
1 parent badf71c commit 169439f
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export const OptionsListEditorOptions = ({
});

useEffect(() => {
// when field type changes, ensure that the selected sort type is still valid
if (!getCompatibleSortingTypes(fieldType).includes(state.sortBy)) {
onChange({ sort: DEFAULT_SORT });
setState((s) => ({ ...s, sortBy: DEFAULT_SORT.by, sortDirection: DEFAULT_SORT.direction }));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Side Public License, v 1.
*/

import React, { useState } from 'react';
import React, { useMemo, useState } from 'react';

import {
EuiButtonGroupOptionProps,
Expand Down Expand Up @@ -62,6 +62,7 @@ export const OptionsListPopoverSortingButton = ({
const sort = select((state) => state.explicitInput.sort ?? DEFAULT_SORT);

const [isSortingPopoverOpen, setIsSortingPopoverOpen] = useState(false);

const [sortByOptions, setSortByOptions] = useState<SortByItem[]>(() => {
return getCompatibleSortingTypes(field?.type).map((key) => {
return {
Expand All @@ -73,19 +74,23 @@ export const OptionsListPopoverSortingButton = ({
} as SortByItem;
});
});
const sortOrderOptions = sortDirections.map((key) => {
return {
id: key,
iconType: `sort${toSentenceCase(key)}ending`,
'data-test-subj': `optionsList__sortOrder_${key}`,
label: OptionsListStrings.editorAndPopover.sortOrder[key].getSortByLabel(),
} as SortOrderItem;
});

const sortOrderOptions = useMemo(
() =>
sortDirections.map((key) => {
return {
id: key,
iconType: `sort${toSentenceCase(key)}ending`,
'data-test-subj': `optionsList__sortOrder_${key}`,
label: OptionsListStrings.editorAndPopover.sortOrder[key].getSortByLabel(),
} as SortOrderItem;
}),
[]
);

const onSortByChange = (updatedOptions: SortByItem[]) => {
setSortByOptions(updatedOptions);
const selectedOption = updatedOptions.find(({ checked }) => checked === 'on');

if (selectedOption) {
dispatch(setSort({ by: selectedOption.data.sortBy }));
}
Expand Down
5 changes: 3 additions & 2 deletions src/plugins/controls/public/options_list/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ interface SearchString {
value: string;
valid: boolean;
}

// Component state is only used by public components.
export interface OptionsListComponentState {
field?: OptionsListField;
totalCardinality?: number;
validSelections?: string[];
searchString: SearchString;
availableOptions?: string[];
invalidSelections?: string[];
validSelections?: string[];
searchString: SearchString;
}

// public only - redux embeddable state type
Expand Down
1 change: 0 additions & 1 deletion test/functional/page_objects/dashboard_page_controls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,6 @@ export class DashboardPageControls extends FtrService {
if (hideSort) await this.testSubjects.click(getSettingTestSubject('hideSort'));
if (defaultSortType) {
await this.testSubjects.click(`optionsListEditor__sortOrder_${defaultSortType.direction}`);
await this.common.sleep(6000);
await this.testSubjects.click('optionsListControl__chooseSortBy');
await this.testSubjects.click(`optionsListEditor__sortBy_${defaultSortType.by}`);
}
Expand Down

0 comments on commit 169439f

Please sign in to comment.