-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathSelect.js
88 lines (74 loc) · 1.74 KB
/
Select.js
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
import React from 'react';
import { FileText } from 'react-feather';
import styled from 'styled-components/macro';
import { COLORS, WEIGHTS } from '../../constants';
import Icon from '../Icon';
const Select = ({ label, value, children, ...delegated }) => {
const childArray = React.Children.toArray(children);
const selectedChild = childArray.find(
(child) => child.props.value === value
);
const displayedValue = selectedChild.props.children;
return (
<Wrapper>
<VisibleLabel>{label}</VisibleLabel>
<SelectWrapper>
<NativeSelect {...delegated}>{children}</NativeSelect>
<DisplayedBit>
{displayedValue}
<ChevronIcon
id="chevron-down"
size={24}
strokeWidth={1.5}
/>
</DisplayedBit>
</SelectWrapper>
</Wrapper>
);
};
const Wrapper = styled.label`
display: flex;
align-items: baseline;
`;
const VisibleLabel = styled.span`
color: ${COLORS.gray[700]};
margin-right: 16px;
`;
const SelectWrapper = styled.div`
position: relative;
`;
const NativeSelect = styled.select`
opacity: 0;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
width: 100%;
height: 100%;
cursor: pointer;
`;
const DisplayedBit = styled.span`
display: block;
background: ${COLORS.gray[100]};
font-size: 1rem;
font-weight: ${WEIGHTS.medium};
color: ${COLORS.gray[900]};
padding: 12px 42px 12px 16px;
border-radius: 8px;
pointer-events: none;
${NativeSelect}:focus ~ & {
outline: 1px dotted #212121;
outline: 5px auto -webkit-focus-ring-color;
}
`;
const ChevronIcon = styled(Icon)`
position: absolute;
top: 0;
right: 9px;
bottom: 0;
margin: auto;
width: 24px;
height: 24px;
`;
export default Select;