Skip to content

Instantly share code, notes, and snippets.

@XaveScor
Created June 29, 2017 18:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save XaveScor/c84f5822dd6957c2b4c6b787548a02d9 to your computer and use it in GitHub Desktop.
Save XaveScor/c84f5822dd6957c2b4c6b787548a02d9 to your computer and use it in GitHub Desktop.
//Field.js
//@flow
import FieldPure from './Field.pure'
import React from 'react'
type PropsType = {
value: string,
header: string,
onChange: string => void | () => void,
type: 'text' | 'password',
className: string,
}
type StateType = {
value: string,
}
class Field extends React.Component<PropsType, PropsType, StateType> {
static defaultProps: PropsType = {
className: '',
header: '',
onChange: () => {},
type: 'text',
value: '',
}
state: StateType
constructor(props: PropsType) {
super(props)
this.state = {
value: props.value,
}
}
changeValue(value: string) {
this.setState({
...this.state,
value,
})
this.props.onChange(value)
}
render() {
return <FieldPure
OnChange={this.changeValue.bind(this)}
header={this.props.header}
value={this.state.value}
className={this.props.className}
/>
}
}
export default Field
////Field.pure.js
//@flow
import css from './Field.css'
type PropsType = {
value: string,
header: string,
OnChange: string => void,
className: string,
}
const FieldPure = ({header, value, OnChange, className}: PropsType) => {
const EventOnChange = (event: SyntheticInputEvent) => OnChange(event.target.value)
return (
<div className={className}>
<label>
<div className={css.header}>{header}:</div>
<input type='text' className={css.value} value={value} onChange={EventOnChange} />
</label>
</div>
)
}
export default FieldPure
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment