Refactor ComponentOptions: replace events with funcs for event handling and enhance getState method for nested state retrieval
This commit is contained in:
parent
1afae00c45
commit
77229cd4b0
22
src/main.ts
22
src/main.ts
|
@ -12,11 +12,11 @@ interface ComponentOptions {
|
||||||
onAttributeChanged?: (attrName: string, oldValue: string, newValue: string) => void
|
onAttributeChanged?: (attrName: string, oldValue: string, newValue: string) => void
|
||||||
states?: Record<string, any>
|
states?: Record<string, any>
|
||||||
statesListeners?: { [key: string]: (value: any) => void }
|
statesListeners?: { [key: string]: (value: any) => void }
|
||||||
events?: { [key: string]: (event: Event) => void }
|
funcs?: { [key: string]: (...args: any[]) => void }
|
||||||
}
|
}
|
||||||
|
|
||||||
export default (options: ComponentOptions) => {
|
export default (options: ComponentOptions) => {
|
||||||
const { tag, template, style, onMount, onUnmount, onAttributeChanged, states, statesListeners } = options
|
const { tag, template, style, onMount, onUnmount, onAttributeChanged, states, statesListeners, funcs } = options
|
||||||
const componentRegistry = new Map()
|
const componentRegistry = new Map()
|
||||||
componentRegistry.set(tag, options)
|
componentRegistry.set(tag, options)
|
||||||
|
|
||||||
|
@ -1002,8 +1002,22 @@ export default (options: ComponentOptions) => {
|
||||||
this._states[keyPath] = value
|
this._states[keyPath] = value
|
||||||
}
|
}
|
||||||
|
|
||||||
getState(keyPath: string) {
|
getState(keyPath: string): any {
|
||||||
return this._states[keyPath]
|
const parts = keyPath.split('.')
|
||||||
|
let result = this._states
|
||||||
|
for (const part of parts) {
|
||||||
|
if (result === undefined || result === null) {
|
||||||
|
return undefined
|
||||||
|
}
|
||||||
|
result = result[part]
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
// function trigger
|
||||||
|
triggerFunc(eventName: string, ...args: any[]) {
|
||||||
|
if (funcs && funcs[eventName])
|
||||||
|
funcs[eventName].call(this, ...args)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user