-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.tsx
106 lines (94 loc) · 3.29 KB
/
index.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
import React from 'react';
import uniqid from 'uniqid';
import { Alert } from '../Alert';
import { Label } from '../Label';
import styled from '../../../';
const arrow =
"'data:image/svg+xml,%3Csvg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 16 16%22%3E%3Cpath d=%22M8 11.841a.5.5 0 0 1-.348-.141L.735 5.018a.5.5 0 0 1 .695-.719L8 10.646l6.569-6.347a.5.5 0 1 1 .695.719L8.348 11.7a.5.5 0 0 1-.348.141z%22 fill=%22%23111%22/%3E%3C/svg%3E'";
const SelectContainer = styled.div({
marginBottom: '1.25em',
});
const SelectFieldContainer = styled.div<{ hasError: boolean; disabled?: boolean }>(({ hasError, disabled, theme }) => ({
position: 'relative',
maxWidth: 220,
border: '1px solid',
backgroundColor: theme.colors.white,
borderRadius: theme.borderRadius.small,
borderColor: hasError ? theme.colors.red : disabled ? theme.colors.lightGray : theme.colors.mediumGray,
'&:after': {
content: '""',
position: 'absolute',
top: 0,
right: 0,
pointerEvents: 'none',
background: `url(${arrow}) no-repeat center`,
backgroundSize: 16,
width: 40,
height: '100%',
},
}));
const SelectField = styled.select(({ theme }) => ({
font: 'inherit',
color: 'inherit',
appearance: 'none',
background: 'transparent',
border: 0,
width: '100%',
padding: '7px 11px', // To make it 11 when accounting for border
paddingRight: 40,
'&::-ms-expand': {
display: 'none',
},
'&:disabled': {
background: theme.colors.offWhite,
},
}));
export interface SelectItem {
key?: string;
label: string;
}
export interface SelectProps {
label?: string | React.ReactNode;
disabled?: boolean;
error?: string | boolean;
items?: SelectItem[];
value?: string;
name?: string;
placeholder?: string;
onChange: (event: React.FormEvent<HTMLSelectElement>) => void;
}
export const Select: React.FC<SelectProps> = ({ label, disabled, value, name, error, placeholder, onChange, items, ...rest }) => {
const id: string = uniqid();
if (!value && items) {
items = [
{
key: '-1',
label: placeholder ? placeholder : '',
},
...items,
];
}
return (
<SelectContainer {...rest}>
<Label htmlFor={id}>{label}</Label>
<SelectFieldContainer hasError={Boolean(error)} disabled={disabled}>
<SelectField id={id} disabled={disabled} name={name} value={value ? value : '-1'} onChange={onChange}>
{items ? (
items.map(item => (
<option
key={item.key ? item.key : item.label}
value={item.key ? item.key : item.label}
disabled={item.key === '-1'}
>
{item.label}
</option>
))
) : (
<option disabled={true} />
)}
</SelectField>
</SelectFieldContainer>
{error && typeof error === 'string' && <Alert type="error" message={error} alertSize="small" />}
</SelectContainer>
);
};