From ec8b9fbcdf2707768e69e5cee363149d7f9dac3e Mon Sep 17 00:00:00 2001 From: sonnguyenkieio <89386072+sonnguyenkieio@users.noreply.github.com> Date: Tue, 7 Sep 2021 12:14:34 +0700 Subject: [PATCH] :sparkles: feat: Tab Common --- src/components/common/TabCommon/TabCommon.tsx | 77 +++++-------------- 1 file changed, 19 insertions(+), 58 deletions(-) diff --git a/src/components/common/TabCommon/TabCommon.tsx b/src/components/common/TabCommon/TabCommon.tsx index 67fe107a6..60a2649f6 100644 --- a/src/components/common/TabCommon/TabCommon.tsx +++ b/src/components/common/TabCommon/TabCommon.tsx @@ -1,75 +1,36 @@ -import React, { useState, RefObject, useEffect, useRef } from "react" +import React, { RefObject, useEffect } from "react" import s from './TabCommon.module.scss' import TabItem from './TabItem/TabItem' interface TabCommonProps { - changeTab: (target:string) => void; + tabs: {ref:RefObject, tabName: string, active: boolean, onClick: (tabIndex: number, tabPane: string) => void}[]; + defaultActiveTab: number; + sliderRef : RefObject; + slideToTab: (ref: any) => void; } -const TabCommon = ({ changeTab } : TabCommonProps) => { - const active = "active", unActive = ""; - const [item1Active, setItem1Active] = useState(active); - const [item2Active, setItem2Active] = useState(unActive); - const [item3Active, setItem3Active] = useState(unActive); - - const item1 = useRef(null); - const item2 = useRef(null); - const item3 = useRef(null); - const slider = useRef(null); - - function slide(ref: RefObject) { - const width = ref.current.offsetWidth; - const left = ref.current.offsetLeft; - - slider.current.style.width = (width-48).toString()+"px"; - slider.current.style.left = left.toString()+"px"; - } - - function toggleItem1():void { - setItem1Active(active) - changeTab("waiting") - - setItem2Active(unActive) - setItem3Active(unActive) - slide(item1) - } - - function toggleItem2():void { - setItem2Active(active) - changeTab("delivering") - - setItem1Active(unActive) - setItem3Active(unActive) - slide(item2) - } - function toggleItem3():void { - setItem3Active(active) - changeTab("delivered") - - setItem1Active(unActive) - setItem2Active(unActive) - slide(item3) - } +const TabCommon = ({ tabs, defaultActiveTab, sliderRef, slideToTab } : TabCommonProps) => { useEffect(() => { - slide(item1); + slideToTab(tabs[defaultActiveTab].ref); }, []) return (
    -
  • - Wait for Comfirmation -
  • -
  • - Delivering -
  • -
  • - Delivered -
  • -
    + { + tabs.map((tab) => { + return ( +
  • + {tab.tabName} +
  • + ) + }) + } + +
) } -export default TabCommon; \ No newline at end of file +export default TabCommon;