Skip to content

Commit

Permalink
Add value, lastValue in the onChange args, Add option to add addition…
Browse files Browse the repository at this point in the history
…al options to config when appending in the renderer.
  • Loading branch information
repalash committed Dec 10, 2023
1 parent 3b79c17 commit 33624c8
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
13 changes: 8 additions & 5 deletions src/UiConfigMethods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -41,17 +43,18 @@ export class UiConfigMethods {
config.parentOnChange?.(...changeArgs)
}

async setValue<T>(config: UiObjectConfig<T>, value: T, props: {last?: boolean, config?: UiObjectConfig, configPath?: UiObjectConfig[]}, forceOnChange?: boolean) {
async setValue<T>(config: UiObjectConfig<T>, 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<T>(config: UiObjectConfig<T>, props: {last?: boolean, config?: UiObjectConfig, configPath?: UiObjectConfig[]}) {
async dispatchOnChange<T>(config: UiObjectConfig<T>, props: {last?: boolean, config?: UiObjectConfig, configPath?: UiObjectConfig[], value?: any, lastValue?: any}) {
return this.runAtEvent(config, () => {
this.dispatchOnChangeSync(config, props)
})
Expand Down
3 changes: 2 additions & 1 deletion src/UiConfigRendererBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,9 @@ export abstract class UiConfigRendererBase<TUiNode = any> 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()
}
Expand Down
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[]

Expand Down

0 comments on commit 33624c8

Please sign in to comment.