From 36f527fe933b601438f8385dd8291341f072b01b Mon Sep 17 00:00:00 2001 From: Jakub Bajkowski Date: Wed, 18 Dec 2024 13:17:00 +0100 Subject: [PATCH] add task solution --- src/App.tsx | 43 ++++++++------------------ src/components/Tabs/Tabs.tsx | 52 +++++++++++++++++++++++++++++++- src/components/Tabs/Types/Tab.ts | 5 +++ 3 files changed, 68 insertions(+), 32 deletions(-) create mode 100644 src/components/Tabs/Types/Tab.ts diff --git a/src/App.tsx b/src/App.tsx index b048a6e65..33744d945 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,46 +1,27 @@ -import React from 'react'; +import React, { useState } from 'react'; import 'bulma/css/bulma.css'; import '@fortawesome/fontawesome-free/css/all.css'; import './App.scss'; +import { Tabs } from './components/Tabs'; +import { Tab } from './components/Tabs/Types/Tab'; -export const tabs = [ +export const tabs: Tab[] = [ { id: 'tab-1', title: 'Tab 1', content: 'Some text 1' }, { id: 'tab-2', title: 'Tab 2', content: 'Some text 2' }, { id: 'tab-3', title: 'Tab 3', content: 'Some text 3' }, ]; export const App: React.FC = () => { + const [selectedTab, setSelectedTab] = useState(tabs[0]); + return (
-

Selected tab is Tab 1

- -
-
- -
- -
- Some text 1 -
-
+

{`Selected tab is ${selectedTab.title}`}

+
); }; diff --git a/src/components/Tabs/Tabs.tsx b/src/components/Tabs/Tabs.tsx index 4b321a091..c21914e06 100644 --- a/src/components/Tabs/Tabs.tsx +++ b/src/components/Tabs/Tabs.tsx @@ -1 +1,51 @@ -export const Tabs = () => {}; +import React from 'react'; +import cn from 'classnames'; +import { Tab } from '../Tabs/Types/Tab'; + +type Props = { + tabs: Tab[]; + selectedTabId: string; + onTabSelected: (tab: Tab) => void; +}; + +export const Tabs: React.FC = ({ + tabs, + selectedTabId, + onTabSelected, +}) => { + const selectedTab = tabs.find(tab => tab.id === selectedTabId) || tabs[0]; + + const selectTab = (tab: Tab) => { + if (tab.id !== selectedTabId) { + onTabSelected(tab); + } + }; + + return ( +
+
+ +
+ +
+ {selectedTab.content} +
+
+ ); +}; diff --git a/src/components/Tabs/Types/Tab.ts b/src/components/Tabs/Types/Tab.ts new file mode 100644 index 000000000..350063e1e --- /dev/null +++ b/src/components/Tabs/Types/Tab.ts @@ -0,0 +1,5 @@ +export interface Tab { + id: string; + title: string; + content: string; +}