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
34 changes: 32 additions & 2 deletions lib/src/select/Select.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ describe("Select component tests", () => {
fireEvent.focus(select);
expect(queryByRole("listbox")).toBeFalsy();
});
test("Controlled - Not optional constraint", () => {
test("Controlled - Single selection - Not optional constraint", () => {
const onChange = jest.fn();
const onBlur = jest.fn();
const { getByRole, getAllByRole } = render(
Expand All @@ -315,11 +315,41 @@ describe("Select component tests", () => {
userEvent.click(getAllByRole("option")[0]);
expect(onChange).toHaveBeenCalled();
expect(onChange).toHaveBeenCalledWith({ value: "1" });
fireEvent.focus(select);
fireEvent.blur(select);
expect(onBlur).toHaveBeenCalled();
expect(onBlur).toHaveBeenCalledWith({ value: "1" });
});
test("Controlled - Multiple selection - Not optional constraint", () => {
const onChange = jest.fn();
const onBlur = jest.fn();
const { getByRole, getAllByRole } = render(
<DxcSelect label="test-select-label" options={single_options} onChange={onChange} onBlur={onBlur} multiple />
);
const select = getByRole("combobox");

expect(select.getAttribute("aria-required")).toBe("true");
fireEvent.focus(select);
fireEvent.blur(select);
expect(onBlur).toHaveBeenCalled();
expect(onBlur).toHaveBeenCalledWith({ value: [], error: "This field is required. Please, enter a value." });
userEvent.click(select);
userEvent.click(getAllByRole("option")[0]);
userEvent.click(getAllByRole("option")[1]);
expect(onChange).toHaveBeenCalled();
expect(onChange).toHaveBeenCalledWith({ value: ["1", "2"] });
fireEvent.blur(select);
expect(onBlur).toHaveBeenCalled();
expect(onBlur).toHaveBeenCalledWith({ value: ["1", "2"] });

userEvent.click(select);
userEvent.click(getAllByRole("option")[0]);
userEvent.click(getAllByRole("option")[1]);
expect(onChange).toHaveBeenCalled();
expect(onChange).toHaveBeenCalledWith({ value: [], error: "This field is required. Please, enter a value." });
fireEvent.blur(select);
expect(onBlur).toHaveBeenCalled();
expect(onBlur).toHaveBeenCalledWith({ value: [], error: "This field is required. Please, enter a value." });
});
test("Controlled - Optional constraint", () => {
const onChange = jest.fn();
const onBlur = jest.fn();
Expand Down
43 changes: 19 additions & 24 deletions lib/src/select/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ const groupsHaveOptions = (innerOptions) =>
const filteredGroupsHaveOptions = (filteredOptions) =>
filteredOptions?.[0].options ? filteredOptions.some((groupOption) => groupOption.options?.length > 0) : true;

const canOpenOptions = (options, disabled) => !disabled && options?.length > 0 && groupsHaveOptions(options);

const filterOptionsBySearchValue = (options, searchValue) => {
if (options?.length > 0) {
if (options[0].options)
Expand Down Expand Up @@ -93,6 +95,8 @@ const getSelectedOption = (value, options, multiple, optional, optionalItem) =>
};
};

const notOptionalCheck = (value, multiple, optional) => !optional && (multiple ? value.length === 0 : value === "");

const DxcSelect = React.forwardRef<RefType, SelectPropsType>(
(
{
Expand Down Expand Up @@ -143,13 +147,8 @@ const DxcSelect = React.forwardRef<RefType, SelectPropsType>(
[value, innerValue, options, multiple, optional, optionalItem]
);

const notOptionalCheck = (value) => !optional && value === "";
const notOptionalMultipleCheck = (value) => !optional && value.length === 0;

const canBeOpenOptions = () => !disabled && options?.length > 0 && groupsHaveOptions(options);

const openOptions = () => {
if (!isOpen && canBeOpenOptions()) changeIsOpen(true);
if (!isOpen && canOpenOptions(options, disabled)) changeIsOpen(true);
};
const closeOptions = () => {
if (isOpen) {
Expand All @@ -159,23 +158,18 @@ const DxcSelect = React.forwardRef<RefType, SelectPropsType>(
};

const handleSelectChangeValue = (newOption) => {
if (multiple) {
let res = [];
let newValue;

if (multiple) {
if ((value ?? innerValue).includes(newOption.value))
res = (value ?? innerValue).filter((optionVal) => optionVal !== newOption.value);
else res = [...(value ?? innerValue), newOption.value];

value ?? setInnerValue(res);
if (notOptionalMultipleCheck(res))
onChange?.({ value: res, error: translatedLabels.formFields.requiredValueErrorMessage });
else onChange?.({ value: res });
} else {
value ?? setInnerValue(newOption.value);
if (notOptionalCheck(newOption.value))
onChange?.({ value: newOption.value, error: translatedLabels.formFields.requiredValueErrorMessage });
else onChange?.({ value: newOption.value });
}
newValue = (value ?? innerValue).filter((optionVal) => optionVal !== newOption.value);
else newValue = [...(value ?? innerValue), newOption.value];
} else newValue = newOption.value;

value ?? setInnerValue(newValue);
notOptionalCheck(newValue, multiple, optional)
? onChange?.({ value: newValue, error: translatedLabels.formFields.requiredValueErrorMessage })
: onChange?.({ value: newValue });
};
const handleSelectOnClick = () => {
searchable && selectSearchInputRef.current.focus();
Expand All @@ -193,9 +187,10 @@ const DxcSelect = React.forwardRef<RefType, SelectPropsType>(
closeOptions();
setSearchValue("");

if (notOptionalCheck(value ?? innerValue))
onBlur?.({ value: value ?? innerValue, error: translatedLabels.formFields.requiredValueErrorMessage });
else onBlur?.({ value: value ?? innerValue });
const currentValue = value ?? innerValue;
notOptionalCheck(currentValue, multiple, optional)
? onBlur?.({ value: currentValue, error: translatedLabels.formFields.requiredValueErrorMessage })
: onBlur?.({ value: currentValue });
}
};
const handleSelectOnKeyDown = (event) => {
Expand Down