Files
react/components/tabs/tabs.tsx
witt bb575498fe chore: release v1.8.0 (#367)
* feat(snippet): add custom symbol and toast

* docs(snippet): add docs for custom symbol and toast

* fix(snippet): default toast type as normal type

* test(snippet): add custom symbol and toast

* docs(snippet): add enum type of APIs

* docs(toast): add normal-types

* chore: release v1.8.0-canary.1

* feat(modal): optimize the animation of layer

* test: update snapshots

* chore: release v1.8.0-canary.2

* docs: format import statement

* feat(modal): optimize the max width, height and alignment of layer

* test: update snapshots

* chore: release v1.8.0-canary.3

* Button (#319)

* feat(button): center the icon if the button is empty

* test(button): add testcase for icon only

* docs(button): add example for icon only

* chore: release v1.8.0-canary.4

* fix(snippet): remove space when the symbol is empty (#325)

* fix(snippet): remove space when the symbol is empty

style(snippet): remove unnecessary escape symbols

* fix(snippet): ignore spaces in symbol

style: fix lint warning

* chore: release v1.8.0-canary.5

* feat(tabs): sync the label and set value to required (#334)

* feat(tabs): sync the label and set value to required

* test(tabs): add testcase for label sync

* docs(tabs): update value to required

* feat(modal): use Button to reconstrust Modal.Action (#332)

* feat(modal): use Button to reconstrust Modal.Action

* docs(modal): add example for action loading

* test: update snapshots

* chore: release v1.8.0-canary.6

* fix(input): always synchronize external value and ignore changes (#336)

* fix(input): always synchronize external value and ignore changes

* feat(input): support imperative API to update

* fix(textarea): imperative api same as input (#341)

* feat(dropdown): allow dropdown to set the specified container (#344)

* feat(dropdown): allow dropdown to set the specified container

* test(modal): update snapshots

* docs(select): add example for custom popup container

* fix(dropdown): fix type of getPopupContainer

* test(dropdown): add testcase for specified container rendering

* chore: release v1.8.0-canary.7

* fix(link): fix hard-coded background color (#347)

* style(description): fix title font-weight (#348)

* docs(link): fix duplicate examples (#346)

* style(popover-item): the whole item should be clickable when using with link (#345)

* fix(modal): fix type of Modal.Action (#351)

* chore: release v1.8.0-canary.8

* feat(modal): lock tab action inside modal (#354)

* feat(button): add style to focus buttons

* feat(collections): add util function

* feat(modal): lock tab action inside modal

* test(modal): add tests for modal focus

* test: update style of button

* fix(table): fix column's props are not tracked (#362)

* chore: release v1.8.0-canary.9

* fix(table): children of column should be kept in sync (#364)

* chore: release v1.8.0-canary.10

Co-authored-by: Augusto <augustoconti10@gmail.com>
Co-authored-by: yqrashawn <namy.19@gmail.com>
Co-authored-by: Zhao Lei <firede@firede.us>
2020-08-14 16:59:49 +08:00

175 lines
4.4 KiB
TypeScript

import React, { useEffect, useMemo, useState } from 'react'
import TabsItem from './tabs-item'
import useTheme from '../styles/use-theme'
import { TabsLabelItem, TabsConfig, TabsContext } from './tabs-context'
interface Props {
initialValue?: string
value?: string
hideDivider?: boolean
onChange?: (val: string) => void
className?: string
}
const defaultProps = {
className: '',
hideDivider: false,
}
type NativeAttrs = Omit<React.HTMLAttributes<any>, keyof Props>
export type TabsProps = Props & typeof defaultProps & NativeAttrs
const Tabs: React.FC<React.PropsWithChildren<TabsProps>> = ({
initialValue: userCustomInitialValue,
value,
hideDivider,
children,
onChange,
className,
...props
}) => {
const theme = useTheme()
const [selfValue, setSelfValue] = useState<string | undefined>(userCustomInitialValue)
const [tabs, setTabs] = useState<Array<TabsLabelItem>>([])
const register = (next: TabsLabelItem) => {
setTabs(last => {
const hasItem = last.find(item => item.value === next.value)
if (!hasItem) return [...last, next]
return last.map(item => {
if (item.value !== next.value) return item
return {
...item,
label: next.label,
disabled: next.disabled,
}
})
})
}
const initialValue = useMemo<TabsConfig>(
() => ({
register,
currentValue: selfValue,
inGroup: true,
}),
[selfValue],
)
useEffect(() => {
if (value === undefined) return
setSelfValue(value)
}, [value])
const clickHandler = (item: TabsLabelItem) => {
if (item.disabled) return
setSelfValue(item.value)
onChange && onChange(item.value)
}
return (
<TabsContext.Provider value={initialValue}>
<div className={`tabs ${className}`} {...props}>
<header className={hideDivider ? 'hide-divider' : ''}>
{tabs.map(item => (
<div
className={`tab ${selfValue === item.value ? 'active' : ''} ${
item.disabled ? 'disabled' : ''
}`}
role="button"
key={item.value}
onClick={() => clickHandler(item)}>
{item.label}
</div>
))}
</header>
<div className="content">{children}</div>
<style jsx>{`
.tabs {
width: initial;
}
header {
display: flex;
flex-wrap: wrap;
align-items: center;
border-bottom: 1px solid ${theme.palette.border};
}
.hide-divider {
border-bottom: none;
}
.content {
padding-top: 0.625rem;
}
.tab {
padding: ${theme.layout.gapQuarter} calc(0.65 * ${theme.layout.gapQuarter});
cursor: pointer;
outline: 0;
transition: all 200ms ease;
text-transform: capitalize;
font-size: 1rem;
margin: 0 calc(0.8 * ${theme.layout.gapHalf});
color: ${theme.palette.accents_6};
user-select: none;
display: flex;
align-items: center;
line-height: 1.25rem;
position: relative;
}
.tab:after {
position: absolute;
content: '';
bottom: -1px;
left: 0;
right: 0;
width: 100%;
height: 2px;
transform: scaleX(0.75);
background-color: transparent;
transition: all 200ms ease;
}
.tab.active:after {
background-color: ${theme.palette.foreground};
transform: scaleX(1);
}
.tab :global(svg) {
max-height: 1em;
margin-right: 5px;
}
.tab:first-of-type {
margin-left: 0;
}
.tab.active {
color: ${theme.palette.foreground};
}
.tab.disabled {
color: ${theme.palette.accents_3};
cursor: not-allowed;
}
`}</style>
</div>
</TabsContext.Provider>
)
}
type TabsComponent<P = {}> = React.FC<P> & {
Item: typeof TabsItem
Tab: typeof TabsItem
}
type ComponentProps = Partial<typeof defaultProps> &
Omit<Props, keyof typeof defaultProps> &
NativeAttrs
Tabs.defaultProps = defaultProps
export default Tabs as TabsComponent<ComponentProps>