import React from 'react'
import { asField } from 'informed'
import system from '@rebass/components'
import { styles } from 'styled-system'
import { withTheme } from 'styled-components'
import { Flex } from 'rebass'
import { Message } from 'components/UI'
// Create an html textarea element that accepts all style props from styled-system.
const SystemTextArea = system(
{
as: 'textarea',
border: 1,
borderColor: 'gray',
borderRadius: '5px',
bg: 'transparent',
color: 'primaryText',
fontFamily: 'sans',
fontSize: 'm',
fontWeight: 'light',
p: 3,
width: 1,
rows: 5
},
...Object.keys(styles)
)
/**
* @render react
* @name TextArea
* @example
*
*/
class TextArea extends React.PureComponent {
static displayName = 'TextArea'
state = {
hasFocus: false
}
constructor(props) {
super(props)
const { forwardedRef } = this.props
this.inputRef = forwardedRef || React.createRef()
}
render() {
const {
css,
onChange,
onBlur,
onFocus,
forwardedRef,
theme,
fieldApi,
fieldState,
justifyContent,
...rest
} = this.props
const { readOnly } = this.props
const { hasFocus } = this.state
const { setValue, setTouched } = fieldApi
const { value } = fieldState
const isValid = value && !fieldState.error
// Calculate the border color based on the current field state.
let borderColor
if (readOnly) {
borderColor = theme.colors.gray
} else if (fieldState.error) {
borderColor = theme.colors.superRed
} else if (value && !fieldState.error) {
borderColor = theme.colors.superGreen
}
return (
{
setValue(e.target.value)
if (onChange) {
onChange(e)
}
}}
onBlur={e => {
setTouched()
// Make the state aware that the element is now focused.
const newHasFocus = document.activeElement === this.inputRef.current
if (hasFocus !== newHasFocus) {
this.setState({ hasFocus: newHasFocus })
}
if (onBlur) {
onBlur(e)
}
}}
onFocus={e => {
// Make the state aware that the element is no longer focused.
const newHasFocus = document.activeElement === this.inputRef.current
if (hasFocus !== newHasFocus) {
this.setState({ hasFocus: newHasFocus })
}
if (onFocus) {
onFocus(e)
}
}}
error={fieldState.error}
/>
{fieldState.error && (
{fieldState.error}
)}
)
}
}
export default asField(withTheme(TextArea))