@semcore/core 1.11.8 → 1.11.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +18 -0
- package/README.md +6 -4
- package/lib/cjs/Component.js.map +1 -1
- package/lib/cjs/enhancement/Children.js.map +1 -1
- package/lib/cjs/enhancement/Root.js +1 -1
- package/lib/cjs/enhancement/Root.js.map +1 -1
- package/lib/cjs/enhancement/bindHandler.js.map +1 -1
- package/lib/cjs/enhancement/dataName.js.map +1 -1
- package/lib/cjs/enhancement/enhance.js.map +1 -1
- package/lib/cjs/enhancement/functionDefaultProps.js.map +1 -1
- package/lib/cjs/enhancement/hoistProps.js.map +1 -1
- package/lib/cjs/enhancement/inheritedName.js.map +1 -1
- package/lib/cjs/enhancement/staticChildren.js.map +1 -1
- package/lib/cjs/enhancement/style.js.map +1 -1
- package/lib/cjs/enhancement/uncontrolledProps.js.map +1 -1
- package/lib/cjs/index.js +4 -4
- package/lib/cjs/index.js.map +1 -1
- package/lib/cjs/register.js.map +1 -1
- package/lib/cjs/styled/index.js.map +1 -1
- package/lib/cjs/styled/sstyled.js +4 -2
- package/lib/cjs/styled/sstyled.js.map +1 -1
- package/lib/es6/Component.js.map +1 -1
- package/lib/es6/enhancement/Children.js.map +1 -1
- package/lib/es6/enhancement/Root.js +1 -1
- package/lib/es6/enhancement/Root.js.map +1 -1
- package/lib/es6/enhancement/bindHandler.js.map +1 -1
- package/lib/es6/enhancement/dataName.js.map +1 -1
- package/lib/es6/enhancement/enhance.js.map +1 -1
- package/lib/es6/enhancement/functionDefaultProps.js.map +1 -1
- package/lib/es6/enhancement/hoistProps.js.map +1 -1
- package/lib/es6/enhancement/inheritedName.js.map +1 -1
- package/lib/es6/enhancement/staticChildren.js.map +1 -1
- package/lib/es6/enhancement/style.js.map +1 -1
- package/lib/es6/enhancement/uncontrolledProps.js.map +1 -1
- package/lib/es6/index.js +4 -4
- package/lib/es6/index.js.map +1 -1
- package/lib/es6/register.js.map +1 -1
- package/lib/es6/styled/index.js.map +1 -1
- package/lib/es6/styled/sstyled.js +4 -2
- package/lib/es6/styled/sstyled.js.map +1 -1
- package/package.json +2 -2
- package/src/Component.tsx +0 -108
- package/src/enhancement/Children.tsx +0 -104
- package/src/enhancement/Root.tsx +0 -78
- package/src/enhancement/bindHandler.tsx +0 -45
- package/src/enhancement/dataName.tsx +0 -12
- package/src/enhancement/enhance.tsx +0 -12
- package/src/enhancement/functionDefaultProps.tsx +0 -29
- package/src/enhancement/hoistProps.tsx +0 -88
- package/src/enhancement/inheritedName.tsx +0 -18
- package/src/enhancement/staticChildren.tsx +0 -128
- package/src/enhancement/style.tsx +0 -47
- package/src/enhancement/uncontrolledProps.tsx +0 -134
- package/src/index.tsx +0 -381
- package/src/register.tsx +0 -18
- package/src/styled/index.tsx +0 -9
- package/src/styled/sstyled.tsx +0 -150
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
import { useContext } from 'react';
|
|
2
|
-
// @ts-ignore
|
|
3
|
-
import { sstyled } from '../styled';
|
|
4
|
-
import { STATIC_COMPONENT } from './staticChildren';
|
|
5
|
-
|
|
6
|
-
export const STYLES_CONTEXT = Symbol('STYLES_CONTEXT');
|
|
7
|
-
const STYLES_SELF = Symbol('STYLES_SELF');
|
|
8
|
-
|
|
9
|
-
function Enhancement(childComponents, Context) {
|
|
10
|
-
return {
|
|
11
|
-
condition: function (Component) {
|
|
12
|
-
return Boolean(Component.style || Component[STATIC_COMPONENT]);
|
|
13
|
-
},
|
|
14
|
-
init: function (props, WrapperComponent) {
|
|
15
|
-
if (WrapperComponent.style) {
|
|
16
|
-
this[STYLES_SELF] = sstyled.merge(WrapperComponent.style, props._styles);
|
|
17
|
-
}
|
|
18
|
-
},
|
|
19
|
-
context: function (context) {
|
|
20
|
-
// Optimization: if has no children there is no need to create new context (by lsroman)
|
|
21
|
-
if (!this[STYLES_SELF] || !Object.keys(childComponents).length) {
|
|
22
|
-
return context;
|
|
23
|
-
}
|
|
24
|
-
return {
|
|
25
|
-
...context,
|
|
26
|
-
[STYLES_CONTEXT]: this[STYLES_SELF],
|
|
27
|
-
};
|
|
28
|
-
},
|
|
29
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
30
|
-
asProps: function ({ _styles, styles, ...props }) {
|
|
31
|
-
return {
|
|
32
|
-
...props,
|
|
33
|
-
styles: styles || this[STYLES_SELF],
|
|
34
|
-
};
|
|
35
|
-
},
|
|
36
|
-
wrapperProps: function ({ styles, ...props }) {
|
|
37
|
-
const context = useContext(Context);
|
|
38
|
-
return {
|
|
39
|
-
...props,
|
|
40
|
-
_styles: styles,
|
|
41
|
-
styles: context[STYLES_CONTEXT],
|
|
42
|
-
};
|
|
43
|
-
},
|
|
44
|
-
};
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
export default Enhancement;
|
|
@@ -1,134 +0,0 @@
|
|
|
1
|
-
// @ts-ignore
|
|
2
|
-
import { callAllEventHandlers } from '@semcore/utils/lib/assignProps';
|
|
3
|
-
// @ts-ignore
|
|
4
|
-
import capitalizeFirstLetter from '@semcore/utils/lib/capitalizeFirstLetter';
|
|
5
|
-
|
|
6
|
-
function assign(target, source) {
|
|
7
|
-
return Object.defineProperties(
|
|
8
|
-
target,
|
|
9
|
-
Object.keys(source).reduce((descriptors, key) => {
|
|
10
|
-
descriptors[key] = Object.getOwnPropertyDescriptor(source, key);
|
|
11
|
-
return descriptors;
|
|
12
|
-
}, {}),
|
|
13
|
-
);
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
function defaultProp(prop) {
|
|
17
|
-
return 'default' + capitalizeFirstLetter(prop);
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
function handlerProp(prop) {
|
|
21
|
-
if (prop === 'value') prop = '';
|
|
22
|
-
if (prop === 'checked') prop = '';
|
|
23
|
-
return `on${capitalizeFirstLetter(prop)}Change`;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
function isControlled(propValue) {
|
|
27
|
-
return propValue !== undefined;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
function uncontrolledProp(self, propName, propValue, propDefaultValue, propHandler, chainHandler) {
|
|
31
|
-
function getValue() {
|
|
32
|
-
return isControlled(propValue)
|
|
33
|
-
? propValue
|
|
34
|
-
: propName in self.state
|
|
35
|
-
? self.state[propName]
|
|
36
|
-
: propDefaultValue;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
return [
|
|
40
|
-
getValue(),
|
|
41
|
-
(value, ...args) => {
|
|
42
|
-
const oldValue = getValue();
|
|
43
|
-
if (oldValue === value) return false;
|
|
44
|
-
if (!isControlled(propValue)) self.setState({ [propName]: value });
|
|
45
|
-
return callAllEventHandlers(propHandler, ...chainHandler)(value, ...args);
|
|
46
|
-
},
|
|
47
|
-
];
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
function uncontrolledUniversal(props, config, uncontrolledProp) {
|
|
51
|
-
const handlerProps = {};
|
|
52
|
-
const uncontrolledProps = Object.keys(config).reduce((result, propName) => {
|
|
53
|
-
const handlerName = handlerProp(propName);
|
|
54
|
-
const defaultPropName = defaultProp(propName);
|
|
55
|
-
const {
|
|
56
|
-
[defaultPropName]: propDefaultValue,
|
|
57
|
-
[propName]: propValue,
|
|
58
|
-
[handlerName]: propHandler,
|
|
59
|
-
...other
|
|
60
|
-
} = result;
|
|
61
|
-
|
|
62
|
-
if (propDefaultValue === undefined) {
|
|
63
|
-
// eslint-disable-next-line no-console
|
|
64
|
-
console.warn(defaultPropName, 'must be installed in "defaultProps"');
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
// if (typeof propDefaultValue === 'function' && propDefaultValue.defaultInit) {
|
|
68
|
-
// propDefaultValue = propDefaultValue(result);
|
|
69
|
-
// }
|
|
70
|
-
|
|
71
|
-
let setter = config[propName];
|
|
72
|
-
let chainHandler = [];
|
|
73
|
-
|
|
74
|
-
if (Array.isArray(setter)) [setter, ...chainHandler] = setter;
|
|
75
|
-
setter = setter || ((v) => v);
|
|
76
|
-
|
|
77
|
-
const [value, handler] = uncontrolledProp(
|
|
78
|
-
propName,
|
|
79
|
-
propValue,
|
|
80
|
-
propDefaultValue,
|
|
81
|
-
propHandler,
|
|
82
|
-
chainHandler,
|
|
83
|
-
);
|
|
84
|
-
// TODO: need to warn about recursion (by lsroman)
|
|
85
|
-
handlerProps[propName] = handler;
|
|
86
|
-
|
|
87
|
-
return {
|
|
88
|
-
...other,
|
|
89
|
-
[propName]: value,
|
|
90
|
-
[handlerName]: (eventOrValue, ...args) => {
|
|
91
|
-
const result = [setter(eventOrValue, ...args), ...args];
|
|
92
|
-
if (eventOrValue && eventOrValue.target) {
|
|
93
|
-
result.push(eventOrValue);
|
|
94
|
-
}
|
|
95
|
-
return handler(...result);
|
|
96
|
-
},
|
|
97
|
-
};
|
|
98
|
-
}, props);
|
|
99
|
-
return [uncontrolledProps, handlerProps];
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
function uncontrolled(self, props, config) {
|
|
103
|
-
return uncontrolledUniversal(props, config, uncontrolledProp.bind(undefined, self));
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
function Enhancement() {
|
|
107
|
-
return {
|
|
108
|
-
condition: function (Component) {
|
|
109
|
-
return Boolean(Component.prototype.uncontrolledProps);
|
|
110
|
-
},
|
|
111
|
-
init: function () {
|
|
112
|
-
this.state = this.state || {};
|
|
113
|
-
assign(this, {
|
|
114
|
-
get handlers() {
|
|
115
|
-
const [, uncontrolledHandlers] = uncontrolled(this, this.props, this.uncontrolledProps());
|
|
116
|
-
return uncontrolledHandlers;
|
|
117
|
-
},
|
|
118
|
-
});
|
|
119
|
-
},
|
|
120
|
-
context: function (context) {
|
|
121
|
-
return {
|
|
122
|
-
...context,
|
|
123
|
-
handlers: this.handlers,
|
|
124
|
-
};
|
|
125
|
-
},
|
|
126
|
-
asProps: function (props) {
|
|
127
|
-
// TODO: need to omit unneccessary props (by lsroman)
|
|
128
|
-
const [uncontrolledProps] = uncontrolled(this, props, this.uncontrolledProps());
|
|
129
|
-
return uncontrolledProps;
|
|
130
|
-
},
|
|
131
|
-
};
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
export default Enhancement;
|
package/src/index.tsx
DELETED
|
@@ -1,381 +0,0 @@
|
|
|
1
|
-
import React, { ForwardRefExoticComponent, RefAttributes, useRef } from 'react';
|
|
2
|
-
import hoistNonReactStatics from 'hoist-non-react-statics';
|
|
3
|
-
// @ts-ignore
|
|
4
|
-
import { useForkRef } from '@semcore/utils/lib/ref';
|
|
5
|
-
// @ts-ignore
|
|
6
|
-
import useEnhancedEffect from '@semcore/utils/lib/use/useEnhancedEffect';
|
|
7
|
-
// @ts-ignore
|
|
8
|
-
import _assignProps from '@semcore/utils/lib/assignProps';
|
|
9
|
-
|
|
10
|
-
import { Component, PropsWithRenderFnChildren } from './Component';
|
|
11
|
-
import register from './register';
|
|
12
|
-
|
|
13
|
-
import childrenEnhancement, { CHILDREN_COMPONENT } from './enhancement/Children';
|
|
14
|
-
import rootEnhancement from './enhancement/Root';
|
|
15
|
-
import uncontrolledPropsEnhancement from './enhancement/uncontrolledProps';
|
|
16
|
-
import functionDefaultPropsEnhancement from './enhancement/functionDefaultProps';
|
|
17
|
-
import staticChildrenEnhancement, { STATIC_COMPONENT } from './enhancement/staticChildren';
|
|
18
|
-
import inheritedNameEnhancement, { INHERITED_NAME } from './enhancement/inheritedName';
|
|
19
|
-
import hoistPropsEnhancement from './enhancement/hoistProps';
|
|
20
|
-
import dataNameEnhancement from './enhancement/dataName';
|
|
21
|
-
import enhanceEnhancement from './enhancement/enhance';
|
|
22
|
-
import styleEnhancement from './enhancement/style';
|
|
23
|
-
import bindHandlerEnhancement from './enhancement/bindHandler';
|
|
24
|
-
|
|
25
|
-
const CORE_COMPONENT = Symbol('CORE_COMPONENT');
|
|
26
|
-
const CORE_INSTANCE = Symbol('CORE_INSTANCE');
|
|
27
|
-
const CORE_AS_PROPS = Symbol('CORE_AS_PROPS');
|
|
28
|
-
const CORE_INIT = Symbol('CORE_INIT');
|
|
29
|
-
const CONTEXT_COMPONENT = Symbol('CONTEXT_COMPONENT');
|
|
30
|
-
const CREATE_COMPONENT = Symbol('CREATE_COMPONENT');
|
|
31
|
-
const PARENT_COMPONENTS = Symbol('PARENT_COMPONENTS');
|
|
32
|
-
|
|
33
|
-
function isEmptyObject(obj) {
|
|
34
|
-
return (
|
|
35
|
-
Object.getOwnPropertyNames(obj).length === 0 &&
|
|
36
|
-
Object.getOwnPropertySymbols && // For IE 11
|
|
37
|
-
Object.getOwnPropertySymbols(obj).length === 0 &&
|
|
38
|
-
Object.getPrototypeOf(obj) === Object.prototype
|
|
39
|
-
);
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
function createGetField(enhancements, Component, isFunction) {
|
|
43
|
-
return function getField(key) {
|
|
44
|
-
return enhancements
|
|
45
|
-
.filter((enhancement) => {
|
|
46
|
-
if (!enhancement.condition) {
|
|
47
|
-
return true;
|
|
48
|
-
}
|
|
49
|
-
return enhancement.condition(Component, isFunction);
|
|
50
|
-
})
|
|
51
|
-
.reduce((acc, item) => {
|
|
52
|
-
if (item[key]) {
|
|
53
|
-
acc.push(item[key]);
|
|
54
|
-
}
|
|
55
|
-
return acc;
|
|
56
|
-
}, []);
|
|
57
|
-
};
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
function createForwardWrapper(Component, wrapperProps, statics, isFunction) {
|
|
61
|
-
// @ts-ignore
|
|
62
|
-
const WrapperComponent = React.forwardRef(function ({ forwardRef = null, ...other }, ref) {
|
|
63
|
-
// WRAPPER PROPS
|
|
64
|
-
const { ref: enhancementRef = null, ...props } = wrapperProps.reduce(
|
|
65
|
-
(acc, enhancement) => enhancement(acc, WrapperComponent, isFunction),
|
|
66
|
-
other,
|
|
67
|
-
);
|
|
68
|
-
// @ts-ignore
|
|
69
|
-
return <Component {...props} forwardRef={useForkRef(enhancementRef, ref, forwardRef)} />;
|
|
70
|
-
});
|
|
71
|
-
|
|
72
|
-
hoistNonReactStatics(WrapperComponent, Component);
|
|
73
|
-
|
|
74
|
-
WrapperComponent.displayName = Component.displayName;
|
|
75
|
-
// TODO: defaultProps is only empty
|
|
76
|
-
WrapperComponent.defaultProps = Component.defaultProps;
|
|
77
|
-
// STATIC
|
|
78
|
-
statics.forEach((enhancement) =>
|
|
79
|
-
Object.assign(WrapperComponent, enhancement(WrapperComponent, isFunction)),
|
|
80
|
-
);
|
|
81
|
-
|
|
82
|
-
return WrapperComponent;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
function wrapClass(OriginComponent, enhancements, Context) {
|
|
86
|
-
const getField = createGetField(enhancements, OriginComponent, false);
|
|
87
|
-
|
|
88
|
-
const inits = getField('init');
|
|
89
|
-
const props = getField('asProps');
|
|
90
|
-
const renders = getField('render');
|
|
91
|
-
const statics = getField('static');
|
|
92
|
-
const wrapperProps = getField('wrapperProps');
|
|
93
|
-
const contexts = getField('context');
|
|
94
|
-
|
|
95
|
-
class Component extends OriginComponent {
|
|
96
|
-
[CORE_AS_PROPS] = null;
|
|
97
|
-
[CORE_INSTANCE] = WrapperComponent;
|
|
98
|
-
[CORE_INIT] = false;
|
|
99
|
-
|
|
100
|
-
constructor(props, context) {
|
|
101
|
-
super(props, context);
|
|
102
|
-
// INITS
|
|
103
|
-
inits.forEach((enhancement) => enhancement.call(this, props, WrapperComponent, false));
|
|
104
|
-
this[CORE_INIT] = true;
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
get asProps() {
|
|
108
|
-
// TODO if not production
|
|
109
|
-
if (!this[CORE_INIT]) {
|
|
110
|
-
throw new Error('Can not call asProps in constructor');
|
|
111
|
-
}
|
|
112
|
-
if (!this[CORE_AS_PROPS]) {
|
|
113
|
-
// PROPS
|
|
114
|
-
this[CORE_AS_PROPS] = props.reduce(
|
|
115
|
-
(acc, enhancement) => enhancement.call(this, acc, WrapperComponent, false),
|
|
116
|
-
this.props,
|
|
117
|
-
);
|
|
118
|
-
}
|
|
119
|
-
return this[CORE_AS_PROPS];
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
setContext() {
|
|
123
|
-
const contextProps = super.setContext ? super.setContext() : {};
|
|
124
|
-
// CONTEXT
|
|
125
|
-
return contexts.reduce(
|
|
126
|
-
(acc, enhancement) => enhancement.call(this, acc, WrapperComponent, false),
|
|
127
|
-
contextProps,
|
|
128
|
-
);
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
render() {
|
|
132
|
-
this[CORE_AS_PROPS] = null;
|
|
133
|
-
// TODO if not production
|
|
134
|
-
if (!super.render) {
|
|
135
|
-
throw new Error('Component `render` method is not defined');
|
|
136
|
-
}
|
|
137
|
-
const asProps = this.asProps;
|
|
138
|
-
const ctx = this.setContext();
|
|
139
|
-
// RENDER
|
|
140
|
-
const render = renders.reduce(
|
|
141
|
-
(acc, enhancement) => enhancement.call(this, acc, asProps, WrapperComponent, false),
|
|
142
|
-
super.render(),
|
|
143
|
-
);
|
|
144
|
-
if (!WrapperComponent[STATIC_COMPONENT] && !isEmptyObject(ctx)) {
|
|
145
|
-
return <Context.Provider value={{ ...asProps, ...ctx }}>{render}</Context.Provider>;
|
|
146
|
-
} else {
|
|
147
|
-
return render;
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
const WrapperComponent = createForwardWrapper(Component, wrapperProps, statics, false);
|
|
153
|
-
return WrapperComponent;
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
function wrapFunction(OriginComponent, enhancements, Context) {
|
|
157
|
-
const getField = createGetField(enhancements, OriginComponent, true);
|
|
158
|
-
|
|
159
|
-
const inits = getField('init');
|
|
160
|
-
const props = getField('asProps');
|
|
161
|
-
const renders = getField('render');
|
|
162
|
-
const statics = getField('static');
|
|
163
|
-
const wrapperProps = getField('wrapperProps');
|
|
164
|
-
const contexts = getField('context');
|
|
165
|
-
|
|
166
|
-
const Component = React.memo(function FunctionMemoComponent(other) {
|
|
167
|
-
const selfRef = useRef({});
|
|
168
|
-
const firstRender = useRef(true);
|
|
169
|
-
useEnhancedEffect(() => {
|
|
170
|
-
firstRender.current = false;
|
|
171
|
-
}, []);
|
|
172
|
-
if (firstRender.current) {
|
|
173
|
-
// INITS
|
|
174
|
-
inits.forEach((enhancement) =>
|
|
175
|
-
enhancement.call(selfRef.current, other, WrapperComponent, true),
|
|
176
|
-
);
|
|
177
|
-
}
|
|
178
|
-
// PROPS
|
|
179
|
-
const asProps = props.reduce(
|
|
180
|
-
(acc, enhancement) => enhancement.call(selfRef.current, acc, WrapperComponent, true),
|
|
181
|
-
other,
|
|
182
|
-
);
|
|
183
|
-
// CONTEXT
|
|
184
|
-
const ctx = contexts.reduce(
|
|
185
|
-
(acc, enhancement) => enhancement.call(selfRef.current, acc, WrapperComponent, true),
|
|
186
|
-
{},
|
|
187
|
-
);
|
|
188
|
-
|
|
189
|
-
// RENDER
|
|
190
|
-
const render = renders.reduce(
|
|
191
|
-
(acc, enhancement) => enhancement.call(selfRef.current, acc, asProps, WrapperComponent, true),
|
|
192
|
-
<OriginComponent {...asProps} />,
|
|
193
|
-
);
|
|
194
|
-
|
|
195
|
-
if (!WrapperComponent[STATIC_COMPONENT] && !isEmptyObject(ctx)) {
|
|
196
|
-
return <Context.Provider value={{ ...asProps, ...ctx }}>{render}</Context.Provider>;
|
|
197
|
-
} else {
|
|
198
|
-
return render;
|
|
199
|
-
}
|
|
200
|
-
});
|
|
201
|
-
Object.assign(Component, OriginComponent);
|
|
202
|
-
const WrapperComponent = createForwardWrapper(Component, wrapperProps, statics, true);
|
|
203
|
-
return WrapperComponent;
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
function wrapCore(OriginComponent, enhancements, Context) {
|
|
207
|
-
const Component = function ({ Root }) {
|
|
208
|
-
return <Root render={OriginComponent} />;
|
|
209
|
-
};
|
|
210
|
-
hoistNonReactStatics(Component, OriginComponent);
|
|
211
|
-
Component.displayName = OriginComponent.displayName;
|
|
212
|
-
Component.defaultProps = OriginComponent.defaultProps;
|
|
213
|
-
return wrapFunction(Component, enhancements, Context);
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
function createComposeComponent(OriginComponent, Context, enhancements): any {
|
|
217
|
-
if (
|
|
218
|
-
React.PureComponent.isPrototypeOf(OriginComponent) ||
|
|
219
|
-
React.Component.isPrototypeOf(OriginComponent)
|
|
220
|
-
) {
|
|
221
|
-
if (OriginComponent.prototype instanceof Component) {
|
|
222
|
-
return wrapClass(OriginComponent, enhancements, Context);
|
|
223
|
-
} else {
|
|
224
|
-
throw new Error('Must inherit from our component');
|
|
225
|
-
}
|
|
226
|
-
} else if (typeof OriginComponent === 'function') {
|
|
227
|
-
return wrapFunction(OriginComponent, enhancements, Context);
|
|
228
|
-
} else if (OriginComponent[CORE_COMPONENT]) {
|
|
229
|
-
return wrapCore(OriginComponent, enhancements, Context);
|
|
230
|
-
} else {
|
|
231
|
-
throw new Error('Must be a React component');
|
|
232
|
-
}
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
export type PropsAndRef<T, Ctx, UCProps> = PropsWithRenderFnChildren<T, Ctx, UCProps> &
|
|
236
|
-
RefAttributes<unknown>;
|
|
237
|
-
export type ForwardRefComponent<T, Ctx, UCProps> = ForwardRefExoticComponent<
|
|
238
|
-
PropsAndRef<T, Ctx, UCProps>
|
|
239
|
-
>;
|
|
240
|
-
type ComponentOrProps<T, Context, UCProps> = T extends [infer ParentProps, infer ChildProps]
|
|
241
|
-
? ComponentType<ParentProps, ChildProps, Context, UCProps>
|
|
242
|
-
: ForwardRefComponent<T, Context, UCProps>;
|
|
243
|
-
|
|
244
|
-
export type ComponentType<
|
|
245
|
-
ComponentProps,
|
|
246
|
-
ChildComponentProps = {},
|
|
247
|
-
ContextType = {},
|
|
248
|
-
UCProps = {},
|
|
249
|
-
FNType = null,
|
|
250
|
-
> = (FNType extends null
|
|
251
|
-
? ForwardRefComponent<ComponentProps, ContextType, UCProps>
|
|
252
|
-
: FNType & { displayName: string }) & {
|
|
253
|
-
[K in keyof ChildComponentProps]: ComponentOrProps<ChildComponentProps[K], ContextType, UCProps>;
|
|
254
|
-
} & {
|
|
255
|
-
[CORE_COMPONENT]: boolean;
|
|
256
|
-
[CREATE_COMPONENT]: () => ComponentType<
|
|
257
|
-
ComponentProps,
|
|
258
|
-
ChildComponentProps,
|
|
259
|
-
ContextType,
|
|
260
|
-
UCProps
|
|
261
|
-
>;
|
|
262
|
-
};
|
|
263
|
-
|
|
264
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
265
|
-
interface ClassWithUncontrolledProps<Props> {
|
|
266
|
-
uncontrolledProps(): unknown;
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
function assignProps(p1, p2) {
|
|
270
|
-
return _assignProps(p2, p1);
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
function createComponent<ComponentProps, ChildComponentProps = {}, ContextType = {}, FNType = null>(
|
|
274
|
-
OriginComponent,
|
|
275
|
-
childComponents = {},
|
|
276
|
-
options: {
|
|
277
|
-
context?: React.Context<ContextType>;
|
|
278
|
-
parent?: ComponentType<unknown> | ComponentType<unknown>[];
|
|
279
|
-
enhancements?: [any];
|
|
280
|
-
} = {},
|
|
281
|
-
): ComponentType<
|
|
282
|
-
ComponentProps extends Component<infer Props> ? Props : ComponentProps,
|
|
283
|
-
ChildComponentProps,
|
|
284
|
-
ContextType,
|
|
285
|
-
ComponentProps extends ClassWithUncontrolledProps<any>
|
|
286
|
-
? ReturnType<ComponentProps['uncontrolledProps']>
|
|
287
|
-
: { [key: string]: (arg: unknown) => void },
|
|
288
|
-
FNType
|
|
289
|
-
> {
|
|
290
|
-
const {
|
|
291
|
-
context = React.createContext<ContextType>({} as ContextType),
|
|
292
|
-
parent = [],
|
|
293
|
-
enhancements = [],
|
|
294
|
-
} = options;
|
|
295
|
-
let parents = Array.isArray(parent) ? parent : [parent];
|
|
296
|
-
if (parents.length) {
|
|
297
|
-
const wholeFamily = parents.reduce((acc, parent) => {
|
|
298
|
-
if (parent[PARENT_COMPONENTS]) {
|
|
299
|
-
acc = [...parent[PARENT_COMPONENTS], ...acc];
|
|
300
|
-
}
|
|
301
|
-
return acc;
|
|
302
|
-
}, parents);
|
|
303
|
-
OriginComponent[PARENT_COMPONENTS] = wholeFamily;
|
|
304
|
-
parents = wholeFamily;
|
|
305
|
-
}
|
|
306
|
-
if (OriginComponent[CORE_COMPONENT]) {
|
|
307
|
-
parents.push(OriginComponent);
|
|
308
|
-
}
|
|
309
|
-
const Component = createComposeComponent(OriginComponent, context, [
|
|
310
|
-
// @ts-ignore
|
|
311
|
-
...enhancements.map((f) => f(context, parents, createComponent, childComponents)),
|
|
312
|
-
bindHandlerEnhancement(),
|
|
313
|
-
childrenEnhancement(context, parents),
|
|
314
|
-
// root must be under the children
|
|
315
|
-
rootEnhancement(),
|
|
316
|
-
uncontrolledPropsEnhancement(),
|
|
317
|
-
staticChildrenEnhancement(childComponents, createComponent, {
|
|
318
|
-
context,
|
|
319
|
-
parent,
|
|
320
|
-
enhancements,
|
|
321
|
-
}),
|
|
322
|
-
// functionDefaultProps must be under the staticChild
|
|
323
|
-
functionDefaultPropsEnhancement(),
|
|
324
|
-
// inheritedName must be under the staticChild
|
|
325
|
-
inheritedNameEnhancement(),
|
|
326
|
-
// dataName must be under the staticChild
|
|
327
|
-
dataNameEnhancement(),
|
|
328
|
-
// enhanceEnhancement must be under the functionDefaultPropsEnhancement
|
|
329
|
-
enhanceEnhancement(),
|
|
330
|
-
styleEnhancement(childComponents, context),
|
|
331
|
-
// must be the last one so any properties can be raised
|
|
332
|
-
hoistPropsEnhancement(childComponents, context),
|
|
333
|
-
]);
|
|
334
|
-
Component[CONTEXT_COMPONENT] = context;
|
|
335
|
-
Component[CREATE_COMPONENT] = function (
|
|
336
|
-
_OriginComponent = OriginComponent,
|
|
337
|
-
_childComponents = childComponents,
|
|
338
|
-
_options = options,
|
|
339
|
-
) {
|
|
340
|
-
return createComponent(_OriginComponent, _childComponents, _options);
|
|
341
|
-
};
|
|
342
|
-
Component[CORE_COMPONENT] = true;
|
|
343
|
-
return Component;
|
|
344
|
-
}
|
|
345
|
-
|
|
346
|
-
function createBaseComponent<ComponentProps>(OriginComponent): ComponentType<ComponentProps> {
|
|
347
|
-
let Component = null;
|
|
348
|
-
if (
|
|
349
|
-
!React.PureComponent.isPrototypeOf(OriginComponent) &&
|
|
350
|
-
!React.Component.isPrototypeOf(OriginComponent) &&
|
|
351
|
-
typeof OriginComponent === 'function'
|
|
352
|
-
) {
|
|
353
|
-
Component = React.forwardRef(OriginComponent);
|
|
354
|
-
Component.displayName = OriginComponent.displayName;
|
|
355
|
-
Component.defaultProps = {
|
|
356
|
-
'data-ui-name': OriginComponent.displayName,
|
|
357
|
-
...OriginComponent.defaultProps,
|
|
358
|
-
};
|
|
359
|
-
Component[CORE_COMPONENT] = true;
|
|
360
|
-
} else {
|
|
361
|
-
throw new Error('createBaseComponent accepts only functional component');
|
|
362
|
-
}
|
|
363
|
-
return Component;
|
|
364
|
-
}
|
|
365
|
-
|
|
366
|
-
export * from './Component';
|
|
367
|
-
export * from './styled';
|
|
368
|
-
export * from './register';
|
|
369
|
-
export {
|
|
370
|
-
createBaseComponent,
|
|
371
|
-
register,
|
|
372
|
-
CREATE_COMPONENT,
|
|
373
|
-
CORE_INSTANCE,
|
|
374
|
-
CONTEXT_COMPONENT,
|
|
375
|
-
CHILDREN_COMPONENT,
|
|
376
|
-
INHERITED_NAME,
|
|
377
|
-
CORE_COMPONENT,
|
|
378
|
-
STATIC_COMPONENT,
|
|
379
|
-
assignProps,
|
|
380
|
-
};
|
|
381
|
-
export default createComponent;
|
package/src/register.tsx
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
class Register {
|
|
2
|
-
data = {};
|
|
3
|
-
|
|
4
|
-
get<D>(name, defaultValue?: D): D {
|
|
5
|
-
if (name in this.data) {
|
|
6
|
-
return this.data[name];
|
|
7
|
-
} else {
|
|
8
|
-
return this.set(name, defaultValue);
|
|
9
|
-
}
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
set<D>(name, value: D): D {
|
|
13
|
-
return (this.data[name] = value);
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export { Register };
|
|
18
|
-
export default new Register();
|
package/src/styled/index.tsx
DELETED