mirror of
https://github.com/muerwre/vault-frontend.git
synced 2025-04-25 12:56:41 +07:00
made better tabs (with context)
This commit is contained in:
parent
f63d2d3228
commit
da510e346a
12 changed files with 103 additions and 194 deletions
|
@ -1,16 +0,0 @@
|
|||
import React, { FC, MouseEventHandler } from 'react';
|
||||
import classNames from 'classnames';
|
||||
import styles from './styles.module.scss';
|
||||
|
||||
interface IProps {
|
||||
active?: boolean;
|
||||
onClick?: MouseEventHandler<any>;
|
||||
}
|
||||
|
||||
const Tab: FC<IProps> = ({ active, onClick, children }) => (
|
||||
<div className={classNames(styles.tab, { [styles.active]: active })} onClick={onClick}>
|
||||
{children}
|
||||
</div>
|
||||
);
|
||||
|
||||
export { Tab };
|
|
@ -1,20 +0,0 @@
|
|||
@import "src/styles/variables";
|
||||
|
||||
.tab {
|
||||
@include outer_shadow();
|
||||
|
||||
padding: $gap;
|
||||
margin-right: $gap;
|
||||
border-radius: $radius $radius 0 0;
|
||||
font: $font_14_semibold;
|
||||
text-transform: uppercase;
|
||||
cursor: pointer;
|
||||
background-color: $content_bg;
|
||||
color: white;
|
||||
text-decoration: none;
|
||||
border: none;
|
||||
|
||||
&.active {
|
||||
background: lighten($content_bg, 4%);
|
||||
}
|
||||
}
|
|
@ -1,12 +1,51 @@
|
|||
import React, { FC, useCallback } from 'react';
|
||||
import React, { createContext, FC, VFC, useContext, useState } from 'react';
|
||||
import styles from './styles.module.scss';
|
||||
import classNames from 'classnames';
|
||||
import { IAuthState } from '~/redux/auth/types';
|
||||
|
||||
interface IProps {}
|
||||
interface TabProps {
|
||||
items: string[];
|
||||
}
|
||||
|
||||
const Tabs: FC<IProps> = ({ children }) => {
|
||||
return <div className={styles.wrap}>{children}</div>;
|
||||
const TabContext = createContext({
|
||||
activeTab: 0,
|
||||
setActiveTab: (activeTab: number) => {},
|
||||
});
|
||||
|
||||
const List: VFC<TabProps> = ({ items }) => {
|
||||
const { activeTab, setActiveTab } = useContext(TabContext);
|
||||
|
||||
return (
|
||||
<div className={styles.tabs}>
|
||||
{items.map((it, index) => (
|
||||
<div
|
||||
className={classNames(styles.tab, { [styles.active]: activeTab === index })}
|
||||
onClick={() => setActiveTab(index)}
|
||||
key={it}
|
||||
>
|
||||
{it}
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
const Content: FC<any> = ({ children }) => {
|
||||
const { activeTab } = useContext(TabContext);
|
||||
|
||||
if (!Array.isArray(children) && activeTab > 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return Array.isArray(children) ? children[activeTab] : children;
|
||||
};
|
||||
|
||||
const Tabs = function({ children }) {
|
||||
const [activeTab, setActiveTab] = useState(0);
|
||||
|
||||
return <TabContext.Provider value={{ activeTab, setActiveTab }}>{children}</TabContext.Provider>;
|
||||
};
|
||||
|
||||
Tabs.List = List;
|
||||
Tabs.Content = Content;
|
||||
|
||||
export { Tabs };
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
@import "src/styles/variables";
|
||||
@import "~/styles/variables";
|
||||
|
||||
.wrap {
|
||||
display: flex;
|
||||
|
@ -6,3 +6,27 @@
|
|||
justify-content: flex-start;
|
||||
padding: 0 $gap / 2;
|
||||
}
|
||||
|
||||
.tab {
|
||||
@include outer_shadow();
|
||||
|
||||
padding: $gap;
|
||||
margin-right: $gap;
|
||||
border-radius: $radius $radius 0 0;
|
||||
font: $font_14_semibold;
|
||||
text-transform: uppercase;
|
||||
cursor: pointer;
|
||||
background-color: $content_bg;
|
||||
color: white;
|
||||
text-decoration: none;
|
||||
border: none;
|
||||
|
||||
&.active {
|
||||
background: lighten($content_bg, 4%);
|
||||
}
|
||||
}
|
||||
|
||||
.tabs {
|
||||
display: flex;
|
||||
flex-direction: row;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue