mirror of
https://github.com/vercel/commerce.git
synced 2025-07-21 11:51:20 +00:00
✨ feat: Tab Common
This commit is contained in:
parent
36deb17fac
commit
7ece18782d
@ -1,46 +1,74 @@
|
||||
import React, { useState } from "react"
|
||||
import React, { useState, RefObject, useEffect, useRef } from "react"
|
||||
import s from './TabCommon.module.scss'
|
||||
|
||||
import TabItem from './TabItem/TabItem'
|
||||
|
||||
interface TabCommonProps {
|
||||
|
||||
changeTab: (target:string) => void;
|
||||
}
|
||||
|
||||
const TabCommon = ({ } : TabCommonProps) => {
|
||||
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<HTMLLIElement>(null);
|
||||
const item2 = useRef<HTMLLIElement>(null);
|
||||
const item3 = useRef<HTMLLIElement>(null);
|
||||
const slider = useRef<HTMLDivElement>(null);
|
||||
|
||||
function slide(ref: RefObject<HTMLLIElement>) {
|
||||
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)
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
slide(item1);
|
||||
}, [])
|
||||
|
||||
return (
|
||||
<section className={s.tabCommonOutSide}>
|
||||
<div className={s.tabCommon}>
|
||||
<span onClick={toggleItem1}><TabItem active={item1Active}>Wait for Comfirmation</TabItem></span>
|
||||
<span onClick={toggleItem2}><TabItem active={item2Active}>Delivering</TabItem></span>
|
||||
<span onClick={toggleItem3}><TabItem active={item3Active}>Delivered</TabItem></span>
|
||||
</div>
|
||||
</section>
|
||||
<ul className={s.tabCommon}>
|
||||
<li ref={item1}>
|
||||
<TabItem onClick={toggleItem1} active={item1Active}>Wait for Comfirmation</TabItem>
|
||||
</li>
|
||||
<li ref={item2}>
|
||||
<TabItem onClick={toggleItem2} active={item2Active}>Delivering</TabItem>
|
||||
</li>
|
||||
<li ref={item3}>
|
||||
<TabItem onClick={toggleItem3} active={item3Active}>Delivered</TabItem>
|
||||
</li>
|
||||
<div ref={slider} className={s.slider}></div>
|
||||
</ul>
|
||||
)
|
||||
}
|
||||
|
||||
|
@ -4,13 +4,14 @@ import s from './TabItem.module.scss'
|
||||
|
||||
interface TabItemProps {
|
||||
active: string;
|
||||
target?: string;
|
||||
children?: string;
|
||||
children: string;
|
||||
onClick: () => void;
|
||||
}
|
||||
|
||||
const TabItem = ({ active = "", children } : TabItemProps) => {
|
||||
const TabItem = ({ active = "", children, onClick } : TabItemProps) => {
|
||||
|
||||
return (
|
||||
<span className={classNames(s.tabItem, {
|
||||
<span onClick={onClick} className={classNames(s.tabItem, {
|
||||
[s[active]]: active
|
||||
})}>
|
||||
{children}
|
||||
|
Loading…
x
Reference in New Issue
Block a user