Created
April 3, 2018 20:39
-
-
Save SergProduction/2266b4a2c099065a85f3ab5b6f31b3ef to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class GenForm { | |
static propTypes = { | |
type: pt.oneOf(['checkbox', radioButton]).isRequared, | |
description: pt.string, | |
radioOptions: pt.arrayOf(pt.shape({ | |
name: pt.string, | |
description: pt.string, | |
})), | |
} | |
state = { | |
checkbox: '', | |
radioButton: '', | |
} | |
handleCheckbox = (event) => { | |
this.setState({checkbox: !event.target.checked}) | |
} | |
handleRadioButton = (event) => { | |
this.setState({radioButton: event.target.name}) | |
} | |
renderCheckbox = () => ( | |
<input | |
type="checkbox" | |
checked={this.state.checkbox} | |
onChange={this.handleCheckbox} | |
> | |
) | |
renderRadioButton = () => ( | |
<div onChange={this.handleRadioButton}> | |
{this.props.radioOptions.map(opt => ( | |
<input | |
type="radio" | |
name={opt.name} | |
value={opt.name} | |
checked={opt.name === this.state.radioButton} | |
> | |
{opt.decription} | |
</input> | |
))} | |
</div> | |
) | |
render() { | |
const { type } = this.props | |
if (type === 'checkbox') return this.renderCheckbox() | |
if (type === 'radioButton') return this.renderRadioButton() | |
} | |
} | |
const api = () => new Propmise((res) => { | |
res([ | |
{ | |
type: 'radioButton', | |
radioOptions: [ | |
{name: 'Gender', description: 'ПОЛ'} | |
{name: 'name', description: 'ИМЯ'} | |
], | |
}, | |
{ | |
type: 'checkbox', | |
description: 'Вы согласны принять условия?' | |
} | |
]) | |
}) | |
class GetData { | |
state = { | |
data: null | |
} | |
didMount() { | |
api.get() | |
.then(data => this.setState({data})) | |
} | |
render() { | |
const { data } = this.state | |
if (data) return null | |
return data.map(form => ( | |
<GenForm | |
type={form.type} | |
radioOptions={form.radioOptions} | |
description={form.description} | |
/> | |
)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
class Test extends Component {
state = {
data: {},
isReady: false,
}
componentDidMount() {
const { actions } = this.props
let result = {}
}
render() {
const { actions } = this.props
}
}