Skip to content

Commit 46649dd

Browse files
committed
fixes
1 parent fbcc4c4 commit 46649dd

File tree

9 files changed

+3443
-3427
lines changed

9 files changed

+3443
-3427
lines changed

package-lock.json

Lines changed: 3404 additions & 3409 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-formular",
3-
"version": "0.1.0",
3+
"version": "0.1.1",
44
"description": "This libraray is an experimental approach to bind forms and its inputs and editors together using the new React Context API. It aims to be fully customizable and composable. It´s only a set of Higher-Order-Components. Because of the decoupled nature, Middlewares makes it easy to build custom Validations, Security Guards and other data interceptors. ",
55
"author": "theluk",
66
"license": "MIT",
@@ -20,9 +20,8 @@
2020
"build-storybook": "build-storybook"
2121
},
2222
"peerDependencies": {
23-
"prop-types": "^15.5.4",
24-
"react": "^15.0.0 || ^16.0.0",
25-
"react-dom": "^15.0.0 || ^16.0.0"
23+
"react": "^16.3.0",
24+
"react-dom": "^16.3.0"
2625
},
2726
"devDependencies": {
2827
"@storybook/addon-actions": "^3.4.6",

src/components/Debounce.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export default class Debounce extends React.Component {
66
timeout = null
77

88
static propTypes = {
9-
children: PropTypes.children,
9+
children: PropTypes.node,
1010
wait: PropTypes.number
1111
}
1212

src/components/Middleware.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class Middleware extends Component {
1212
// calling it means passing the data up.
1313
update: PropTypes.func.isRequired,
1414
data: PropTypes.shape(),
15-
children: PropTypes.children
15+
children: PropTypes.node
1616
}
1717

1818
state = {

src/context.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React from 'react'
22

33
const { Provider: DataProvider, Consumer: DataConsumer } = React.createContext({
44
update: null,
5-
data: null
5+
data: {}
66
})
77
const { Provider: ErrorProvider, Consumer: ErrorConsumer } = React.createContext({
88
errors: {}

src/form.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ import PropTypes from 'prop-types'
33
import { DataProvider, getDisplayName, formAware } from './index'
44

55
function provideFormular(WrappedComponent) {
6+
if (WrappedComponent === undefined) {
7+
WrappedComponent = 'form'
8+
}
9+
610
class ProvideFormular extends Component {
711
static displayName = `WithForm(${getDisplayName(WrappedComponent)})`
812

@@ -22,7 +26,10 @@ function provideFormular(WrappedComponent) {
2226
...this.state.data,
2327
...newData
2428
}
25-
onChange(newData)
29+
if (typeof onChange === 'function') {
30+
onChange(newData)
31+
}
32+
2633
this.setState({ data: newData })
2734
}
2835

@@ -43,12 +50,13 @@ function provideFormular(WrappedComponent) {
4350

4451
return formAware(
4552
({ data, update, field, ...props }) => {
53+
const isNested = typeof update === 'function' && field !== undefined;
4654
if (field === undefined && typeof update === 'function') {
4755
throw new Error('Nested form usage without field specification')
4856
}
4957
props = {
5058
initialData: field !== undefined && data.hasOwnProperty(field) ? data[field] : null,
51-
onChange: newData => update(field, newData),
59+
onChange: isNested && (newData => update({ [field]: newData })),
5260
...props
5361
}
5462

src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ export * from './withError'
44
export * from './form'
55
export * from './common'
66
export { default as Middleware } from './components/Middleware'
7+
export { default as Debounce } from './components/Debounce'

src/test.js

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,23 @@ import React from 'react'
22
import { provideFormular, withFormField, Middleware } from './index'
33
import { create } from 'react-test-renderer'
44

5-
const Input = withFormField((props) => <input onChange={(e) => props.update(e.target.value)} value={props.value} />)
6-
const Form = provideFormular(({children, ...props}) => (
7-
<div {...props}>
8-
{children}
9-
</div>
10-
))
5+
const Input = withFormField(e => e.target.value)('input')
6+
const Form = provideFormular()
7+
8+
test('provideFormular', () => {
9+
const Form = provideFormular()
10+
const s = create(<Form />)
11+
expect(s.toJSON().type).toBe('form')
12+
13+
const DivForm = provideFormular('div')
14+
const d = create(<DivForm />)
15+
expect(d.toJSON().type).toBe('div')
16+
})
17+
18+
test('withFormField', () => {
19+
const rendered = create(<Form initialData={{}}><Input field="value" type="text" /></Form>)
20+
rendered.root.findByType('input').props.onChange({target: {value: 'foo'}})
21+
})
1122

1223
test('basic middleware', () => {
1324
const onChange = jest.fn()

src/withForm.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import React from 'react'
22
import { DataConsumer } from './context'
33
import { getDisplayName } from './common'
44

5+
const identity = value => value;
6+
57
/**
68
* formAware creates a function which
79
* returns a Component that calls the
@@ -12,7 +14,7 @@ function formAware(handler, displayName) {
1214
function FormAware(props) {
1315
return (
1416
<DataConsumer>
15-
{ ({ data, update }) => handler({ ...props, data, update }) }
17+
{ ({ data, update }) => handler({ data, update, ...props }) }
1618
</DataConsumer>
1719
)
1820
}
@@ -27,13 +29,13 @@ function formAware(handler, displayName) {
2729
* the form props (update and data) and value of
2830
* the specified field
2931
*/
30-
function withFormField(WrappedComponent) {
32+
const withFormField = (valueSelector = identity) => WrappedComponent => {
3133
return formAware(
3234
({ data, update, field, ...props }) => (
3335
<WrappedComponent
3436
{...props}
3537
value={data[field]}
36-
update={value => update({ [field]: value })}
38+
onChange={value => update({ [field]: valueSelector(value) })}
3739
/>
3840
),
3941
`WithFormField(${getDisplayName(WrappedComponent)})`

0 commit comments

Comments
 (0)