From 33624c89de27a6d730008913ffa689958f5610e3 Mon Sep 17 00:00:00 2001 From: Palash Bansal Date: Mon, 11 Dec 2023 04:09:22 +0530 Subject: [PATCH] Add value, lastValue in the onChange args, Add option to add additional options to config when appending in the renderer. --- package.json | 2 +- src/UiConfigMethods.ts | 13 ++++++++----- src/UiConfigRendererBase.ts | 3 ++- src/types.ts | 2 ++ 4 files changed, 13 insertions(+), 7 deletions(-) diff --git a/package.json b/package.json index 7b64b64..beb357a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "uiconfig.js", - "version": "0.0.6", + "version": "0.0.7", "description": "A framework for building user interface layouts with JSON configuration.", "main": "src/index.ts", "module": "dist/index.mjs", diff --git a/src/UiConfigMethods.ts b/src/UiConfigMethods.ts index 4d2e85f..7fe26b7 100644 --- a/src/UiConfigMethods.ts +++ b/src/UiConfigMethods.ts @@ -21,13 +21,15 @@ export class UiConfigMethods { return tar ? tar[key] : undefined } - dispatchOnChangeSync(config: UiObjectConfig, props: {last?: boolean, config?: UiObjectConfig, configPath?: UiObjectConfig[]}, ...args: any[]) { + dispatchOnChangeSync(config: UiObjectConfig, props: {last?: boolean, config?: UiObjectConfig, configPath?: UiObjectConfig[], value?: any, lastValue?: any}, ...args: any[]) { const changeEvent: ChangeEvent = { type: 'change', last: props.last ?? true, config: props.config ?? config, configPath: [config, ...props.configPath || []], target: config, + value: props.value, + lastValue: props.lastValue, } const changeArgs: ChangeArgs = [changeEvent, ...args] if (typeof config.onChange === 'function') config.onChange(...changeArgs) @@ -41,17 +43,18 @@ export class UiConfigMethods { config.parentOnChange?.(...changeArgs) } - async setValue(config: UiObjectConfig, value: T, props: {last?: boolean, config?: UiObjectConfig, configPath?: UiObjectConfig[]}, forceOnChange?: boolean) { + async setValue(config: UiObjectConfig, value: T, props: {last?: boolean, config?: UiObjectConfig, configPath?: UiObjectConfig[], lastValue?: T}, forceOnChange?: boolean) { return this.runAtEvent(config, () => { const [tar, key] = this.getBinding(config) - if (!tar || value === tar[key] || !safeSetProperty(tar, key, value, true, true)) { + const lastValue = props.lastValue ?? tar[key] + if (!tar || value === lastValue || !safeSetProperty(tar, key, value, true, true)) { if (!forceOnChange) return false } - this.dispatchOnChangeSync(config, props) + this.dispatchOnChangeSync(config, {...props, value, lastValue}) return true }) } - async dispatchOnChange(config: UiObjectConfig, props: {last?: boolean, config?: UiObjectConfig, configPath?: UiObjectConfig[]}) { + async dispatchOnChange(config: UiObjectConfig, props: {last?: boolean, config?: UiObjectConfig, configPath?: UiObjectConfig[], value?: any, lastValue?: any}) { return this.runAtEvent(config, () => { this.dispatchOnChangeSync(config, props) }) diff --git a/src/UiConfigRendererBase.ts b/src/UiConfigRendererBase.ts index 92551c3..294c6a7 100644 --- a/src/UiConfigRendererBase.ts +++ b/src/UiConfigRendererBase.ts @@ -87,8 +87,9 @@ export abstract class UiConfigRendererBase extends SimpleEventDis // this._renderUiConfig(uiConfig) // } - appendChild(config?: UiObjectConfig) { + appendChild(config?: UiObjectConfig, params?: UiObjectConfig) { if (!config) return + Object.assign(config, params) this.config.children!.push(config) this.refreshRoot() } diff --git a/src/types.ts b/src/types.ts index 2a39610..f1b1320 100644 --- a/src/types.ts +++ b/src/types.ts @@ -8,6 +8,8 @@ export interface ChangeEvent { last?: boolean, // true if this is the last change event in a chain of changes config?: UiObjectConfig, // the config that triggered the change configPath?: UiObjectConfig[], // list of all configs from target to the one that triggered the change + value?: any, // the new value + lastValue?: any, // the old value } export type ChangeArgs = [ChangeEvent, ...any[]] | never[]