import type { Component, Props, RefObject } from 'js-widgets'; // === exports ======================================================= export { classes, component, createRef, createRefFor, setName }; // === local data ==================================================== const hasOwn = {}.hasOwnProperty; // === exported functions ============================================ function createRef(value?: T): { current: T | null } { return { current: arguments.length === 0 ? null : value! }; } function createRefFor( component: Component<{ ref: RefObject }> ): RefObject { return createRef(); } function classes( ...args: ( | undefined | null | false | string | (undefined | null | false | string)[] | Record )[] ): string { const arr: string[] = []; for (const arg of args) { if (arg) { if (typeof arg === 'string') { arr.push(arg); } else if (Array.isArray(arg)) { for (const s of arg) { if (s) { arr.push(s); } } } else { for (const key in arg) { if (hasOwn.call(arg, key) && (arg as any)[key]) { arr.push(key); } } } } } return arr.join(' '); } function setName(func: Function, name: string) { Object.defineProperty(func, name, { value: name }); } function component

(main: Component

): Component

; function component

( name: string, main: Component

): Component

; function component( name: string ):

(main: Component

) => Component

; function component

( main: Component

): >( defaults: D ) => (main: (props: P & D) => () => JSX.Element) => Component

; function component(arg1: any, arg2?: any): any { if (typeof arg1 === 'function') { return component('', arg1); } else if (arguments.length < 2) { return (arg3: any) => { if (typeof arg3 === 'function') { return component(arg1, arg3); } else { return (arg4: any) => component(arg1, (props) => { const defaults = arg3; (props.constructor as any).__defaults = defaults; for (const key of Object.keys(defaults)) { if (props[key] === undefined) { props[key] = defaults[key]; } } return arg4(props); }); } }; } const ret = arg2.bind(null); setName(ret, arg1); return ret; }