import type { ComponentCtrl, Props, VNode } from './core'; // === exports ======================================================= export { opt, props, req, widget }; export type { ComponentCtrl, PropDef, PropsDef }; // === types ========================================================= type ComponentFunc
= (p: P) => () => VNode;
type PropDefReq {
validateProps?: (nextProps: P, oldProps: P) => null | Error;
defaults: D;
}
// === constants =====================================================
const symbolProps = Symbol('props');
// === local data ====================================================
const reqDef = Object.freeze({ required: true });
const optDef = Object.freeze({ required: false });
// === exported functions ============================================
function req >(
propsConfig: PropsConfig
) => (fn: (props: Prettify ) => () => VNode) => ComponentFunc ;
function widget(name: string, arg2?: any): any {
if (arguments.length > 1) {
const ret = arg2.bind(null);
Object.defineProperty(ret, 'name', {
value: name
});
return ret;
}
return >(
propsConfig: PropsConfig
) => {
const defaults = propsConfig.defaults || {};
// TODO!!!!
return (fn: Function) => {
return (props: Props) => {
(props.constructor as any).__defaults = defaults;
for (const key of Object.keys(defaults)) {
if (props[key] === undefined) {
props[key] = defaults[key];
}
}
return fn(props);
};
};
};
}
// === utilities =====================================================
function hasOwn(subj: any, propName: string) {
return subj != null && Object.prototype.hasOwnProperty.call(subj, propName);
}