Commit 9937a88b authored by Remy Huet's avatar Remy Huet

Default values

parent 4ebd4c27
Pipeline #31928 passed with stage
in 55 seconds
......@@ -22,7 +22,7 @@ function CategoryEdit({
confirmButton={<Button primary onClick={() => (dispatch(submit('category')))}> OK </Button>}
header={<Header as="h1">Éditer</Header>}
content={
<CategoryForm onSubmit={handleFormSubmit} />
<CategoryForm onSubmit={handleFormSubmit} defaultValues={{ name, parentId }} />
}
/>
);
......@@ -30,7 +30,10 @@ function CategoryEdit({
CategoryEdit.propTypes = {
id: PropTypes.number.isRequired,
name: PropTypes.string.isRequired,
parentId: PropTypes.number.isRequired,
dispatch: PropTypes.func.isRequired,
callback: PropTypes.func.isRequired,
};
export default connect()(CategoryEdit);
import React from 'react';
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import { Field, reduxForm } from 'redux-form';
......@@ -8,27 +8,35 @@ import {
import Dropdown from './CustomDropdown';
function CategoryForm({ handleSubmit, categories }) {
const parentOptions = categories.map(({ name, id }) => ({
key: id,
value: id,
text: name,
}));
return (
<Container>
<Segment>
<Form onSubmit={handleSubmit}>
<Form.Field>
<Field name="name" component={Input} placeholder="Nom" />
</Form.Field>
<Form.Field>
<Field name="parentId" component={Dropdown} search selection clearable options={parentOptions} />
</Form.Field>
</Form>
</Segment>
</Container>
);
class CategoryForm extends Component {
componentDidMount() {
const { initialize, defaultValues } = this.props;
initialize(defaultValues);
}
render() {
const { categories, handleSubmit, defaultValues } = this.props;
const parentOptions = categories.map(({ name, id }) => ({
key: id,
value: id,
text: name,
}));
return (
<Container>
<Segment>
<Form onSubmit={handleSubmit}>
<Form.Field>
<Field name="name" component={Input} placeholder="Nom" />
</Form.Field>
<Form.Field>
<Field name="parentId" defaultValue={defaultValues.parentId} component={Dropdown} search selection clearable options={parentOptions} />
</Form.Field>
</Form>
</Segment>
</Container>
);
}
}
CategoryForm.propTypes = {
......@@ -37,6 +45,18 @@ CategoryForm.propTypes = {
name: PropTypes.string.isRequired,
id: PropTypes.number.isRequired,
})).isRequired,
defaultValues: PropTypes.shape({
name: PropTypes.string,
parentId: PropTypes.number,
}),
initialize: PropTypes.func.isRequired,
};
CategoryForm.defaultProps = {
defaultValues: {
name: null,
parentId: null,
},
};
const form = reduxForm({
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment