react-toolbox/spec/components/form.js

78 lines
2.4 KiB
JavaScript
Raw Normal View History

import React from 'react';
2015-09-21 11:01:52 +03:00
import Form from '../../components/form';
const countriesArray = ['Spain', 'England', 'USA', 'Thailand', 'France'];
const countries = [
{ value: 'EN-gb', label: 'England', img: 'http://' },
{ value: 'ES-es', label: 'Spain', img: 'http://' },
{ value: 'TH-th', label: 'Thailand', img: 'http://' },
{ value: 'EN-en', label: 'USA', img: 'http://' },
2017-01-26 20:05:32 +03:00
{ value: 'FR-fr', label: 'France', img: 'http://' },
];
const FormFields = {
2017-01-26 20:05:32 +03:00
autocomplete: { kind: 'Autocomplete', label: 'Autocomplete', source: countriesArray, value: '' },
input: { kind: 'Input', type: 'text', label: 'Input', value: '@soyjavi', required: true },
multiline: { kind: 'Input', type: 'text', label: 'Input (multiline)', multiline: true },
number: { kind: 'Input', type: 'number', label: 'Input (number)' },
checkbox: { kind: 'Checkbox', label: 'Checkbox' },
date: { kind: 'DatePicker', label: 'DatePicker', value: undefined },
dropdown: { kind: 'Dropdown', label: 'Dropdown', source: countries, value: countries[2].value },
time: { kind: 'TimePicker', label: 'TimePicker', value: undefined },
switch: { kind: 'Switch', label: 'Switch' },
slider: { kind: 'Slider', label: 'Slider', min: 0, max: 10, value: 4, pinned: true },
submit: { kind: 'Button', type: 'submit', label: 'Button () you a nomad?' },
};
2015-10-22 02:31:17 +03:00
class FormTest extends React.Component {
state = {
model: {
autocomplete: countriesArray[3],
input: 'soyjavi',
multiline: 'Overwritten',
number: 0,
2017-01-26 20:05:32 +03:00
checkbox: true,
},
};
2015-09-21 11:01:52 +03:00
2015-10-22 02:31:17 +03:00
handleEvent = (type, event, form) => {
console.log(`[FORM.${type}]`, event, form);
};
handleChange = (field, value) => {
console.log('FORM.change', field, value);
const model = {
...this.state.model,
2017-01-26 20:05:32 +03:00
[field]: value,
};
this.setState({ model });
2015-10-22 02:31:17 +03:00
};
2015-09-21 11:01:52 +03:00
2017-01-26 20:05:32 +03:00
render() {
Object.keys(this.state.model).map((field) => {
const formField = FormFields[field];
formField[formField.hasOwnProperty('value') ? 'value' : 'checked'] = this.state.model[field];
});
console.log(FormFields);
2015-09-21 11:01:52 +03:00
return (
<section>
<h5>Form</h5>
2015-09-21 11:01:52 +03:00
<p>lorem ipsum...</p>
<Form
model={FormFields}
onChange={this.handleChange}
2015-10-22 02:31:17 +03:00
onError={this.handleEvent.bind(this, 'error')}
onValid={this.handleEvent.bind(this, 'valid')}
2017-01-26 20:05:32 +03:00
onSubmit={this.handleEvent.bind(this, 'submit')}
/>
2015-09-21 11:01:52 +03:00
</section>
);
}
2015-10-21 13:25:07 +03:00
}
2015-10-22 02:31:17 +03:00
export default FormTest;