Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] : Table 컴포넌트의 상위 context가 존재하지 않아도 useContext 에러가 발생하지 않도록 수정해요. #174

Merged
merged 4 commits into from
Nov 24, 2024
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
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
Loading