-
Notifications
You must be signed in to change notification settings - Fork 331
/
Copy pathfieldset-group.tsx
148 lines (132 loc) · 4.23 KB
/
fieldset-group.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import React, { useCallback, useMemo, useState } from 'react'
import useTheme from '../use-theme'
import useCurrentState from '../utils/use-current-state'
import { FieldsetContext, FieldItem } from './fieldset-context'
import useWarning from '../utils/use-warning'
import useScale, { withScale } from '../use-scale'
import useClasses from '../use-classes'
interface Props {
value: string
className?: string
onChange?: (value: string) => void
}
const defaultProps = {
className: '',
}
type NativeAttrs = Omit<React.HTMLAttributes<any>, keyof Props>
export type FieldsetGroupProps = Props & NativeAttrs
const FieldsetGroupComponent: React.FC<React.PropsWithChildren<FieldsetGroupProps>> = ({
className,
children,
value,
onChange,
...props
}: React.PropsWithChildren<FieldsetGroupProps> & typeof defaultProps) => {
const theme = useTheme()
const { SCALES } = useScale()
const [selfVal, setSelfVal] = useState<string>(value)
const [items, setItems, ref] = useCurrentState<FieldItem[]>([])
const classes = useClasses('group', className)
const register = (newItem: FieldItem) => {
const hasItem = ref.current.find(item => item.value === newItem.value)
if (hasItem) {
useWarning('The "value" of each "Fieldset" must be unique.', 'Fieldset')
}
setItems([...ref.current, newItem])
}
const providerValue = useMemo(
() => ({
currentValue: selfVal,
inGroup: true,
register,
}),
[selfVal],
)
const clickHandle = useCallback(
(nextValue: string) => {
setSelfVal(nextValue)
onChange && onChange(nextValue)
},
[onChange],
)
return (
<FieldsetContext.Provider value={providerValue}>
<div className={classes} {...props}>
<div className="group-tabs">
{items.map(item => (
<button
onClick={() => clickHandle(item.value)}
key={item.value}
className={selfVal === item.value ? 'active' : ''}
>
{item.label}
</button>
))}
</div>
<div className="group-content">{children}</div>
<style jsx>{`
.group {
width: ${SCALES.width(1, 'auto')};
height: ${SCALES.height(1, 'auto')};
padding: ${SCALES.pt(0)} ${SCALES.pr(0)} ${SCALES.pb(0)} ${SCALES.pl(0)};
margin: ${SCALES.mt(0)} ${SCALES.mr(0)} ${SCALES.mb(0, 0)} ${SCALES.ml(0)};
}
.group-tabs {
white-space: nowrap;
overflow-y: hidden;
overflow-x: auto;
font-size: ${SCALES.font(1)};
margin-bottom: -1px;
}
.group-content {
border-top-left-radius: 0;
overflow: hidden;
}
.group-content :global(.fieldset) {
border-top-left-radius: 0;
}
button {
height: 2.7em;
line-height: 2.7em;
text-align: center;
user-select: none;
color: ${theme.palette.accents_3};
background-color: ${theme.palette.accents_1};
font-size: 0.875em;
white-space: nowrap;
text-transform: capitalize;
-webkit-appearance: none;
cursor: pointer;
margin: 0;
padding: 0 1.45em;
overflow: hidden;
transition: all 0.2s ease 0s;
border-radius: 0;
border: 1px solid ${theme.palette.border};
text-decoration: none;
outline: none;
}
button.active {
border-bottom-color: transparent;
background-color: ${theme.palette.background};
color: ${theme.palette.foreground};
cursor: default;
}
button:first-of-type {
border-top-left-radius: ${theme.layout.radius};
}
button:last-of-type {
border-top-right-radius: ${theme.layout.radius};
}
button + button {
border-left: 0;
}
`}</style>
</div>
</FieldsetContext.Provider>
)
}
FieldsetGroupComponent.defaultProps = defaultProps
FieldsetGroupComponent.displayName = 'GeistFieldsetGroup'
const FieldsetGroup = withScale(FieldsetGroupComponent)
export default FieldsetGroup