Skip to content

Commit 78e6310

Browse files
authored
Add cnstrc data attributes (#167)
1 parent c2c175e commit 78e6310

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

src/components/Autocomplete/AutocompleteResults/AutocompleteResults.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import React, { ReactNode, useContext } from 'react';
2-
import { GetItemProps, Section } from '../../../types';
2+
import { Item, Section } from '../../../types';
33
import { toKebabCase } from '../../../utils';
44
import { CioAutocompleteContext } from '../CioAutocompleteProvider';
55
import SectionItemsList from '../SectionItemsList/SectionItemsList';
66

77
export type RenderResults = (renderResultsArguments: {
88
sections: Section[];
9-
getItemProps: GetItemProps;
9+
getItemProps: (item: Item) => any;
1010
}) => ReactNode;
1111

1212
type AutocompleteResultsProps = {

src/hooks/useCioAutocomplete.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
Section,
1010
UserDefinedSection,
1111
HTMLPropsWithCioDataAttributes,
12+
Item,
1213
} from '../types';
1314
import usePrevious from './usePrevious';
1415
import {
@@ -122,7 +123,7 @@ const useCioAutocomplete = (options: UseCioAutocompleteOptions) => {
122123
closeMenu,
123124
highlightedIndex,
124125
getInputProps,
125-
getItemProps,
126+
getItemProps: getItemPropsDownShift,
126127
} = useDownShift({ setQuery, items, onSubmit, cioClient, previousQuery });
127128

128129
// Log console errors
@@ -142,19 +143,24 @@ const useCioAutocomplete = (options: UseCioAutocompleteOptions) => {
142143
...getMenuProps(),
143144
className: 'cio-results',
144145
'data-testid': 'cio-results',
146+
'data-cnstrc-autosuggest': '',
145147
}),
146148
getLabelProps,
147149
openMenu,
148150
closeMenu,
149-
getItemProps: (item) => {
151+
getItemProps: (item: Item) => {
150152
const { index, sectionId } = getItemPosition({ item, items });
151153
const sectionItemTestId = `cio-item-${sectionId?.replace(' ', '')}`;
152154

153155
return {
154-
...getItemProps({ item, index }),
156+
...getItemPropsDownShift({ item, index }),
155157
// @deprecated `sectionItemTestId` will be removed as a className in the next major version
156158
className: `cio-item ${sectionItemTestId}`,
157159
'data-testid': sectionItemTestId,
160+
'data-cnstrc-item-section': item.section,
161+
'data-cnstrc-item-group': item.groupId,
162+
'data-cnstrc-item-name': item.value,
163+
'data-cnstrc-item-id': item.data?.id,
158164
};
159165
},
160166
getInputProps: () => ({
@@ -193,6 +199,7 @@ const useCioAutocomplete = (options: UseCioAutocompleteOptions) => {
193199
},
194200
className: 'cio-input',
195201
'data-testid': 'cio-input',
202+
'data-cnstrc-search-input': '',
196203
placeholder,
197204
onKeyDownCapture: ({ code, key, nativeEvent }) => {
198205
const isEnter = code === 'Enter' || key === 'Enter';
@@ -234,6 +241,7 @@ const useCioAutocomplete = (options: UseCioAutocompleteOptions) => {
234241
},
235242
className: 'cio-form',
236243
'data-testid': 'cio-form',
244+
'data-cnstrc-search-form': '',
237245
}),
238246
getSectionProps: (section: Section) => {
239247
// @deprecated ClassNames derived from this fn will be removed in the next major version

0 commit comments

Comments
 (0)