Skip to content

Commit 0a29525

Browse files
authored
fix(select): render data attributes of select components (#438)
* fix(select): render data attributes of select components * chore(select): separate HOC components
1 parent 9b8501d commit 0a29525

File tree

1 file changed

+41
-29
lines changed

1 file changed

+41
-29
lines changed

src/components/select/index.js

Lines changed: 41 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React, { useMemo } from "react"
22
import styled from "styled-components"
33
import ReactSelect, { components as defaultComponents } from "react-select"
44

5-
const withDataAttrs = Component => props => {
5+
const useDataAttrs = (props, name) => {
66
const { "data-ga": dataGA, "data-testid": dataTestId } = props.selectProps
77

88
const ga = useMemo(() => {
@@ -11,42 +11,54 @@ const withDataAttrs = Component => props => {
1111
const gaParts = dataGA.split("::")
1212
if (!gaParts[1]) return dataGA
1313

14-
gaParts[1] = `${gaParts[1]}-${Component.displayName}`
14+
gaParts[1] = `${gaParts[1]}-${name}`
1515
return gaParts.join("::")
1616
}, [dataGA])
1717

18-
const testId = `${dataTestId || ""}${Component.displayName}`
18+
const testId = `${dataTestId || ""}${name}`
1919

20-
return <Component data-ga={ga} data-testid={testId} {...props} />
20+
return { "data-ga": ga, "data-testid": testId }
21+
}
22+
23+
const withDataAttrs = (Component, name) => props => {
24+
const dataProps = useDataAttrs(props, name)
25+
26+
return <Component {...props} innerProps={{ ...(props.innerProps || {}), ...dataProps }} />
27+
}
28+
29+
const withDOMDataAttrs = (Component, name) => props => {
30+
const dataProps = useDataAttrs(props, name)
31+
32+
return <Component {...props} {...dataProps} />
2133
}
2234

2335
const customComponents = {
2436
...defaultComponents,
25-
ClearIndicator: withDataAttrs(defaultComponents.ClearIndicator),
26-
Control: withDataAttrs(defaultComponents.Control),
27-
DropdownIndicator: withDataAttrs(defaultComponents.DropdownIndicator),
28-
DownChevron: withDataAttrs(defaultComponents.DownChevron),
29-
CrossIcon: withDataAttrs(defaultComponents.CrossIcon),
30-
Group: withDataAttrs(defaultComponents.Group),
31-
GroupHeading: withDataAttrs(defaultComponents.GroupHeading),
32-
IndicatorsContainer: withDataAttrs(defaultComponents.IndicatorsContainer),
33-
IndicatorSeparator: withDataAttrs(defaultComponents.IndicatorSeparator),
34-
Input: withDataAttrs(defaultComponents.Input),
35-
LoadingIndicator: withDataAttrs(defaultComponents.LoadingIndicator),
36-
Menu: withDataAttrs(defaultComponents.Menu),
37-
MenuList: withDataAttrs(defaultComponents.MenuList),
38-
MenuPortal: withDataAttrs(defaultComponents.MenuPortal),
39-
LoadingMessage: withDataAttrs(defaultComponents.LoadingMessage),
40-
NoOptionsMessage: withDataAttrs(defaultComponents.NoOptionsMessage),
41-
MultiValue: withDataAttrs(defaultComponents.MultiValue),
42-
MultiValueContainer: withDataAttrs(defaultComponents.MultiValueContainer),
43-
MultiValueLabel: withDataAttrs(defaultComponents.MultiValueLabel),
44-
MultiValueRemove: withDataAttrs(defaultComponents.MultiValueRemove),
45-
Option: withDataAttrs(defaultComponents.Option),
46-
Placeholder: withDataAttrs(defaultComponents.Placeholder),
47-
SelectContainer: withDataAttrs(defaultComponents.SelectContainer),
48-
SingleValue: withDataAttrs(defaultComponents.SingleValue),
49-
ValueContainer: withDataAttrs(defaultComponents.ValueContainer),
37+
ClearIndicator: withDataAttrs(defaultComponents.ClearIndicator, "ClearIndicator"),
38+
Control: withDataAttrs(defaultComponents.Control, "Control"),
39+
DropdownIndicator: withDataAttrs(defaultComponents.DropdownIndicator, "DropdownIndicator"),
40+
DownChevron: withDataAttrs(defaultComponents.DownChevron, "DownChevron"),
41+
CrossIcon: withDataAttrs(defaultComponents.CrossIcon, "CrossIcon"),
42+
Group: withDataAttrs(defaultComponents.Group, "Group"),
43+
GroupHeading: withDataAttrs(defaultComponents.GroupHeading, "GroupHeading"),
44+
IndicatorsContainer: withDataAttrs(defaultComponents.IndicatorsContainer, "IndicatorsContainer"),
45+
IndicatorSeparator: withDataAttrs(defaultComponents.IndicatorSeparator, "IndicatorSeparator"),
46+
Input: withDOMDataAttrs(defaultComponents.Input, "Input"),
47+
LoadingIndicator: withDataAttrs(defaultComponents.LoadingIndicator, "LoadingIndicator"),
48+
Menu: withDataAttrs(defaultComponents.Menu, "Menu"),
49+
MenuList: withDataAttrs(defaultComponents.MenuList, "MenuList"),
50+
MenuPortal: withDataAttrs(defaultComponents.MenuPortal, "MenuPortal"),
51+
LoadingMessage: withDataAttrs(defaultComponents.LoadingMessage, "LoadingMessage"),
52+
MultiValue: withDataAttrs(defaultComponents.MultiValue, "MultiValue"),
53+
MultiValueContainer: withDataAttrs(defaultComponents.MultiValueContainer, "MultiValueContainer"),
54+
MultiValueLabel: withDataAttrs(defaultComponents.MultiValueLabel, "MultiValueLabel"),
55+
MultiValueRemove: withDataAttrs(defaultComponents.MultiValueRemove, "MultiValueRemove"),
56+
NoOptionsMessage: withDataAttrs(defaultComponents.NoOptionsMessage, "NoOptionsMessage"),
57+
Option: withDataAttrs(defaultComponents.Option, "Option"),
58+
Placeholder: withDataAttrs(defaultComponents.Placeholder, "Placeholder"),
59+
SelectContainer: withDataAttrs(defaultComponents.SelectContainer, "SelectContainer"),
60+
SingleValue: withDataAttrs(defaultComponents.SingleValue, "SingleValue"),
61+
ValueContainer: withDataAttrs(defaultComponents.ValueContainer, "ValueContainer"),
5062
}
5163

5264
const makeCustomTheme = theme => selectTheme => {

0 commit comments

Comments
 (0)