Skip to content

Commit

Permalink
Fix: respect defaultValue in Select and MultiSelect (#522)
Browse files Browse the repository at this point in the history
* feat: add useUpdateEffect hook

* fix(SingleSelect): respect defaultValue

* test(SingleSelect): respect defaultValue

* fix(MultiSelect): respect defaultValue

* test(MultiSelect): respect defaultValue
  • Loading branch information
ariser authored Jan 20, 2025
1 parent b38422c commit bf10971
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,8 @@ module.exports = {
singleReturnOnly: false,
},
],
"react-hooks/exhaustive-deps": ["warn", {
"additionalHooks": "(useUpdateEffect)"
}]
},
};
10 changes: 10 additions & 0 deletions src/components/Select/MultiSelect.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,16 @@ describe("MultiSelect", () => {
expect(queryByTestingText(selectTrigger, "Content3")).toBeNull();
});

it("should respect given defaultValue in select", () => {
const { getByTestId } = renderSelect({
defaultValue: ["content0", "content2"],
});
const selectTrigger = getByTestId("select-trigger");
expect(selectTrigger).not.toBeNull();
expect(queryByTestingText(selectTrigger, "Content0")).not.toBeNull();
expect(queryByTestingText(selectTrigger, "Content2")).not.toBeNull();
});

it("should show error", () => {
const { queryByText } = renderSelect({
error: "Select Error",
Expand Down
6 changes: 4 additions & 2 deletions src/components/Select/MultiSelect.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { useCallback, useEffect, useState } from "react";
import { useCallback, useState } from "react";

import {useUpdateEffect} from "@/hooks";

import { SelectContainerProps, SelectOptionProp, SelectionType } from "./common/types";
import {
Expand Down Expand Up @@ -43,7 +45,7 @@ export const MultiSelect = ({
[onOpenChangeProp]
);

useEffect(() => {
useUpdateEffect(() => {
setSelectedValues(valueProp ?? []);
}, [valueProp]);

Expand Down
8 changes: 8 additions & 0 deletions src/components/Select/SingleSelect.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,14 @@ describe("Select", () => {
expect(queryByTestingText(selectTrigger, "Content0")).not.toBeNull();
});

it("should respect given defaultValue in select", () => {
const { getByTestId } = renderSelect({
defaultValue: "content0",
});
const selectedValue = getByTestId("select-trigger");
expect(selectedValue.textContent).toBe("Content0");
});

it("should render options", () => {
const { queryByText, getByTestId } = renderSelect({
options: selectOptions,
Expand Down
7 changes: 5 additions & 2 deletions src/components/Select/SingleSelect.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { useCallback, useEffect, useState } from "react";
import { useCallback, useState } from "react";

import { useUpdateEffect } from "@/hooks";

import { SelectContainerProps, SelectOptionProp, SelectionType } from "./common/types";
import { InternalSelect, SelectGroup, SelectItem } from "./common/InternalSelect";

Expand Down Expand Up @@ -65,7 +68,7 @@ export const Select = ({
[selectedValues, onSelect]
);

useEffect(() => {
useUpdateEffect(() => {
setSelectedValues(valueProp ? [valueProp] : []);
}, [valueProp]);

Expand Down
1 change: 1 addition & 0 deletions src/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./useUpdateEffect";
15 changes: 15 additions & 0 deletions src/hooks/useUpdateEffect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { useEffect, useRef } from "react";

export const useUpdateEffect: typeof useEffect = (effect, deps) => {
const isFirstMount = useRef(true);

useEffect(() => {
if (isFirstMount) {
isFirstMount.current = false;
return;
}

return effect();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, deps);
};

0 comments on commit bf10971

Please sign in to comment.