diff --git a/packages/form-js-viewer/src/core/FormCore.js b/packages/form-js-viewer/src/core/FormCore.js index 8d222aa8d..45430ac4e 100644 --- a/packages/form-js-viewer/src/core/FormCore.js +++ b/packages/form-js-viewer/src/core/FormCore.js @@ -17,11 +17,11 @@ import { clone } from '../util'; * @typedef { any } Schema * @typedef { { [x: string]: any } } Data * @typedef { { [x: string]: string[] } } Errors - * @typedef { { [x: string]: any } } Properties - * - * @typedef { { data: Data, schema: Schema, properties?: Properties } } FormCoreOptions - * - * @typedef { { data: Data, errors: Errors, schema: Schema, properties: Properties } } State + * @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 */ /** @@ -86,6 +86,9 @@ export default class FormCore { }); } + /** + * @returns { { data: Data, errors: Errors } } + */ submit() { const data = this.state.data; @@ -125,8 +128,8 @@ export default class FormCore { } /** - * @param { any } data - * @return { {[x: string]: string[]} } errors + * @param { Data } data + * @returns Errors */ validateAll(data) { const errors = Array.from(this.fields.values()).reduce((errors, field) => { @@ -148,6 +151,9 @@ export default class FormCore { return clone(this.state); } + /** + * @param { Partial } state + */ setState(state) { this.state = { ...this.state, @@ -157,20 +163,33 @@ 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 }); } + /** + * @type { (event: Events, callback: (state: any) => void) => void } + */ on(event, callback) { this.emitter.on(event, callback); } + /** + * @type { (event: Events, callback: (state: any) => void) => void } + */ off(event, callback) { this.emitter.off(event, callback); }