Skip to content

Commit 2806a05

Browse files
committed
* core: enhance HElement class with detailed documentation for lifecycle methods and props handling
1 parent 3fbcd9e commit 2806a05

1 file changed

Lines changed: 88 additions & 0 deletions

File tree

lib/core/src/react/components/h-element.ts

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ export class HElement<P extends HElementProps, S = object> extends Component<P,
3939
*/
4040
declare ['constructor']: typeof HElement<P, S>;
4141

42+
/**
43+
* The unique global ID of the component instance, used to locate the rendered DOM element.
44+
*/
4245
protected _gid = nextGid();
4346

4447
constructor(props: P) {
@@ -47,10 +50,20 @@ export class HElement<P extends HElementProps, S = object> extends Component<P,
4750
this.state = this.getDefaultState(props);
4851
}
4952

53+
/**
54+
* Get the unique global ID of the component instance.
55+
*
56+
* @returns The unique global ID.
57+
*/
5058
get gid() {
5159
return this._gid;
5260
}
5361

62+
/**
63+
* Get the DOM element.
64+
*
65+
* @returns The DOM element.
66+
*/
5467
get element() {
5568
return document.querySelector(`[z-gid-${this._gid}]`);
5669
}
@@ -69,10 +82,22 @@ export class HElement<P extends HElementProps, S = object> extends Component<P,
6982
return this.constructor.NAME;
7083
}
7184

85+
/**
86+
* Get the default state.
87+
*
88+
* @param props The props.
89+
* @returns The default state.
90+
*/
7291
getDefaultState(_props?: RenderableProps<P>): S {
7392
return {} as S;
7493
}
7594

95+
/**
96+
* Reset the state.
97+
*
98+
* @param props The props.
99+
* @param init Whether to initialize the state.
100+
*/
76101
resetState(props?: RenderableProps<P>, init?: boolean) {
77102
const defaultState = this.getDefaultState(props);
78103
if (init) {
@@ -124,6 +149,13 @@ export class HElement<P extends HElementProps, S = object> extends Component<P,
124149
?? `{i18n:${key}}`;
125150
}
126151

152+
/**
153+
* Change the component state.
154+
*
155+
* @param state The new state.
156+
* @param callback The callback to call after the state is changed.
157+
* @returns The promise of the state.
158+
*/
127159
changeState(state: Partial<S> | ((prevState: Readonly<S>) => Partial<S>), callback?: () => void): Promise<S> {
128160
return new Promise<S>((resolve) => {
129161
this.setState(state, () => {
@@ -133,6 +165,13 @@ export class HElement<P extends HElementProps, S = object> extends Component<P,
133165
});
134166
}
135167

168+
/**
169+
* Execute a command.
170+
*
171+
* @param context The command context.
172+
* @param params The command parameters.
173+
* @returns The result of the command.
174+
*/
136175
executeCommand(context: CommandContext | string, params: unknown[] = []) {
137176
const {onCommand, commands} = this.props;
138177
let result;
@@ -153,10 +192,25 @@ export class HElement<P extends HElementProps, S = object> extends Component<P,
153192
return result;
154193
}
155194

195+
/**
196+
* Get the class name(s) applied to the root element.
197+
* Subclasses can override this to inject component-specific classes.
198+
*
199+
* @param props The current renderable props.
200+
* @returns The class name(s) to merge onto the root element.
201+
*/
156202
protected _getClassName(props: RenderableProps<P>): ClassNameLike {
157203
return props.className;
158204
}
159205

206+
/**
207+
* Resolve the final attributes/props passed to the underlying DOM element or component.
208+
* Filters out framework-only props, allows whitelisted custom props, forwards `data-*`,
209+
* `z-*`, `zui-*` and event handler attributes, and stamps the unique `z-gid-*` marker.
210+
*
211+
* @param props The current renderable props.
212+
* @returns The merged props object to spread onto the rendered element.
213+
*/
160214
protected _getProps(props: RenderableProps<P>): Record<string, unknown> {
161215
const {className, attrs, props: componentProps, data, forwardRef, children, component, style, class: classNameAlt, commands, onCommand, ...others} = props;
162216
const customProps = new Set((this.constructor as typeof HElement).customProps);
@@ -171,19 +225,53 @@ export class HElement<P extends HElementProps, S = object> extends Component<P,
171225
return {ref: forwardRef, className: classes(this._getClassName(props), classNameAlt), style, [`z-gid-${this._gid}`]: '', ...other, ...attrs, ...componentProps};
172226
}
173227

228+
/**
229+
* Resolve the actual component/tag to render.
230+
* Accepts a tag name, a registered component name, or a component reference;
231+
* defaults to a `div` when not specified.
232+
*
233+
* @param props The current renderable props.
234+
* @returns The resolved component type or intrinsic HTML tag name.
235+
*/
174236
protected _getComponent(props: RenderableProps<P>): ComponentType | keyof JSX.IntrinsicElements {
175237
const {component = 'div'} = props;
176238
return (typeof component === 'string' ? getReactComponent(component as string) : component) || component;
177239
}
178240

241+
/**
242+
* Resolve the children rendered inside the root element.
243+
* Subclasses can override this to compose additional content around `props.children`.
244+
*
245+
* @param props The current renderable props.
246+
* @returns The children to render.
247+
*/
179248
protected _getChildren(props: RenderableProps<P>): ComponentChildren {
180249
return props.children;
181250
}
182251

252+
/**
253+
* Hook invoked before the render pipeline starts.
254+
* Subclasses can override this to transform or replace the incoming props;
255+
* returning `void` keeps the original props unchanged.
256+
*
257+
* @param props The current renderable props.
258+
* @returns The (optionally) transformed props, or `void` to keep them as-is.
259+
*/
183260
protected _beforeRender(props: RenderableProps<P>): RenderableProps<P> | void {
184261
return props;
185262
}
186263

264+
/**
265+
* Final hook invoked right before `h()` is called.
266+
* Subclasses can override this to swap the component, mutate the resolved props,
267+
* or wrap the children. Returning `undefined` keeps the inputs unchanged.
268+
*
269+
* @param component The resolved component type or tag name.
270+
* @param componentProps The resolved props to pass to the component.
271+
* @param children The resolved children.
272+
* @param _props The original renderable props (unused by default).
273+
* @returns A `[component, props, children]` tuple, or `undefined` to keep inputs.
274+
*/
187275
protected _onRender(component: ComponentType | keyof JSX.IntrinsicElements, componentProps: Record<string, unknown>, children: ComponentChildren, _props: RenderableProps<P>): [component: ComponentType | keyof JSX.IntrinsicElements, componentProps: Record<string, unknown>, children: ComponentChildren] | undefined {
188276
return [component, componentProps, children];
189277
}

0 commit comments

Comments
 (0)