Skip to content

Commit

Permalink
[TIP] Support category pre-selection inside the FieldsBrowser component
Browse files Browse the repository at this point in the history
  • Loading branch information
lgestc committed Oct 5, 2022
1 parent 205a2b8 commit e34423d
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ describe('<IndicatorsFieldBrowser />', () => {
columnIds: [],
onResetColumns: stub,
onToggleColumn: stub,
options: {},
options: {
preselectedCategoryIds: ['threat'],
},
})
);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ export const IndicatorsFieldBrowser: VFC<IndicatorsFieldBrowserProps> = ({
columnIds,
onResetColumns,
onToggleColumn,
options: {},
options: {
preselectedCategoryIds: ['threat'],
},
});
};
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,25 @@ describe('FieldsBrowser', () => {
const result = renderComponent({ isEventViewer, width: FIELD_BROWSER_WIDTH });
expect(result.getByTestId('show-field-browser')).toBeInTheDocument();
});

describe('options.preselectedCategoryIds', () => {
it("should render fields list narrowed to preselected category id's", async () => {
const agentFieldsCount = Object.keys(mockBrowserFields.agent?.fields || {}).length;

// Narrowing the selection to 'agent' only
const result = renderComponent({ options: { preselectedCategoryIds: ['agent'] } });

result.getByTestId('show-field-browser').click();

// Wait for the modal to open
await waitFor(() => {
expect(result.getByTestId('fields-browser-container')).toBeInTheDocument();
});

// Check if there are only 4 fields in the table
expect(result.queryByTestId('field-table')?.querySelectorAll('tbody tr')).toHaveLength(
agentFieldsCount
);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ export const FieldBrowserComponent: React.FC<FieldBrowserProps> = ({
options,
width,
}) => {
const initialCategories = useMemo(
() => options?.preselectedCategoryIds ?? [],
[options?.preselectedCategoryIds]
);

const customizeColumnsButtonRef = useRef<HTMLButtonElement | null>(null);
/** all field names shown in the field browser must contain this string (when specified) */
const [filterInput, setFilterInput] = useState('');
Expand All @@ -43,7 +48,7 @@ export const FieldBrowserComponent: React.FC<FieldBrowserProps> = ({
/** when true, show a spinner in the input to indicate the field browser is searching for matching field names */
const [isSearching, setIsSearching] = useState(false);
/** this category will be displayed in the right-hand pane of the field browser */
const [selectedCategoryIds, setSelectedCategoryIds] = useState<string[]>([]);
const [selectedCategoryIds, setSelectedCategoryIds] = useState<string[]>(initialCategories);
/** show the field browser */
const [show, setShow] = useState(false);

Expand Down Expand Up @@ -93,9 +98,9 @@ export const FieldBrowserComponent: React.FC<FieldBrowserProps> = ({
setFilteredBrowserFields(null);
setFilterSelectedEnabled(false);
setIsSearching(false);
setSelectedCategoryIds([]);
setSelectedCategoryIds(initialCategories);
setShow(false);
}, []);
}, [initialCategories]);

/** Invoked when the user types in the filter input */
const updateFilter = useCallback(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ export type GetFieldTableColumns = (params: {
export interface FieldBrowserOptions {
createFieldButton?: CreateFieldComponent;
getFieldTableColumns?: GetFieldTableColumns;
/**
* Categories that should be selected initially
*/
preselectedCategoryIds?: string[];
}

export interface FieldBrowserProps {
Expand Down

0 comments on commit e34423d

Please sign in to comment.