From 9ece660c0f1a65f223a1713a1c519ca9b4fae216 Mon Sep 17 00:00:00 2001 From: Vinicius Goulart Date: Tue, 30 Mar 2021 20:19:04 +0200 Subject: [PATCH] feat: improve typedefs --- packages/form-js-viewer/src/core/FormCore.js | 37 ++++++++++++++++---- 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/packages/form-js-viewer/src/core/FormCore.js b/packages/form-js-viewer/src/core/FormCore.js index 8d222aa8d..de23db3e9 100644 --- a/packages/form-js-viewer/src/core/FormCore.js +++ b/packages/form-js-viewer/src/core/FormCore.js @@ -17,11 +17,15 @@ import { clone } from '../util'; * @typedef { any } Schema * @typedef { { [x: string]: any } } Data * @typedef { { [x: string]: string[] } } Errors - * @typedef { { [x: string]: any } } Properties + * @typedef { ('readOnly') } Properties + * @typedef { { readOnly?: boolean } } PropertyOptions + * @typedef { ('submit' | 'changed') } Events + * @typedef { { data: Data, schema: Schema, properties?: PropertyOptions } } FormCoreOptions + * @typedef { { data: Data, errors: Errors, schema: Schema, properties: PropertyOptions } } State * - * @typedef { { data: Data, schema: Schema, properties?: Properties } } FormCoreOptions - * - * @typedef { { data: Data, errors: Errors, schema: Schema, properties: Properties } } State + * @callback EventHandler + * @param { { data: Data, errors: Errors} } state + * @returns { void } */ /** @@ -86,6 +90,9 @@ export default class FormCore { }); } + /** + * @returns { { data: Data, errors: Errors } } + */ submit() { const data = this.state.data; @@ -125,8 +132,8 @@ export default class FormCore { } /** - * @param { any } data - * @return { {[x: string]: string[]} } errors + * @param { Data } data + * @return Errors */ validateAll(data) { const errors = Array.from(this.fields.values()).reduce((errors, field) => { @@ -148,6 +155,9 @@ export default class FormCore { return clone(this.state); } + /** + * @param { Partial } state + */ setState(state) { this.state = { ...this.state, @@ -157,20 +167,35 @@ export default class FormCore { this.changed(this.state); } + /** + * @param { State } state + */ changed(state) { this.emitter.emit('changed', clone(state)); } + /** + * @param { Properties } property + * @param { any } value + */ setProperty(property, value) { const properties = set(this.getState().properties, [ property ], value); this.setState({ properties }); } + /** + * @param { Events } event + * @param { EventHandler } callback + */ on(event, callback) { this.emitter.on(event, callback); } + /** + * @param { Events } event + * @param { EventHandler } callback + */ off(event, callback) { this.emitter.off(event, callback); }