Skip to content

Commit 9ec4ee6

Browse files
committed
reduce complexity of code organization, fixes
- there's not real need in Form.jsx to be located under 'components' subfolder, and utils.js under 'utils' subfolder. placing them both under 'src' root folder. - fix `bindState` export in 'src/index.js' - update demo's App.jsx to use bindState helper - update tests accordingly
1 parent f26997a commit 9ec4ee6

File tree

9 files changed

+24
-31
lines changed

9 files changed

+24
-31
lines changed

CHANGELOG

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
=== master
22

3+
Reduce complexity of code organization, fixes (akuzko)
4+
35
Add bindState helper method, improve Form props (akuzko)
46

57
Fix rendering flaws in Demo forms, add CHANGELOG (akuzko)

demo/src/App.jsx

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import React, { PureComponent } from 'react';
22
import { Intro, InputPrerequisites } from './components';
33
import * as Forms from './forms';
4+
import { bindState } from '../../src';
45

56
const sections = [
67
'inputs', 'form01', 'form02', 'form03', 'form04',
78
'form05', 'form06', 'form07', 'form08', 'form09', 'form10'
89
].reverse();
910

1011
export default class App extends PureComponent {
11-
state = { forms: {}, section: 'inputs' };
12+
state = { section: 'inputs' };
1213

1314
componentDidMount() {
1415
window.addEventListener('scroll', this.setActiveSection);
@@ -30,17 +31,6 @@ export default class App extends PureComponent {
3031
return this.state.section === section ? 'active' : '';
3132
}
3233

33-
formProps(name) {
34-
const props = this.state.forms[name] || {};
35-
36-
return {
37-
attrs: props.attrs || {},
38-
onChange: (attrs, errors) => this.setState({
39-
forms: { ...this.state.forms, [name]: { attrs, errors } }
40-
})
41-
};
42-
}
43-
4434
saveForm11 = (attrs, form) => {
4535
// simulated AJAX request
4636
return new Promise((resolve, reject) => {
@@ -77,17 +67,17 @@ export default class App extends PureComponent {
7767
<div className="content paper flex-item p-20">
7868
<Intro />
7969
<div id="inputs"><InputPrerequisites /></div>
80-
<div id="form01"><Forms.Form1 {...this.formProps('form1')} /></div>
81-
<div id="form02"><Forms.Form2 {...this.formProps('form2')} /></div>
82-
<div id="form03"><Forms.Form3 {...this.formProps('form3')} /></div>
83-
<div id="form04"><Forms.Form4 {...this.formProps('form4')} /></div>
84-
<div id="form05"><Forms.Form5 {...this.formProps('form5')} /></div>
85-
<div id="form06"><Forms.Form6 {...this.formProps('form6')} /></div>
86-
<div id="form07"><Forms.Form7 {...this.formProps('form7')} /></div>
87-
<div id="form08"><Forms.Form8 {...this.formProps('form8')} /></div>
88-
<div id="form09"><Forms.Form9 {...this.formProps('form9')} /></div>
89-
<div id="form10"><Forms.Form10 {...this.formProps('form10')} /></div>
90-
<div id="form11"><Forms.Form11 {...this.formProps('form11')} onRequestSave={this.saveForm11} /></div>
70+
<div id="form01"><Forms.Form1 {...bindState(this, 'form1')} /></div>
71+
<div id="form02"><Forms.Form2 {...bindState(this, 'form2')} /></div>
72+
<div id="form03"><Forms.Form3 {...bindState(this, 'form3')} /></div>
73+
<div id="form04"><Forms.Form4 {...bindState(this, 'form4')} /></div>
74+
<div id="form05"><Forms.Form5 {...bindState(this, 'form5')} /></div>
75+
<div id="form06"><Forms.Form6 {...bindState(this, 'form6')} /></div>
76+
<div id="form07"><Forms.Form7 {...bindState(this, 'form7')} /></div>
77+
<div id="form08"><Forms.Form8 {...bindState(this, 'form8')} /></div>
78+
<div id="form09"><Forms.Form9 {...bindState(this, 'form9')} /></div>
79+
<div id="form10"><Forms.Form10 {...bindState(this, 'form10')} /></div>
80+
<div id="form11"><Forms.Form11 {...bindState(this, 'form11')} onRequestSave={this.saveForm11} /></div>
9181
</div>
9282
</div>
9383
</div>

demo/src/forms/Form7.jsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ const SOURCE = [['Form7.jsx', `
4242
}
4343
`], ['Page.jsx', `
4444
import React, { Component } from 'react';
45+
import { bindState } from 'react-form-base';
4546
import Form7 from './Form7';
4647
4748
class Page extends Component {

src/components/Form.jsx renamed to src/Form.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React, { PropTypes, PureComponent } from 'react';
22

3-
import { nameToPath, buildFormValidator } from '../utils';
3+
import { nameToPath, buildFormValidator } from './utils';
44
import isPlainObject from 'lodash/isPlainObject';
55
import cloneDeep from 'lodash/cloneDeep';
66
import get from 'lodash/get';

src/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
export default from './components/Form';
2-
export bindState from './utils';
1+
export default from './Form';
2+
export { bindState } from './utils';

src/utils/utils.js renamed to src/utils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import isPlainObject from 'lodash/isPlainObject';
44
export function bindState(component, key = 'form') {
55
return {
66
attrs: (component.state && component.state[key]) || {},
7-
onChange: function(attrs) { return component.setState({ [key]: attrs }) }
7+
onChange: function(attrs) { return component.setState({ [key]: attrs }); }
88
};
99
}
1010

src/utils/index.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

test/components/Form.test.js renamed to test/Form.test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React, { PropTypes, Component } from 'react';
2-
import Form from '../../src';
2+
import Form from '../src';
33
import { mount, shallow } from 'enzyme';
44
import expect, { createSpy } from 'expect';
55
import range from 'lodash/range';
@@ -12,6 +12,7 @@ describe('<Form />', function() {
1212
class Input extends Component {
1313
static propTypes = {
1414
onChange: PropTypes.func,
15+
value: PropTypes.string,
1516
error: PropTypes.string
1617
};
1718

test/utils/utils.test.js renamed to test/utils.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React, { Component } from 'react';
2-
import Form from '../../src/Form';
3-
import { nameToPath, bindState } from '../../src/utils';
2+
import Form from '../src/Form';
3+
import { nameToPath, bindState } from '../src/utils';
44
import { shallow } from 'enzyme';
55
import expect from 'expect';
66

0 commit comments

Comments
 (0)