-
Notifications
You must be signed in to change notification settings - Fork 293
/
Copy pathindex.js
57 lines (50 loc) · 1.2 KB
/
index.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
import React from 'react'
import PropTypes from 'prop-types'
import styled from 'styled-components'
import { Label, Input, Block } from 'components'
const Error = styled(Block)`
margin: 0.5rem 0 0;
`
const Wrapper = styled.div`
margin-bottom: 1rem;
input[type="checkbox"],
input[type="radio"] {
margin-right: 0.5rem;
}
label {
vertical-align: middle;
}
`
const Field = ({
error, name, invalid, label, type, ...props
}) => {
const inputProps = {
id: name, name, type, invalid, 'aria-describedby': `${name}Error`, ...props,
}
const renderInputFirst = type === 'checkbox' || type === 'radio'
return (
<Wrapper>
{renderInputFirst && <Input {...inputProps} />}
{label && <Label htmlFor={inputProps.id}>{label}</Label>}
{renderInputFirst || <Input {...inputProps} />}
{invalid && error
&& (
<Error id={`${name}Error`} role="alert" palette="danger">
{error}
</Error>
)
}
</Wrapper>
)
}
Field.propTypes = {
name: PropTypes.string.isRequired,
invalid: PropTypes.bool,
error: PropTypes.string,
label: PropTypes.string,
type: PropTypes.string,
}
Field.defaultProps = {
type: 'text',
}
export default Field