Skip to content

Commit 3b03367

Browse files
authored
feat: Add label analytics metadata to Select and Multiselect component (#4185)
1 parent 0f8232f commit 3b03367

File tree

6 files changed

+15
-3
lines changed

6 files changed

+15
-3
lines changed

src/multiselect/__tests__/analytics-metadata.test.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ const getMetadataContexts = (
7171
disabled: disabled ? 'true' : 'false',
7272
selectedOptionsCount: `${selectedOptionsCount}`,
7373
selectedOptionsValues: selectedOptions.map(option => option.value) as Array<string>,
74+
selectedOptions: selectedOptions.map(option => option.label ?? option.value) as Array<string>,
7475
},
7576
},
7677
},

src/multiselect/analytics-metadata/interfaces.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,6 @@ export interface GeneratedAnalyticsMetadataMultiselectComponent {
2626
disabled: string;
2727
selectedOptionsCount: string;
2828
selectedOptionsValues: Array<string>;
29+
selectedOptions: Array<string>;
2930
};
3031
}

src/multiselect/index.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ const Multiselect = React.forwardRef(
5757
selectedOptionsValues: selectedOptions
5858
.filter(option => option.value !== undefined)
5959
.map(option => `${option.value}`),
60+
selectedOptions: selectedOptions
61+
.filter(option => option.value !== undefined)
62+
// fallback on value when label's undefined because in the dropdown if there's no label
63+
// the value will be shown.
64+
.map(option => `${option.label ?? option.value}`),
6065
},
6166
};
6267

src/select/__tests__/analytics-metadata.test.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ function renderSelect(props: Partial<SelectProps>) {
3535
const getMetadataContexts = (
3636
label = 'select with metadata',
3737
disabled: boolean = false,
38-
selectedOptionValue = 'null'
38+
selectedOptionValue = 'null',
39+
selectedOption = 'null'
3940
) => {
4041
const metadata: GeneratedAnalyticsMetadataFragment = {
4142
contexts: [
@@ -47,6 +48,7 @@ const getMetadataContexts = (
4748
properties: {
4849
disabled: disabled ? 'true' : 'false',
4950
selectedOptionValue,
51+
selectedOption,
5052
},
5153
},
5254
},
@@ -140,13 +142,13 @@ describe('Select renders correct analytics metadata', () => {
140142
test('and defined value', () => {
141143
const wrapper = renderSelect({ selectedOption: { value: 'value1' } });
142144
expect(getGeneratedAnalyticsMetadata(wrapper.getElement())).toEqual({
143-
...getMetadataContexts(undefined, undefined, 'value1'),
145+
...getMetadataContexts(undefined, undefined, 'value1', 'value1'),
144146
});
145147
});
146148
test('and undefined value', () => {
147149
const wrapper = renderSelect({ selectedOption: { label: 'label1' } });
148150
expect(getGeneratedAnalyticsMetadata(wrapper.getElement())).toEqual({
149-
...getMetadataContexts(),
151+
...getMetadataContexts(undefined, undefined, 'null', 'label1'),
150152
});
151153
});
152154
});

src/select/analytics-metadata/interfaces.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,6 @@ export interface GeneratedAnalyticsMetadataSelectComponent {
1818
properties: {
1919
disabled: string;
2020
selectedOptionValue: string;
21+
selectedOption: string;
2122
};
2223
}

src/select/index.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ const Select = React.forwardRef(
5050
properties: {
5151
disabled: `${!!externalProps.disabled}`,
5252
selectedOptionValue: `${externalProps.selectedOption && externalProps.selectedOption.value ? externalProps.selectedOption.value : null}`,
53+
// Use label for display if available, fallback to value because that's what gets shown in dropdown when label is not provided
54+
selectedOption: `${externalProps.selectedOption?.label ?? externalProps.selectedOption?.value ?? null}`,
5355
},
5456
};
5557

0 commit comments

Comments
 (0)