kettanaito / react-advanced-form

Functional reactive forms. Multi-layer validation, custom styling, field grouping, reactive props, and much more.
https://redd.gitbook.io/react-advanced-form
MIT License
217 stars 24 forks source link

Form: Move out field handlers into subscription functions #365

Open kettanaito opened 5 years ago

kettanaito commented 5 years ago

Why?

Field handlers that exist as Form methods now do not need to access the form (except of to pass the form reference as the form argument). They can be isolated into own functions.

// Form.jsx
constructor() {
+ fromEvent('fieldChange').subscribe(handleFieldChange(this.eventEmitter, this))
}

- handleFieldChange() {...}
- handleFieldFocus() {...}
- handleFieldBlur() {...}

Subscription example:.

// utils/subscriptions/handleFieldChange.js
const handleFieldChange = (eventEmitter, form) = (eventPayload) => {
  const nextFieldState = foo(...)
  eventEmitter.emit('applyStatePatch', ..., ...)
}