Skip to content
Merged
Show file tree
Hide file tree
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
@@ -1,12 +1,12 @@
import React, { ReactNode, useContext } from 'react';
import { GetItemProps, Section } from '../../../types';
import { Item, Section } from '../../../types';
import { toKebabCase } from '../../../utils';
import { CioAutocompleteContext } from '../CioAutocompleteProvider';
import SectionItemsList from '../SectionItemsList/SectionItemsList';

export type RenderResults = (renderResultsArguments: {
sections: Section[];
getItemProps: GetItemProps;
getItemProps: (item: Item) => any;
}) => ReactNode;

type AutocompleteResultsProps = {
Expand Down
14 changes: 11 additions & 3 deletions src/hooks/useCioAutocomplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
Section,
UserDefinedSection,
HTMLPropsWithCioDataAttributes,
Item,
} from '../types';
import usePrevious from './usePrevious';
import {
Expand Down Expand Up @@ -122,7 +123,7 @@ const useCioAutocomplete = (options: UseCioAutocompleteOptions) => {
closeMenu,
highlightedIndex,
getInputProps,
getItemProps,
getItemProps: getItemPropsDownShift,
} = useDownShift({ setQuery, items, onSubmit, cioClient, previousQuery });

// Log console errors
Expand All @@ -142,19 +143,24 @@ const useCioAutocomplete = (options: UseCioAutocompleteOptions) => {
...getMenuProps(),
className: 'cio-results',
'data-testid': 'cio-results',
'data-cnstrc-autosuggest': '',
}),
getLabelProps,
openMenu,
closeMenu,
getItemProps: (item) => {
getItemProps: (item: Item) => {
const { index, sectionId } = getItemPosition({ item, items });
const sectionItemTestId = `cio-item-${sectionId?.replace(' ', '')}`;

return {
...getItemProps({ item, index }),
...getItemPropsDownShift({ item, index }),
// @deprecated `sectionItemTestId` will be removed as a className in the next major version
className: `cio-item ${sectionItemTestId}`,
'data-testid': sectionItemTestId,
'data-cnstrc-item-section': item.section,
'data-cnstrc-item-group': item.groupId,
'data-cnstrc-item-name': item.value,
'data-cnstrc-item-id': item.data?.id,
};
},
getInputProps: () => ({
Expand Down Expand Up @@ -193,6 +199,7 @@ const useCioAutocomplete = (options: UseCioAutocompleteOptions) => {
},
className: 'cio-input',
'data-testid': 'cio-input',
'data-cnstrc-search-input': '',
placeholder,
onKeyDownCapture: ({ code, key, nativeEvent }) => {
const isEnter = code === 'Enter' || key === 'Enter';
Expand Down Expand Up @@ -234,6 +241,7 @@ const useCioAutocomplete = (options: UseCioAutocompleteOptions) => {
},
className: 'cio-form',
'data-testid': 'cio-form',
'data-cnstrc-search-form': '',
}),
getSectionProps: (section: Section) => {
// @deprecated ClassNames derived from this fn will be removed in the next major version
Expand Down