Skip to content

Commit 1b60e52

Browse files
Added custom data-cy label for Select component (#2472)
1 parent 0715f47 commit 1b60e52

1 file changed

Lines changed: 27 additions & 11 deletions

File tree

src/components/Select.jsx

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import classnames from "classnames";
44
import { _existsBy, isPresent } from "neetocist";
55
import { Down, Close } from "neetoicons";
66
import PropTypes from "prop-types";
7-
import { prop, assoc, flatten, pluck, mergeDeepRight } from "ramda";
7+
import { prop, assoc, flatten, pluck, mergeDeepRight, isEmpty } from "ramda";
88
import SelectInput, { components } from "react-select";
99
import Async from "react-select/async";
1010
import AsyncCreatable from "react-select/async-creatable";
@@ -87,7 +87,7 @@ const CustomOption = props => {
8787
innerRef={ref}
8888
innerProps={{
8989
...props.innerProps,
90-
"data-cy": dataCy || `${hyphenize(props.label)}-select-option`,
90+
"data-cy": dataCy || `${props.hyphenatedDataCyLabel}-select-option`,
9191
}}
9292
/>
9393
);
@@ -102,7 +102,7 @@ const Placeholder = props => {
102102
innerProps={{
103103
...props.innerProps,
104104
"data-cy": selectProps
105-
? `${hyphenize(selectProps.label)}-select-placeholder`
105+
? `${selectProps.hyphenatedDataCyLabel}-select-placeholder`
106106
: "select-placeholder",
107107
}}
108108
/>
@@ -118,7 +118,7 @@ const Menu = props => {
118118
innerProps={{
119119
...props.innerProps,
120120
"data-cy": selectProps
121-
? `${hyphenize(selectProps.label)}-select-menu`
121+
? `${selectProps.hyphenatedDataCyLabel}-select-menu`
122122
: "select-menu",
123123
}}
124124
/>
@@ -142,7 +142,7 @@ const ValueContainer = props => {
142142
...props.innerProps,
143143
name: selectProps.name,
144144
"data-cy": selectProps
145-
? `${hyphenize(selectProps.label)}-select-value-container`
145+
? `${selectProps.hyphenatedDataCyLabel}-select-value-container`
146146
: "select-value-container",
147147
}}
148148
/>
@@ -220,13 +220,18 @@ const Select = ({
220220
onMenuOpen,
221221
onKeyDown,
222222
styles = {},
223+
dataCy = "nui",
223224
...otherProps
224225
}) => {
225226
const inputId = useId(id);
226227
const isMenuOpen = useRef(
227228
otherProps.isMenuOpen ?? otherProps.defaultMenuIsOpen ?? false
228229
);
229230

231+
const hyphenatedDataCyLabel = isEmpty(label)
232+
? hyphenize(dataCy)
233+
: hyphenize(label);
234+
230235
let Parent = SelectInput;
231236

232237
if (isCreateable) {
@@ -301,13 +306,13 @@ const Select = ({
301306
return (
302307
<div
303308
className={classnames(["neeto-ui-input__wrapper", className])}
304-
data-cy={`${hyphenize(label)}-select-container-wrapper`}
309+
data-cy={`${hyphenatedDataCyLabel}-select-container-wrapper`}
305310
data-testid="select"
306311
>
307312
{label && (
308313
<Label
309314
{...{ required }}
310-
data-cy={`${hyphenize(label)}-input-label`}
315+
data-cy={`${hyphenatedDataCyLabel}-input-label`}
311316
data-testid="select-label"
312317
htmlFor={inputId}
313318
{...labelProps}
@@ -319,7 +324,7 @@ const Select = ({
319324
blurInputOnSelect={false}
320325
classNamePrefix="neeto-ui-react-select"
321326
closeMenuOnSelect={!otherProps.isMulti}
322-
data-cy={`${hyphenize(label)}-select-container`}
327+
data-cy={`${hyphenatedDataCyLabel}-select-container`}
323328
defaultValue={findInOptions(defaultValue)}
324329
ref={innerRef}
325330
value={findInOptions(value)}
@@ -346,12 +351,19 @@ const Select = ({
346351
onKeyDown={handleKeyDown}
347352
onMenuClose={handleMenuClose}
348353
onMenuOpen={handleMenuOpen}
349-
{...{ inputId, label, styles, ...portalProps, ...otherProps }}
354+
{...{
355+
inputId,
356+
label,
357+
styles,
358+
...portalProps,
359+
...otherProps,
360+
hyphenatedDataCyLabel,
361+
}}
350362
/>
351363
{!!error && (
352364
<p
353365
className="neeto-ui-input__error"
354-
data-cy={`${hyphenize(label)}-select-error`}
366+
data-cy={`${hyphenatedDataCyLabel}-select-error`}
355367
data-testid="select-error"
356368
>
357369
{error}
@@ -360,7 +372,7 @@ const Select = ({
360372
{helpText && (
361373
<p
362374
className="neeto-ui-input__help-text"
363-
data-cy={`${hyphenize(label)}-select-help-text`}
375+
data-cy={`${hyphenatedDataCyLabel}-select-help-text`}
364376
data-testid="select-help-text"
365377
>
366378
{helpText}
@@ -486,6 +498,10 @@ Select.propTypes = {
486498
* To specify the styles for the Select component.
487499
*/
488500
styles: PropTypes.object,
501+
/**
502+
* To specify the custom data-cy label for the Select component and its child components.
503+
*/
504+
dataCy: PropTypes.string,
489505
};
490506

491507
export default Select;

0 commit comments

Comments
 (0)