/
Control.tsx
77 lines (70 loc) · 2.29 KB
/
Control.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
import React, { useState, useCallback, PropsWithChildren } from 'react'
import intersection from 'lodash/intersection'
import styled from 'styled-components'
import { MdKeyboardArrowRight, MdKeyboardArrowDown } from 'react-icons/md'
import { Flavor } from '../../types'
import PropertyDescription from './PropertyDescription'
import { PropertyFlavors } from './PropertyFlavors'
import { Cell } from './styled'
interface ControlProps {
id: string
description?: string
flavors: Flavor[]
currentFlavor: Flavor
supportedFlavors?: Flavor[]
}
export const Control = ({
id,
description,
flavors,
currentFlavor,
supportedFlavors,
children,
}: PropsWithChildren<ControlProps>) => {
const [showDescription, setShowDescription] = useState(false)
const toggle = useCallback(() => setShowDescription(flag => !flag), [setShowDescription])
let isPropertySupported = true
let showFlavors = false
if (Array.isArray(supportedFlavors)) {
if (intersection(flavors, supportedFlavors).length < flavors.length) {
showFlavors = true
}
if (!supportedFlavors.includes(currentFlavor)) {
isPropertySupported = false
}
}
return (
<Container id={id} isPropertySupported={isPropertySupported}>
{description !== undefined && (
<Toggle onClick={toggle}>
{showDescription && <MdKeyboardArrowDown size={18} />}
{!showDescription && <MdKeyboardArrowRight size={18} />}
</Toggle>
)}
{children}
{showFlavors && (
<PropertyFlavors flavors={flavors} supportedFlavors={supportedFlavors!} />
)}
{description && showDescription && <PropertyDescription description={description} />}
</Container>
)
}
const Container = styled(Cell)<{ isPropertySupported: boolean }>`
border-bottom: 1px solid ${({ theme }) => theme.colors.borderLight};
&:last-child {
border-bottom-width: 0;
}
`
const Toggle = styled.span`
user-select: none;
display: block;
position: absolute;
width: 20px;
top: 0;
bottom: 0;
left: 0;
padding-top: 11px;
padding-left: 1px;
cursor: pointer;
background: ${({ theme }) => theme.colors.borderLight};
`