Skip to content

Commit

Permalink
[Fix] : Table 컴포넌트의 상위 context가 존재하지 않아도 useContext 에러가 발생하지 않도록 수정해요. (
Browse files Browse the repository at this point in the history
#174)

* fix: context가 존재하지 않아도 useContext 에러가 발생하지 않도록 수정

* feat: chagneset 작성

* fix: rowValue 기본 값 사용하지 않기 때문에 제거

* fix: 코드리뷰 반영
  • Loading branch information
eugene028 authored Nov 24, 2024
1 parent a51161c commit bcc0eee
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/eighty-cheetahs-kiss.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wowds-ui": patch
---

Table 컴포넌트의 context 문제를 해결해요
2 changes: 1 addition & 1 deletion packages/wow-ui/src/components/Table/Table.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export const Primary: Story = {
<Table.Tbody>
{data.map(({ name, studyId, id }) => {
return (
<Table.Tr key={id} value={id}>
<Table.Tr key={id}>
<Table.Td>{name}</Table.Td>
<Table.Td>{studyId}</Table.Td>
</Table.Tr>
Expand Down
14 changes: 11 additions & 3 deletions packages/wow-ui/src/components/Table/TableContext.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import type { Dispatch } from "react";
import { createContext } from "react";
import { createContext, useContext } from "react";

import type { TableProps } from "@/components/Table/Table";
import useSafeContext from "@/hooks/useSafeContext";
import type useTableCheckState from "@/hooks/useTableCheckState";

export const TableContext = createContext<
Expand All @@ -15,7 +14,16 @@ export const TableContext = createContext<
>(null);

export const useTableContext = () => {
const context = useSafeContext(TableContext);
const context = useContext(TableContext);
if (!context)
return {
selectedRows: new Set<number>(),
showCheckbox: false,
rowValues: new Set<number>(),
handleRowCheckboxChange: () => {},
handleHeaderCheckboxChange: () => {},
setRowValues: () => {},
};
return context;
};

Expand Down
14 changes: 10 additions & 4 deletions packages/wow-ui/src/components/Table/Td.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { cva } from "@styled-system/css";
import { styled } from "@styled-system/jsx";
import type { CSSProperties, PropsWithChildren, Ref } from "react";
import { forwardRef } from "react";
import { forwardRef, useContext, useEffect, useState } from "react";

import {
TableCheckedContext,
useTableContext,
} from "@/components/Table/TableContext";
import useSafeContext from "@/hooks/useSafeContext";

interface TableCellProps extends PropsWithChildren {
style?: CSSProperties;
Expand All @@ -18,8 +17,15 @@ const Td = forwardRef(
(props: TableCellProps, ref: Ref<HTMLTableCellElement>) => {
const { children, ...rest } = props;
const { selectedRows } = useTableContext();
const rowValue = useSafeContext(TableCheckedContext);
const isSelected = selectedRows.has(rowValue);
const [isSelected, setIsSelected] = useState(false);

const rowValue = useContext(TableCheckedContext);
useEffect(() => {
if (rowValue) {
const value = selectedRows.has(rowValue);
setIsSelected(value);
}
}, [rowValue, selectedRows]);

return (
<styled.td
Expand Down

0 comments on commit bcc0eee

Please sign in to comment.