From ddbfca3f16fcba6987329bb85656ccb8bb95a547 Mon Sep 17 00:00:00 2001 From: yoosangwook Date: Fri, 27 Dec 2024 14:46:32 +0900 Subject: [PATCH] =?UTF-8?q?=F0=9F=93=8C=20fix:=20Add=20sample=20reducer?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/Playground.jsx | 4 + src/components/sample/SampleReducer.jsx | 115 ++++++++++++++++++++++++ 2 files changed, 119 insertions(+) create mode 100644 src/components/sample/SampleReducer.jsx diff --git a/src/components/Playground.jsx b/src/components/Playground.jsx index e2901061..fcc5468d 100644 --- a/src/components/Playground.jsx +++ b/src/components/Playground.jsx @@ -25,6 +25,7 @@ import QPagination from './common/pagination/QPagination' import { trestleRequestModels, constructionRequestModels, trestleDetailRequestModels } from '@/models/apiModels' import QSelectBox from './common/select/QSelectBox' +import SampleReducer from './sample/SampleReducer' export default function Playground() { const [useCadFile, setUseCadFile] = useRecoilState(useCadFileState) @@ -504,6 +505,9 @@ export default function Playground() {
+
+ +
) diff --git a/src/components/sample/SampleReducer.jsx b/src/components/sample/SampleReducer.jsx new file mode 100644 index 00000000..26150fea --- /dev/null +++ b/src/components/sample/SampleReducer.jsx @@ -0,0 +1,115 @@ +import { Card, CardBody, Input, Tab, Tabs } from '@nextui-org/react' +import { useEffect, useReducer } from 'react' + +const reducer = (prevState, nextState) => { + return { ...prevState, ...nextState } +} + +const defaultData = { + commonData: 'common', + tabs: [ + { + id: 1, + name: 'tab1', + range: 10, + maker: 'maker1', + law: 'law1', + basis: 'basis1', + }, + { + id: 2, + name: 'tab2', + range: 20, + maker: 'maker2', + law: 'law2', + basis: 'basis2', + }, + { + id: 3, + name: 'tab3', + range: 30, + maker: 'maker3', + law: 'law3', + basis: 'basis3', + }, + { + id: 4, + name: 'tab4', + range: 40, + maker: 'maker4', + law: 'law4', + basis: 'basis4', + }, + ], +} + +export default function SampleReducer() { + const [sampleState, setSampleState] = useReducer(reducer, defaultData) + + const handleChangeTabsData = (newTab) => { + const newTabs = sampleState.tabs.map((t) => { + if (t.id === newTab.id) { + return newTab + } else { + return t + } + }) + setSampleState({ ...sampleState, tabs: newTabs }) + } + + useEffect(() => { + console.log('🚀 ~ SampleReducer ~ sampleState:', sampleState) + }, [sampleState]) + + return ( + <> +
공통: {sampleState.commonData}
+
+ + {sampleState.tabs.map((s) => ( + + + +
+ { + handleChangeTabsData({ ...s, range: e.target.value }) + }} + /> + { + handleChangeTabsData({ ...s, maker: e.target.value }) + }} + /> + { + handleChangeTabsData({ ...s, law: e.target.value }) + }} + /> + { + handleChangeTabsData({ ...s, basis: e.target.value }) + }} + /> +
+
+
+
+ ))} +
+
+ + ) +}