Comparing version 1.0.2 to 1.0.3
@@ -5,3 +5,3 @@ /*! | ||
* | ||
* Version: 1.0.2 | ||
* Version: 1.0.3 | ||
* | ||
@@ -12,3 +12,3 @@ * Copyright KingSora | Rene Haas. | ||
* Released under the MIT license. | ||
* Date: 17.02.2022 | ||
* Date: 20.02.2022 | ||
*/ | ||
@@ -25,3 +25,5 @@ import React, { isValidElement, cloneElement, createElement, Fragment } from 'react'; | ||
*/ | ||
const renderComponentOrComponentType = (component) => isValidElement(component) ? cloneElement(component) : createElement(component); | ||
const renderComponentOrComponentType = (component) => isValidElement(component) | ||
? cloneElement(component) | ||
: createElement(component); | ||
/** | ||
@@ -77,3 +79,5 @@ * Overwrites the passed props with the passed overwrite function. | ||
finalType, | ||
(props || finalType.defaultProps || {}), | ||
(props || | ||
finalType.defaultProps || | ||
{}), | ||
]; | ||
@@ -83,18 +87,12 @@ }; | ||
/** | ||
* Creates a modified version of the passed base render function | ||
* Creates a modified version of the passed base render function. | ||
* @param renderFunc The base render function of the component. | ||
* @param as The "as" component type. | ||
* @param componentTypeProps The component type and props tuple. | ||
* @param asTypeProps The "as" component type and props tuble. | ||
* @param options The options for the transformation. | ||
* @returns A modified version of the passed base render function. | ||
*/ | ||
/** | ||
* | ||
* @param renderFunc | ||
* @param as | ||
* @param componentProps | ||
* @param asProps | ||
* @param options | ||
* @returns | ||
*/ | ||
const createModifiedRenderFunc = (renderFunc, as, componentProps, asProps, options) => { | ||
const createModifiedRenderFunc = (renderFunc, componentTypeProps, asTypeProps, options) => { | ||
const [componentType, componentProps] = componentTypeProps; | ||
const [asType, asProps] = asTypeProps; | ||
const { strategy, recursive } = options; | ||
@@ -108,10 +106,12 @@ // Note: this function must NOT be an arrow function because the "this" context matters | ||
const validRootElm = isObject(rootElm); | ||
const defaultType = validRootElm ? as : () => React.createElement(Fragment, null, rootElm); | ||
const defaultType = validRootElm ? asType : () => React.createElement(Fragment, null, rootElm); | ||
const { props, type = defaultType } = validRootElm ? rootElm : { props: args[0] }; | ||
if (isFunction(type) && recursive) { | ||
return React.createElement(As, { component: createElement(type, props), as: as, options: options }); | ||
return React.createElement(As, { component: createElement(type, props), as: asType, options: options }); | ||
} | ||
const finalType = getStrategyElement(type, as, strategy); | ||
const finalType = getStrategyElement(type, asType, strategy); | ||
const passedComponentProps = finalType === componentType ? componentProps : {}; | ||
const passedAsProps = finalType === asType ? asProps : {}; | ||
// @ts-ignore | ||
return createElement(finalType, Object.assign(Object.assign({}, (finalType === as ? asProps : componentProps)), props)); | ||
return createElement(finalType, Object.assign(Object.assign(Object.assign({}, passedComponentProps), passedAsProps), props)); | ||
}; | ||
@@ -121,5 +121,5 @@ }; | ||
* Gets the modified component type. | ||
* @param componentType The component type. | ||
* @param asType The as component type. | ||
* @param options The options. | ||
* @param componentTypeProps The component type and props tuple. | ||
* @param asTypeProps The "as" component type and props tuble. | ||
* @param options The options for the transformation. | ||
* @returns The modified component type. | ||
@@ -129,4 +129,4 @@ */ | ||
var _a; | ||
const [componentType, componentProps] = componentTypeProps; | ||
const [asType, asProps] = asTypeProps; | ||
const [componentType] = componentTypeProps; | ||
const [asType] = asTypeProps; | ||
const isClassComponent = (_a = componentType.prototype) === null || _a === void 0 ? void 0 : _a.isReactComponent; | ||
@@ -145,3 +145,3 @@ const isFunctionComponent = isFunction(componentType); | ||
const prototype = ModifiedClass.prototype; | ||
prototype.render = createModifiedRenderFunc(prototype.render, asType, componentProps, asProps, options); | ||
prototype.render = createModifiedRenderFunc(prototype.render, componentTypeProps, asTypeProps, options); | ||
return ModifiedClass; | ||
@@ -151,3 +151,3 @@ } | ||
? // create new component | ||
createModifiedRenderFunc(componentType, asType, componentProps, asProps, options) | ||
createModifiedRenderFunc(componentType, componentTypeProps, asTypeProps, options) | ||
: // choose from the input components depending on strategy | ||
@@ -154,0 +154,0 @@ getStrategyElement(componentType, asType, options.strategy); |
@@ -1,1 +0,1 @@ | ||
import t,{isValidElement as e,cloneElement as r,createElement as n,Fragment as o}from"react";const s=t=>"function"==typeof t,c=t=>!!t&&"object"==typeof t,a=t=>e(t)?r(t):n(t),p=(t,e,r)=>"leave"===r?"string"==typeof t&&e||t:e,i=(t,e)=>{let r,n,o=t;c(t)&&({type:o,props:n,render:r}=t);let s=o||r;return c(s)&&!e&&([s]=i(s,e)),[s,n||s.defaultProps||{}]},u=(e,r,a,i,u)=>{const{strategy:l,recursive:f}=u;return function(...v){var d;const m=null===(d=null==e?void 0:e.apply)||void 0===d?void 0:d.call(e,this,v),g=c(m),b=g?r:()=>t.createElement(o,null,m),{props:j,type:A=b}=g?m:{props:v[0]};if(s(A)&&f)return t.createElement(y,{component:n(A,j),as:r,options:u});const E=p(A,r,l);return n(E,Object.assign(Object.assign({},E===r?i:a),j))}},l=(e,r,n)=>{const{strategy:o="wrap",recursive:c=!0,overwriteProps:l}=n||{};if(!e&&!r)return null;if(!e)return a(r);if(!r)return a(e);const[y,f]=i(e,!1),[v,d]=i(r,!0),[m,g]=((t,e,r)=>{const n=s(t)?t(e,r):null;return Array.isArray(n)?n:[e,r]})(l,f,d),b=((t,e,r)=>{var n;const[o,c]=t,[a,i]=e,l=null===(n=o.prototype)||void 0===n?void 0:n.isReactComponent,y=s(o);if(l){const t=`${o.constructor.name}#As`,e={[t]:class extends o{}}[t],n=e.prototype;return n.render=u(n.render,a,c,i,r),e}return y?u(o,a,c,i,r):p(o,a,r.strategy)})([y,m],[v,g],{strategy:o,recursive:c});return t.createElement(b,Object.assign({},m,g))},y=({component:t,as:e,options:r})=>l(t,e,r);export{y as As,y as default,l as transform}; | ||
import t,{isValidElement as e,cloneElement as r,createElement as n,Fragment as o}from"react";const s=t=>"function"==typeof t,c=t=>!!t&&"object"==typeof t,a=t=>e(t)?r(t):n(t),i=(t,e,r)=>"leave"===r?"string"==typeof t&&e||t:e,p=(t,e)=>{let r,n,o=t;c(t)&&({type:o,props:n,render:r}=t);let s=o||r;return c(s)&&!e&&([s]=p(s,e)),[s,n||s.defaultProps||{}]},u=(e,r,a,p)=>{const[u,l]=r,[f,v]=a,{strategy:d,recursive:g}=p;return function(...r){var a;const m=null===(a=null==e?void 0:e.apply)||void 0===a?void 0:a.call(e,this,r),b=c(m),j=b?f:()=>t.createElement(o,null,m),{props:O,type:A=j}=b?m:{props:r[0]};if(s(A)&&g)return t.createElement(y,{component:n(A,O),as:f,options:p});const E=i(A,f,d),w=E===u?l:{},x=E===f?v:{};return n(E,Object.assign(Object.assign(Object.assign({},w),x),O))}},l=(e,r,n)=>{const{strategy:o="wrap",recursive:c=!0,overwriteProps:l}=n||{};if(!e&&!r)return null;if(!e)return a(r);if(!r)return a(e);const[y,f]=p(e,!1),[v,d]=p(r,!0),[g,m]=((t,e,r)=>{const n=s(t)?t(e,r):null;return Array.isArray(n)?n:[e,r]})(l,f,d),b=((t,e,r)=>{var n;const[o]=t,[c]=e,a=null===(n=o.prototype)||void 0===n?void 0:n.isReactComponent,p=s(o);if(a){const n=`${o.constructor.name}#As`,s={[n]:class extends o{}}[n],c=s.prototype;return c.render=u(c.render,t,e,r),s}return p?u(o,t,e,r):i(o,c,r.strategy)})([y,g],[v,m],{strategy:o,recursive:c});return t.createElement(b,Object.assign({},g,m))},y=({component:t,as:e,options:r})=>l(t,e,r);export{y as As,y as default,l as transform}; |
@@ -5,3 +5,3 @@ /*! | ||
* | ||
* Version: 1.0.2 | ||
* Version: 1.0.3 | ||
* | ||
@@ -12,3 +12,3 @@ * Copyright KingSora | Rene Haas. | ||
* Released under the MIT license. | ||
* Date: 17.02.2022 | ||
* Date: 20.02.2022 | ||
*/ | ||
@@ -74,3 +74,5 @@ (function (global, factory) { | ||
var renderComponentOrComponentType = function (component) { | ||
return React.isValidElement(component) ? React.cloneElement(component) : React.createElement(component); | ||
return React.isValidElement(component) | ||
? React.cloneElement(component) | ||
: React.createElement(component); | ||
}; | ||
@@ -128,3 +130,5 @@ /** | ||
finalType, | ||
(props || finalType.defaultProps || {}), | ||
(props || | ||
finalType.defaultProps || | ||
{}), | ||
]; | ||
@@ -134,18 +138,12 @@ }; | ||
/** | ||
* Creates a modified version of the passed base render function | ||
* Creates a modified version of the passed base render function. | ||
* @param renderFunc The base render function of the component. | ||
* @param as The "as" component type. | ||
* @param componentTypeProps The component type and props tuple. | ||
* @param asTypeProps The "as" component type and props tuble. | ||
* @param options The options for the transformation. | ||
* @returns A modified version of the passed base render function. | ||
*/ | ||
/** | ||
* | ||
* @param renderFunc | ||
* @param as | ||
* @param componentProps | ||
* @param asProps | ||
* @param options | ||
* @returns | ||
*/ | ||
var createModifiedRenderFunc = function (renderFunc, as, componentProps, asProps, options) { | ||
var createModifiedRenderFunc = function (renderFunc, componentTypeProps, asTypeProps, options) { | ||
var componentType = componentTypeProps[0], componentProps = componentTypeProps[1]; | ||
var asType = asTypeProps[0], asProps = asTypeProps[1]; | ||
var strategy = options.strategy, recursive = options.recursive; | ||
@@ -163,10 +161,12 @@ // Note: this function must NOT be an arrow function because the "this" context matters | ||
var validRootElm = isObject(rootElm); | ||
var defaultType = validRootElm ? as : function () { return React__default["default"].createElement(React.Fragment, null, rootElm); }; | ||
var defaultType = validRootElm ? asType : function () { return React__default["default"].createElement(React.Fragment, null, rootElm); }; | ||
var _b = validRootElm ? rootElm : { props: args[0] }, props = _b.props, _c = _b.type, type = _c === void 0 ? defaultType : _c; | ||
if (isFunction(type) && recursive) { | ||
return React__default["default"].createElement(As, { component: React.createElement(type, props), as: as, options: options }); | ||
return React__default["default"].createElement(As, { component: React.createElement(type, props), as: asType, options: options }); | ||
} | ||
var finalType = getStrategyElement(type, as, strategy); | ||
var finalType = getStrategyElement(type, asType, strategy); | ||
var passedComponentProps = finalType === componentType ? componentProps : {}; | ||
var passedAsProps = finalType === asType ? asProps : {}; | ||
// @ts-ignore | ||
return React.createElement(finalType, __assign(__assign({}, (finalType === as ? asProps : componentProps)), props)); | ||
return React.createElement(finalType, __assign(__assign(__assign({}, passedComponentProps), passedAsProps), props)); | ||
}; | ||
@@ -176,5 +176,5 @@ }; | ||
* Gets the modified component type. | ||
* @param componentType The component type. | ||
* @param asType The as component type. | ||
* @param options The options. | ||
* @param componentTypeProps The component type and props tuple. | ||
* @param asTypeProps The "as" component type and props tuble. | ||
* @param options The options for the transformation. | ||
* @returns The modified component type. | ||
@@ -185,4 +185,4 @@ */ | ||
var _b; | ||
var componentType = componentTypeProps[0], componentProps = componentTypeProps[1]; | ||
var asType = asTypeProps[0], asProps = asTypeProps[1]; | ||
var componentType = componentTypeProps[0]; | ||
var asType = asTypeProps[0]; | ||
var isClassComponent = (_b = componentType.prototype) === null || _b === void 0 ? void 0 : _b.isReactComponent; | ||
@@ -206,3 +206,3 @@ var isFunctionComponent = isFunction(componentType); | ||
var prototype = ModifiedClass.prototype; | ||
prototype.render = createModifiedRenderFunc(prototype.render, asType, componentProps, asProps, options); | ||
prototype.render = createModifiedRenderFunc(prototype.render, componentTypeProps, asTypeProps, options); | ||
return ModifiedClass; | ||
@@ -212,3 +212,3 @@ } | ||
? // create new component | ||
createModifiedRenderFunc(componentType, asType, componentProps, asProps, options) | ||
createModifiedRenderFunc(componentType, componentTypeProps, asTypeProps, options) | ||
: // choose from the input components depending on strategy | ||
@@ -215,0 +215,0 @@ getStrategyElement(componentType, asType, options.strategy); |
@@ -1,1 +0,1 @@ | ||
!function(e){"function"==typeof define&&define.amd?define(e):e()}((function(){"use strict";var e,t;e=this,t=function(e,t){function n(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var r=n(t),o=function(e,t){return o=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var n in t)t.hasOwnProperty(n)&&(e[n]=t[n])},o(e,t)},u=function(){return u=Object.assign||function(e){for(var t,n=1,r=arguments.length;n<r;n++)for(var o in t=arguments[n])Object.prototype.hasOwnProperty.call(t,o)&&(e[o]=t[o]);return e},u.apply(this,arguments)},i=function(e){return"function"==typeof e},a=function(e){return!!e&&"object"==typeof e},f=function(e){return t.isValidElement(e)?t.cloneElement(e):t.createElement(e)},c=function(e,t,n){return"leave"===n?"string"==typeof e&&t||e:t},p=function(e,t){var n,r,o,u=e;a(e)&&(u=(n=e).type,o=n.props,r=n.render);var i=u||r;return a(i)&&!t&&(i=p(i,t)[0]),[i,o||i.defaultProps||{}]},l=function(e,n,o,f,p){var l=p.strategy,s=p.recursive;return function(){for(var d,y=[],m=0;m<arguments.length;m++)y[m]=arguments[m];var h=null===(d=null==e?void 0:e.apply)||void 0===d?void 0:d.call(e,this,y),g=a(h),b=g?n:function(){return r.default.createElement(t.Fragment,null,h)},_=g?h:{props:y[0]},j=_.props,E=_.type,O=void 0===E?b:E;if(i(O)&&s)return r.default.createElement(v,{component:t.createElement(O,j),as:n,options:p});var P=c(O,n,l);return t.createElement(P,u(u({},P===n?f:o),j))}},s=function(e,t,n){var r,u,a=e[0],f=e[1],p=t[0],s=t[1],d=null===(u=a.prototype)||void 0===u?void 0:u.isReactComponent,v=i(a);if(d){var y="".concat(a.constructor.name,"#As"),m=(r={},r[y]=function(e){function t(){return null!==e&&e.apply(this,arguments)||this}return function(e,t){function n(){this.constructor=e}o(e,t),e.prototype=null===t?Object.create(t):(n.prototype=t.prototype,new n)}(t,e),t}(a),r)[y],h=m.prototype;return h.render=l(h.render,p,f,s,n),m}return v?l(a,p,f,s,n):c(a,p,n.strategy)},d=function(e,t,n){var o=n||{},a=o.strategy,c=void 0===a?"wrap":a,l=o.recursive,d=void 0===l||l,v=o.overwriteProps;if(!e&&!t)return null;if(!e)return f(t);if(!t)return f(e);var y=p(e,!1),m=y[0],h=y[1],g=p(t,!0),b=g[0],_=function(e,t,n){var r=i(e)?e(t,n):null;return Array.isArray(r)?r:[t,n]}(v,h,g[1]),j=_[0],E=_[1],O=s([m,j],[b,E],{strategy:c,recursive:d});return r.default.createElement(O,u({},j,E))},v=function(e){var t=e.component,n=e.as,r=e.options;return d(t,n,r)};e.As=v,e.default=v,e.transform=d,Object.defineProperty(e,"__esModule",{value:!0})},"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("react")):"function"==typeof define&&define.amd?define(["exports","react"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self)["react-as"]={},e.React)})); | ||
!function(e){"function"==typeof define&&define.amd?define(e):e()}((function(){"use strict";var e,t;e=this,t=function(e,t){function n(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var r=n(t),o=function(e,t){return o=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var n in t)t.hasOwnProperty(n)&&(e[n]=t[n])},o(e,t)},u=function(){return u=Object.assign||function(e){for(var t,n=1,r=arguments.length;n<r;n++)for(var o in t=arguments[n])Object.prototype.hasOwnProperty.call(t,o)&&(e[o]=t[o]);return e},u.apply(this,arguments)},i=function(e){return"function"==typeof e},a=function(e){return!!e&&"object"==typeof e},f=function(e){return t.isValidElement(e)?t.cloneElement(e):t.createElement(e)},c=function(e,t,n){return"leave"===n?"string"==typeof e&&t||e:t},p=function(e,t){var n,r,o,u=e;a(e)&&(u=(n=e).type,o=n.props,r=n.render);var i=u||r;return a(i)&&!t&&(i=p(i,t)[0]),[i,o||i.defaultProps||{}]},l=function(e,n,o,f){var p=n[0],l=n[1],s=o[0],d=o[1],y=f.strategy,m=f.recursive;return function(){for(var n,o=[],h=0;h<arguments.length;h++)o[h]=arguments[h];var g=null===(n=null==e?void 0:e.apply)||void 0===n?void 0:n.call(e,this,o),b=a(g),_=b?s:function(){return r.default.createElement(t.Fragment,null,g)},j=b?g:{props:o[0]},E=j.props,O=j.type,P=void 0===O?_:O;if(i(P)&&m)return r.default.createElement(v,{component:t.createElement(P,E),as:s,options:f});var w=c(P,s,y),A=w===p?l:{},x=w===s?d:{};return t.createElement(w,u(u(u({},A),x),E))}},s=function(e,t,n){var r,u,a=e[0],f=t[0],p=null===(u=a.prototype)||void 0===u?void 0:u.isReactComponent,s=i(a);if(p){var d="".concat(a.constructor.name,"#As"),v=(r={},r[d]=function(e){function t(){return null!==e&&e.apply(this,arguments)||this}return function(e,t){function n(){this.constructor=e}o(e,t),e.prototype=null===t?Object.create(t):(n.prototype=t.prototype,new n)}(t,e),t}(a),r)[d],y=v.prototype;return y.render=l(y.render,e,t,n),v}return s?l(a,e,t,n):c(a,f,n.strategy)},d=function(e,t,n){var o=n||{},a=o.strategy,c=void 0===a?"wrap":a,l=o.recursive,d=void 0===l||l,v=o.overwriteProps;if(!e&&!t)return null;if(!e)return f(t);if(!t)return f(e);var y=p(e,!1),m=y[0],h=y[1],g=p(t,!0),b=g[0],_=function(e,t,n){var r=i(e)?e(t,n):null;return Array.isArray(r)?r:[t,n]}(v,h,g[1]),j=_[0],E=_[1],O=s([m,j],[b,E],{strategy:c,recursive:d});return r.default.createElement(O,u({},j,E))},v=function(e){var t=e.component,n=e.as,r=e.options;return d(t,n,r)};e.As=v,e.default=v,e.transform=d,Object.defineProperty(e,"__esModule",{value:!0})},"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("react")):"function"==typeof define&&define.amd?define(["exports","react"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self)["react-as"]={},e.React)})); |
@@ -5,3 +5,2 @@ import { ReactElement } from 'react'; | ||
export declare const isFunction: (value: unknown) => value is (...args: any) => any; | ||
export declare const isSymbol: (value: unknown) => value is symbol; | ||
export declare const isObject: (value: unknown) => value is object; | ||
@@ -8,0 +7,0 @@ /** |
{ | ||
"name": "react-as", | ||
"author": "KingSora | Rene Haas", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "Render react components as other components.", | ||
@@ -6,0 +6,0 @@ "license": "MIT", |
@@ -20,3 +20,3 @@ <h1 align="center">react-as</h1> | ||
The solution was to introduce an `component`, `tag` or `as` property to the components which had the appearance but not the functionality. In the `Link` and `Button` example the `Button` would receive such an property. Many popular libraries are doing like this: [MUI](https://mui.com/api/button/#props), [Ant Design](https://ant.design/components/button/#API), [react-bootstrap](https://react-bootstrap.github.io/components/buttons/#button-props). The problem is that if more components should receive the functionality of the `Link` component, the property needs to be re-implement over and over again for each component. And this is where `react-as` comes in! **Not only can `react-as` replicate the current solution, its also possible to give the `Link` component the `as` prop instead of the `Button`.** | ||
The solution was to introduce an `component`, `tag` or `as` property to the components which had the appearance but not the functionality. In the `Link` and `Button` example the `Button` would receive such an property. Many popular libraries are doing like this: [MUI](https://mui.com/api/button/#props), [Ant Design](https://ant.design/components/button/#API), [react-bootstrap](https://react-bootstrap.github.io/components/buttons/#button-props). The problem is that if more components should receive the functionality of the `Link` component, the property needs to be re-implement over and over again for each component. And this is where `react-as` comes in! **Not only can `react-as` replicate the current solution without re-implementing it over and over again, its also possible to give the `Link` component the `as` prop instead of the `Button`.** | ||
@@ -52,5 +52,5 @@ ## Usage | ||
Simply put the library traverses the root elements of your component until it encounters a intrinsic element, because this means this is the deepest root element. The it creates a new custom render function where it changes the type of this element to your desired `as` component type. | ||
Simply put the library traverses the root elements of your components until it encounters a intrinsic element, because this means this is the element which most likely should be transformed. It creates a new custom render function where it changes the type of this element to your desired `as` component type. | ||
If the deepest root element is not an intrinsic type but something else, the transformation is considered unsuccessful and you can decide with the `strategy` option how to handle the case. | ||
If during the traversal no intrinsic element type could be found, the transformation is considered unsuccessful and you can decide with the `strategy` option how to handle the case. | ||
@@ -57,0 +57,0 @@ ## API |
@@ -66,4 +66,6 @@ import { | ||
*/ | ||
export type InputComponentProps<C extends InputComponent> = C extends JSXElementConstructor<any> | keyof JSX.IntrinsicElements | ||
export type InputComponentProps<C extends InputComponent> = C extends | ||
| JSXElementConstructor<any> | ||
| keyof JSX.IntrinsicElements | ||
? ComponentProps<C> | ||
: ComponentPropsDefault; |
@@ -1,7 +0,20 @@ | ||
import { isValidElement, cloneElement, createElement, ExoticComponent, ForwardRefRenderFunction, ReactElement } from 'react'; | ||
import { Strategy, ComponentType, ValidComponentType, InputComponent, InputComponentProps, OverwriteProps } from './types'; | ||
import { | ||
isValidElement, | ||
cloneElement, | ||
createElement, | ||
ExoticComponent, | ||
ForwardRefRenderFunction, | ||
ReactElement, | ||
} from 'react'; | ||
import { | ||
Strategy, | ||
ComponentType, | ||
ValidComponentType, | ||
InputComponent, | ||
InputComponentProps, | ||
OverwriteProps, | ||
} from './types'; | ||
export const isString = (value: unknown): value is string => typeof value === 'string'; | ||
export const isFunction = (value: unknown): value is (...args: any) => any => typeof value === 'function'; | ||
export const isSymbol = (value: unknown): value is symbol => typeof value === 'symbol'; | ||
export const isObject = (value: unknown): value is object => !!value && typeof value === 'object'; | ||
@@ -15,3 +28,5 @@ | ||
export const renderComponentOrComponentType = (component?: InputComponent | null): ReactElement => | ||
isValidElement(component) ? cloneElement(component) : createElement(component as Exclude<InputComponent, JSX.Element>); | ||
isValidElement(component) | ||
? cloneElement(component) | ||
: createElement(component as Exclude<InputComponent, JSX.Element>); | ||
@@ -41,3 +56,7 @@ /** | ||
*/ | ||
export const getStrategyElement = (componentType: ComponentType, asType: ComponentType, strategy: Strategy): ComponentType => { | ||
export const getStrategyElement = ( | ||
componentType: ComponentType, | ||
asType: ComponentType, | ||
strategy: Strategy | ||
): ComponentType => { | ||
switch (strategy) { | ||
@@ -83,4 +102,6 @@ case 'leave': { | ||
finalType as B extends false ? ValidComponentType : ComponentType, | ||
(props || (finalType as Exclude<ComponentType, string | ExoticComponent | ReactElement>).defaultProps || {}) as InputComponentProps<C>, | ||
(props || | ||
(finalType as Exclude<ComponentType, string | ExoticComponent | ReactElement>).defaultProps || | ||
{}) as InputComponentProps<C>, | ||
]; | ||
}; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
74589
842