mirror of
https://github.com/flarum/framework.git
synced 2024-11-28 11:34:36 +08:00
Add typings for class component state attribute (#2995)
* Add `state` typings to class components
This commit is contained in:
parent
cc29cf3e10
commit
7f2e6543ed
|
@ -29,7 +29,7 @@ export interface ComponentAttrs extends Mithril.Attributes {}
|
||||||
*
|
*
|
||||||
* @see https://mithril.js.org/components.html
|
* @see https://mithril.js.org/components.html
|
||||||
*/
|
*/
|
||||||
export default abstract class Component<T extends ComponentAttrs = ComponentAttrs> implements Mithril.ClassComponent<T> {
|
export default abstract class Component<Attrs extends ComponentAttrs = ComponentAttrs, State = undefined> implements Mithril.ClassComponent<Attrs> {
|
||||||
/**
|
/**
|
||||||
* The root DOM element for the component.
|
* The root DOM element for the component.
|
||||||
*/
|
*/
|
||||||
|
@ -40,48 +40,61 @@ export default abstract class Component<T extends ComponentAttrs = ComponentAttr
|
||||||
*
|
*
|
||||||
* @see https://mithril.js.org/components.html#passing-data-to-components
|
* @see https://mithril.js.org/components.html#passing-data-to-components
|
||||||
*/
|
*/
|
||||||
protected attrs!: T;
|
protected attrs!: Attrs;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class component state that is persisted between redraws.
|
||||||
|
*
|
||||||
|
* Updating this will **not** automatically trigger a redraw, unlike
|
||||||
|
* other frameworks.
|
||||||
|
*
|
||||||
|
* This is different to Vnode state, which is always an instance of your
|
||||||
|
* class component.
|
||||||
|
*
|
||||||
|
* This is `undefined` by default.
|
||||||
|
*/
|
||||||
|
protected state!: State;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @inheritdoc
|
* @inheritdoc
|
||||||
*/
|
*/
|
||||||
abstract view(vnode: Mithril.Vnode<T, this>): Mithril.Children;
|
abstract view(vnode: Mithril.Vnode<Attrs, this>): Mithril.Children;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @inheritdoc
|
* @inheritdoc
|
||||||
*/
|
*/
|
||||||
oninit(vnode: Mithril.Vnode<T, this>) {
|
oninit(vnode: Mithril.Vnode<Attrs, this>) {
|
||||||
this.setAttrs(vnode.attrs);
|
this.setAttrs(vnode.attrs);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @inheritdoc
|
* @inheritdoc
|
||||||
*/
|
*/
|
||||||
oncreate(vnode: Mithril.VnodeDOM<T, this>) {
|
oncreate(vnode: Mithril.VnodeDOM<Attrs, this>) {
|
||||||
this.element = vnode.dom;
|
this.element = vnode.dom;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @inheritdoc
|
* @inheritdoc
|
||||||
*/
|
*/
|
||||||
onbeforeupdate(vnode: Mithril.VnodeDOM<T, this>) {
|
onbeforeupdate(vnode: Mithril.VnodeDOM<Attrs, this>) {
|
||||||
this.setAttrs(vnode.attrs);
|
this.setAttrs(vnode.attrs);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @inheritdoc
|
* @inheritdoc
|
||||||
*/
|
*/
|
||||||
onupdate(vnode: Mithril.VnodeDOM<T, this>) {}
|
onupdate(vnode: Mithril.VnodeDOM<Attrs, this>) {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @inheritdoc
|
* @inheritdoc
|
||||||
*/
|
*/
|
||||||
onbeforeremove(vnode: Mithril.VnodeDOM<T, this>) {}
|
onbeforeremove(vnode: Mithril.VnodeDOM<Attrs, this>) {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @inheritdoc
|
* @inheritdoc
|
||||||
*/
|
*/
|
||||||
onremove(vnode: Mithril.VnodeDOM<T, this>) {}
|
onremove(vnode: Mithril.VnodeDOM<Attrs, this>) {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a jQuery object for this component's element. If you pass in a
|
* Returns a jQuery object for this component's element. If you pass in a
|
||||||
|
@ -118,7 +131,7 @@ export default abstract class Component<T extends ComponentAttrs = ComponentAttr
|
||||||
* Saves a reference to the vnode attrs after running them through initAttrs,
|
* Saves a reference to the vnode attrs after running them through initAttrs,
|
||||||
* and checking for common issues.
|
* and checking for common issues.
|
||||||
*/
|
*/
|
||||||
private setAttrs(attrs: T = {} as T): void {
|
private setAttrs(attrs: Attrs = {} as Attrs): void {
|
||||||
(this.constructor as typeof Component).initAttrs(attrs);
|
(this.constructor as typeof Component).initAttrs(attrs);
|
||||||
|
|
||||||
if (attrs) {
|
if (attrs) {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user