|
@@ -1,95 +1,103 @@
|
|
|
import React, { Component } from "react";
|
|
|
+import { reduxForm, Field, FieldArray } from "redux-form";
|
|
|
|
|
|
-export default class LoadForm extends Component {
|
|
|
+const renderField = ({ input, meta: { touched, error }, label, type }) => (
|
|
|
+ <div className="form__input-box">
|
|
|
+ <label className="form__input-lable" htmlFor="origin">
|
|
|
+ {label}
|
|
|
+ <input type={type} {...input} />
|
|
|
+ </label>
|
|
|
+ {touched && error && <span>{error}</span>}
|
|
|
+ </div>
|
|
|
+);
|
|
|
+
|
|
|
+const ololo = values => {
|
|
|
+ const { origin, destination } = values;
|
|
|
+
|
|
|
+ const errors = {};
|
|
|
+ if (!origin) {
|
|
|
+ errors.origin = "Required";
|
|
|
+ }
|
|
|
+ if (!destination) {
|
|
|
+ errors.destination = "Required";
|
|
|
+ }
|
|
|
+
|
|
|
+ return errors;
|
|
|
+};
|
|
|
+
|
|
|
+const renderHobbies = ({ fields, meta: { error } }) => {
|
|
|
+ console.log(fields);
|
|
|
+ return (
|
|
|
+ <ul>
|
|
|
+ <li>
|
|
|
+ <button type="button" onClick={() => fields.push()}>
|
|
|
+ Добавить груз
|
|
|
+ </button>
|
|
|
+ </li>
|
|
|
+
|
|
|
+ {fields.map((el, index) => (
|
|
|
+ <li key={index}>
|
|
|
+ <button type="button" title="Remove Hobby" onClick={() => fields.remove(index)}>
|
|
|
+ x
|
|
|
+ </button>
|
|
|
+
|
|
|
+ <Field name={`${el}.name`} label="Груз" type="text" component={renderField} />
|
|
|
+ </li>
|
|
|
+ ))}
|
|
|
+ {error && <li className="error">{error}</li>}
|
|
|
+ </ul>
|
|
|
+ );
|
|
|
+};
|
|
|
+
|
|
|
+class LoadForm extends Component {
|
|
|
changeHandler = e => {
|
|
|
const { changeInputValue } = this.props;
|
|
|
const { value, id, checked } = e.target;
|
|
|
changeInputValue({ value, id, checked });
|
|
|
};
|
|
|
|
|
|
+ submit = values => {
|
|
|
+ const { addToList } = this.props;
|
|
|
+
|
|
|
+ addToList(values);
|
|
|
+ };
|
|
|
+
|
|
|
render() {
|
|
|
- const { addToList, loads, changeInputValue, addLoads } = this.props;
|
|
|
- return (
|
|
|
- <div className="form">
|
|
|
- <div className="form__input-box">
|
|
|
- <label className="form__input-lable" htmlFor="origin">
|
|
|
- Город отправитель
|
|
|
- <input onChange={e => this.changeHandler(e)} className="form__input" id="origin" type="text" />
|
|
|
- </label>
|
|
|
- </div>
|
|
|
- <div className="form__input-box">
|
|
|
- <label className="form__input-lable" htmlFor="destination">
|
|
|
- Город получатель
|
|
|
- <input
|
|
|
- onChange={e => this.changeHandler(e)}
|
|
|
- className="form__input"
|
|
|
- id="destination"
|
|
|
- type="text"
|
|
|
- />
|
|
|
- </label>
|
|
|
- </div>
|
|
|
- <div className="form__input-box">
|
|
|
- <label className="form__input-lable" htmlFor="name">
|
|
|
- Введите имя водителя
|
|
|
- <input onChange={e => this.changeHandler(e)} className="form__input" id="name" type="text" />
|
|
|
- </label>
|
|
|
- </div>
|
|
|
+ const { handleSubmit } = this.props;
|
|
|
+ console.log("tssad", this.props);
|
|
|
|
|
|
- <div className="form__input-box">
|
|
|
- <label className="form__input-lable" htmlFor="night">
|
|
|
- Ночная перевозка ?
|
|
|
- <input onChange={e => this.changeHandler(e)} className="form__input" id="night" type="checkbox" />
|
|
|
- </label>
|
|
|
- </div>
|
|
|
+ return (
|
|
|
+ <form onSubmit={handleSubmit(this.submit)} className="form">
|
|
|
+ <Field name="origin" label="Город отправитель" type="text" component={renderField} />
|
|
|
+ <Field name="destination" label="Город получатель" type="text" component={renderField} />
|
|
|
+ <Field name="contactName" label="Введите имя водителя" type="text" component={renderField} />
|
|
|
+ <Field name="night" label="Ночная перевозка ?" type="checkbox" component={renderField} />
|
|
|
|
|
|
<div className="form__input-box">
|
|
|
<label className="form__input-lable" htmlFor="bodyType">
|
|
|
Тип кузова
|
|
|
- <select
|
|
|
- onChange={e => this.changeHandler(e)}
|
|
|
- className="form__input"
|
|
|
- name="bodyType"
|
|
|
- id="bodyType">
|
|
|
+ <Field name="bodyType" component="select">
|
|
|
<option value="type1">Тип 1</option>
|
|
|
<option value="type2">Тип 2</option>
|
|
|
<option value="type3">Тип 3</option>
|
|
|
<option value="type4">Тип 4</option>
|
|
|
- </select>
|
|
|
+ </Field>
|
|
|
</label>
|
|
|
</div>
|
|
|
|
|
|
- <div className="form__input-box">
|
|
|
- <label className="form__input-lable" htmlFor="weight">
|
|
|
- Вес
|
|
|
- <input onChange={e => this.changeHandler(e)} className="form__input" id="weight" type="number" />
|
|
|
- </label>
|
|
|
- </div>
|
|
|
+ <Field name="weight" label="Вес" type="number" component={renderField} />
|
|
|
|
|
|
- <button type="button" onClick={addLoads}>
|
|
|
- Добавить груз
|
|
|
- </button>
|
|
|
-
|
|
|
- {loads.map(el => (
|
|
|
- <div key={el.id} className="form__input-box">
|
|
|
- <label className="form__input-lable" htmlFor="name">
|
|
|
- Груз
|
|
|
- <input
|
|
|
- onChange={e => changeInputValue({ id: el.id, value: e.target.value, loads: true })}
|
|
|
- className="form__input"
|
|
|
- id="name"
|
|
|
- type="text"
|
|
|
- />
|
|
|
- </label>
|
|
|
- </div>
|
|
|
- ))}
|
|
|
+ <FieldArray name="loads" component={renderHobbies} />
|
|
|
|
|
|
<div className="form__button-box">
|
|
|
- <button className="form__submit-button" onClick={addToList}>
|
|
|
- Создать Доставку
|
|
|
+ <button className="form__submit-button">Создать Доставку</button>
|
|
|
+ <button type="button" className="form__submit-button form__submit-button--reset">
|
|
|
+ Очистить форму
|
|
|
</button>
|
|
|
- <button className="form__submit-button form__submit-button--reset">Очистить форму</button>
|
|
|
</div>
|
|
|
- </div>
|
|
|
+ </form>
|
|
|
);
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+export default reduxForm({ form: "LoadForm", validate: ololo })(LoadForm);
|