UNPKG

@ffsm/factory

Version:

A powerful component factory for React that simplifies creation of reusable components with built-in support for composition patterns, conditional rendering, prop management, and TypeScript integration.

96 lines (95 loc) 3.56 kB
var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { if (ar || !(i in from)) { if (!ar) ar = Array.prototype.slice.call(from, 0, i); ar[i] = from[i]; } } return to.concat(ar || Array.prototype.slice.call(from)); }; import { clsx as defaultClsx } from './clsx'; /** * Creates a shallow copy of props object or returns empty object if undefined * @internal */ export function cloneProps(props) { return (props ? __assign({}, props) : {}); } /** * Retrieves and removes a property from an object * @internal */ export function getAndDeleteProp(props, key, defaultValue) { if (props && key in props) { var value = props[key]; delete props[key]; return value; } return defaultValue; } /** * Extracts and processes component props from init config and user props * @internal */ export function extractProps(init, props) { var clonedInit = cloneProps(init); var clonedProps = cloneProps(props); if ('displayName' in clonedInit) { delete clonedInit.displayName; } var initClsx = getAndDeleteProp(clonedInit, 'clsx', defaultClsx); var asSlot = getAndDeleteProp(clonedInit, 'asSlot'); var outlet = getAndDeleteProp(clonedInit, 'children'); var initClassName = getAndDeleteProp(clonedInit, 'className'); var initStyle = getAndDeleteProp(clonedInit, 'style'); var Component = getAndDeleteProp(clonedProps, 'as', 'div'); var clsx = getAndDeleteProp(clonedProps, 'clsx', initClsx); var propClassName = getAndDeleteProp(clonedProps, 'className'); var propStyle = getAndDeleteProp(clonedProps, 'style'); var children = getAndDeleteProp(clonedProps, 'children'); var merged = Object.assign({}, clonedInit, clonedProps); var solvedClassName = typeof initClassName === 'function' ? initClassName(merged) : initClassName; var solvedStyle = typeof initStyle === 'function' ? initStyle(merged) : initStyle; var className = clsx(solvedClassName, propClassName); var style = solvedStyle || propStyle ? Object.assign({}, solvedStyle, propStyle) : undefined; return { Component: Component, initProps: cloneProps(init), asSlot: asSlot, outlet: outlet, children: children, merged: __assign(__assign({}, merged), { className: className, style: style }), }; } /** * Filters props based on provided options * @internal */ export function filterProps(props, options) { if (options === void 0) { options = {}; } var unionProps = __spreadArray([], (options.excludeProps || []), true).filter(function (v, i, s) { return s.indexOf(v) === i; }); var shouldForwardProps = options.shouldForwardProp || (function (key) { return !unionProps.includes(key); }); return Object.entries(props).reduce(function (acc, _a) { var _b; var k = _a[0], v = _a[1]; if (shouldForwardProps(k)) { acc = Object.assign(acc, (_b = {}, _b[k] = v, _b)); } return acc; }, {}); }