refactor: extract withProps mixin to eliminate TypedBase/observedAttributes boilerplate
18 components now use withProps(HTMLElement, PROPS) instead of
repeating TypedBase cast, static get observedAttributes(), and
static { definePropAccessors(...) } in each file.
Adds src/core/with-props.ts — a mixin factory that derives
PropTypesFromTable, defines property accessors, and sets up
observedAttributes from a PROPS table in a single expression.
main
parent
26e110d0b4
commit
80c87468b6
@ -0,0 +1,21 @@
|
|||||||
|
import type { PropDef, PropTypesFromTable } from '../types/props.js';
|
||||||
|
import { definePropAccessors } from './utils.js';
|
||||||
|
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||||
|
type Ctor<T = object> = new (...args: any[]) => T;
|
||||||
|
|
||||||
|
export function withProps<TBase extends Ctor<HTMLElement>, TProps extends Record<string, PropDef>>(
|
||||||
|
Base: TBase,
|
||||||
|
props: TProps,
|
||||||
|
): TBase & Ctor<HTMLElement & PropTypesFromTable<TProps>> {
|
||||||
|
class WithProps extends Base {
|
||||||
|
static {
|
||||||
|
definePropAccessors(WithProps.prototype, props);
|
||||||
|
}
|
||||||
|
static get observedAttributes(): string[] {
|
||||||
|
return Object.values(props).map((s) => s.attr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return WithProps as TBase & Ctor<HTMLElement & PropTypesFromTable<TProps>>;
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue