-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathform.js
More file actions
202 lines (178 loc) · 5.91 KB
/
Copy pathform.js
File metadata and controls
202 lines (178 loc) · 5.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import React from 'react'
import makeRequest from '@app-elements/with-request/makeRequest'
import equal from '@app-elements/equal'
let storeRef // Will get populated if we receive `store` via context
const hasProp = Object.prototype.hasOwnProperty
const isReactNative = typeof window !== 'undefined' &&
window.navigator.product === 'ReactNative'
// children can be an array or object in React,
// but always array in Preact.
const compatMap = React.Children
? React.Children.map
: (ls, fn) => Array.prototype.map.call(ls, fn)
// React method to skip textNodes, and Preact fallback.
const compatIsValid = React.isValidElement
? React.isValidElement
: child => child && child.nodeName != null
const getProps = child => child.attributes || child.props || {}
// Is one of the above defined form fields, (or has attribute isFormField)
// and has a `name` prop set. We can't sync state if the component doesn't have
// have a `name` prop set.
const isFormField = child => {
const props = getProps(child)
if (props.native || props.isFormField === false) {
return false
} else if (props.name) {
return true
}
}
// Our actual Form component
export default class Form extends React.Component {
constructor (props, { store }) {
super(props)
storeRef = store
if (!this.props.name) throw new Error('<Form /> Components needs a `name` prop.')
this.state = {
values: this.props.initialData || {},
errors: {},
submitting: false
}
this._fields = {}
}
_updateChildFormFields (children, formName) {
return compatMap(children, child => {
if (!compatIsValid(child)) {
return child
}
const childProps = child.attributes || child.props || {}
if (childProps.isSubmit) {
// if has isSubmit flag, treat as Submit button on ReactNative
child = React.cloneElement(child, { formName, onPress: () => this._onSubmit() })
} else if (isFormField(child)) {
// If one of our nested Form Fields, add syncState prop.
// If not ReactNative, override the onChange event to sync value.
const newProps = {
formName,
text: this.state.values[childProps.name],
value: this.state.values[childProps.name],
syncState: state => this.setState({
values: {
...this.state.values,
[childProps.name]: state.value != null ? state.value : state.text
}
})
}
if (!isReactNative) {
newProps.onChange = ev => this.setState({
values: {
...this.state.values,
[childProps.name]: ev.target.type === 'checkbox' ? ev.target.checked : ev.target.value
}
})
}
child = React.cloneElement(child, newProps)
// Store a reference to our fields, so we can validate them on submit
this._fields[childProps.name] = child
} else if (child.children || childProps.children) {
// Recursively search children for more form fields
child = React.cloneElement(
child,
{ formName },
this._updateChildFormFields(child.children || childProps.children, formName)
)
}
return child
})
}
_onSubmit (ev) {
ev && ev.preventDefault()
if (this.state.submitting) {
return
}
this.setState({ submitting: true })
const fieldNames = Object.keys(this._fields)
const { validations } = this.props
const errors = fieldNames.reduce((errs, name) => {
const comp = this._fields[name]
if (getProps(comp).required && !this.state.values[name]) {
errs[name] = 'Is required.'
}
if (validations && hasProp.call(validations, name)) {
const validationResult = validations[name](this.state.values[name])
if (validationResult) {
errs[name] = validationResult
}
}
return errs
}, {})
const hasError = Object.keys(errors).length > 0
hasError && this.setState({ errors: { ...this.state.errors, ...errors } })
const done = (errors = {}, values) => this.setState({
values: values || this.props.initialData || {},
submitting: false,
errors
})
if (this.props.onSubmit) {
this.props.onSubmit({
hasError,
done,
errors: errors,
data: this.state.values,
state: this.state
})
} else {
if (!hasError) {
const { xhr, promise } = makeRequest({
endpoint: this.props.action,
method: this.props.method,
data: this.state.values,
noAuth: this.props.noAuth || false,
invalidate: this.props.invalidate
})
promise
.then(r => {
this.setState({
values: this.props.initialData || {},
errors: {},
submitting: false
})
this.props.onSuccess && this.props.onSuccess(r)
})
.catch(err => {
this.setState({ submitting: false })
this.props.onFailure && this.props.onFailure({ err, xhr, done })
})
} else {
this.setState({ submitting: false })
storeRef.setState({ notification: { status: 'failure', message: 'Please complete all form fields!' } })
}
}
}
componentDidMount () {
if (!equal(this.state, storeRef.getState()[this.props.name])) {
storeRef.setState({ [this.props.name]: this.state })
}
}
componentDidUpdate () {
if (!equal(this.state, storeRef.getState()[this.props.name])) {
storeRef.setState({ [this.props.name]: this.state })
}
}
render () {
const children = this._updateChildFormFields(
this.children || this.props.children,
this.props.name
)
return isReactNative
? children
: (
<form
id={`Form-${this.props.name}`}
key={this.props.name}
onSubmit={this._onSubmit.bind(this)}
>
{children}
</form>
)
}
}