forked from diegohaz/arc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
50 lines (43 loc) · 1.15 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
import React, { PropTypes } from 'react'
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