@tramvai/react 2.11.0 → 2.21.0

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.
@@ -1,3 +1,3 @@
1
1
  import type { Context } from 'react';
2
- import type { DI_TOKEN } from '@tramvai/core';
2
+ import type { DI_TOKEN } from '@tinkoff/dippy';
3
3
  export declare const DIContext: Context<typeof DI_TOKEN>;
@@ -1,4 +1,6 @@
1
1
  import type { LoadableComponent, DefaultComponent } from '@loadable/component';
2
+ import type { TramvaiComponent, TramvaiComponentDecl } from '../typings/components';
3
+ export declare const resolveLazyComponent: (componentOrLoader?: TramvaiComponentDecl | undefined) => Promise<TramvaiComponent | undefined>;
2
4
  interface Options {
3
5
  loading?: JSX.Element;
4
6
  }
@@ -7,5 +9,5 @@ interface Options {
7
9
  * The method is intended to make it possible to wait for retries to synchronously load unsuccessful CSS assets
8
10
  */
9
11
  export declare const __lazyErrorHandler: (error: any, load: () => Promise<any>) => Promise<any>;
10
- export declare const lazy: <Props, Component>(load: (props?: Props | undefined) => Promise<DefaultComponent<Props>>, options?: Options) => LoadableComponent<Props>;
12
+ export declare const lazy: <Props>(load: (props?: Props | undefined) => Promise<DefaultComponent<Props>>, options?: Options) => LoadableComponent<Props>;
11
13
  export {};
package/lib/react.d.ts CHANGED
@@ -6,4 +6,5 @@ export { ErrorBoundary } from './error/component';
6
6
  export { FallbackError } from './error/fallback';
7
7
  export { withError } from './error/hoc';
8
8
  export * from './error/tokens';
9
- export { lazy, __lazyErrorHandler } from './lazy/lazy';
9
+ export { lazy, __lazyErrorHandler, resolveLazyComponent } from './lazy/lazy';
10
+ export * from './typings/components';
package/lib/react.es.js CHANGED
@@ -164,6 +164,26 @@ const withError = ({ fallbackComponent, } = {}) => (WrappedComponent) => {
164
164
  return hoistStatics(WrapperWithError, WrappedComponent);
165
165
  };
166
166
 
167
+ const resolveLazyComponent = async (componentOrLoader) => {
168
+ if (!componentOrLoader) {
169
+ return;
170
+ }
171
+ if ('load' in componentOrLoader && typeof componentOrLoader.load === 'function') {
172
+ const mod = await componentOrLoader.load();
173
+ let component;
174
+ if (mod.__esModule) {
175
+ component = mod.default;
176
+ }
177
+ else {
178
+ component = mod.default || mod;
179
+ }
180
+ // manually hoist static properties from preloaded component to loadable wrapper,
181
+ // this open access to current page component static properties outside before rendering
182
+ hoistStatics(componentOrLoader, component);
183
+ return component;
184
+ }
185
+ return componentOrLoader;
186
+ };
167
187
  /**
168
188
  * @private Only for internal usage!
169
189
  * The method is intended to make it possible to wait for retries to synchronously load unsuccessful CSS assets
@@ -204,4 +224,4 @@ the first argument should be transformed into a special object, but the current
204
224
  });
205
225
  };
206
226
 
207
- export { DIContext, ERROR_BOUNDARY_FALLBACK_COMPONENT_TOKEN, ERROR_BOUNDARY_TOKEN, ErrorBoundary, FallbackError, ROOT_ERROR_BOUNDARY_COMPONENT_TOKEN, UniversalErrorBoundary, __lazyErrorHandler, lazy, useDi, useDiContainer, withDi, withError };
227
+ export { DIContext, ERROR_BOUNDARY_FALLBACK_COMPONENT_TOKEN, ERROR_BOUNDARY_TOKEN, ErrorBoundary, FallbackError, ROOT_ERROR_BOUNDARY_COMPONENT_TOKEN, UniversalErrorBoundary, __lazyErrorHandler, lazy, resolveLazyComponent, useDi, useDiContainer, withDi, withError };
package/lib/react.js CHANGED
@@ -174,6 +174,26 @@ const withError = ({ fallbackComponent, } = {}) => (WrappedComponent) => {
174
174
  return hoistStatics__default["default"](WrapperWithError, WrappedComponent);
175
175
  };
176
176
 
177
+ const resolveLazyComponent = async (componentOrLoader) => {
178
+ if (!componentOrLoader) {
179
+ return;
180
+ }
181
+ if ('load' in componentOrLoader && typeof componentOrLoader.load === 'function') {
182
+ const mod = await componentOrLoader.load();
183
+ let component;
184
+ if (mod.__esModule) {
185
+ component = mod.default;
186
+ }
187
+ else {
188
+ component = mod.default || mod;
189
+ }
190
+ // manually hoist static properties from preloaded component to loadable wrapper,
191
+ // this open access to current page component static properties outside before rendering
192
+ hoistStatics__default["default"](componentOrLoader, component);
193
+ return component;
194
+ }
195
+ return componentOrLoader;
196
+ };
177
197
  /**
178
198
  * @private Only for internal usage!
179
199
  * The method is intended to make it possible to wait for retries to synchronously load unsuccessful CSS assets
@@ -222,6 +242,7 @@ exports.ROOT_ERROR_BOUNDARY_COMPONENT_TOKEN = ROOT_ERROR_BOUNDARY_COMPONENT_TOKE
222
242
  exports.UniversalErrorBoundary = UniversalErrorBoundary;
223
243
  exports.__lazyErrorHandler = __lazyErrorHandler;
224
244
  exports.lazy = lazy;
245
+ exports.resolveLazyComponent = resolveLazyComponent;
225
246
  exports.useDi = useDi;
226
247
  exports.useDiContainer = useDiContainer;
227
248
  exports.withDi = withDi;
@@ -0,0 +1,26 @@
1
+ import type { ComponentType, ReactNode } from 'react';
2
+ import type { PageAction, Reducer } from '@tramvai/types-actions-state-context';
3
+ import type { LoadableComponent } from '@loadable/component';
4
+ export declare type LazyComponentWrapper<Component extends ComponentType<any>> = {
5
+ load: (props?: Component extends ComponentType<infer Props> ? Props : {}) => Promise<{
6
+ default: Component;
7
+ }>;
8
+ } & LoadableComponent<Component extends ComponentType<infer Props> ? Props : {}>;
9
+ export interface PageComponentOptions {
10
+ actions?: PageAction[];
11
+ reducers?: Reducer<any, any>[];
12
+ components?: Record<string, TramvaiComponent>;
13
+ }
14
+ export interface PageComponentProps {
15
+ }
16
+ export declare type PageComponent = ComponentType<PageComponentProps> & Partial<PageComponentOptions>;
17
+ export interface LayoutComponentOptions {
18
+ }
19
+ export interface LayoutComponentProps {
20
+ Header: ComponentType;
21
+ Footer: ComponentType;
22
+ children?: ReactNode;
23
+ }
24
+ export declare type LayoutComponent = ComponentType<LayoutComponentProps> & Partial<LayoutComponentOptions>;
25
+ export declare type TramvaiComponent = PageComponent | LayoutComponent;
26
+ export declare type TramvaiComponentDecl = TramvaiComponent | LazyComponentWrapper<TramvaiComponent>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tramvai/react",
3
- "version": "2.11.0",
3
+ "version": "2.21.0",
4
4
  "description": "",
5
5
  "main": "lib/react.js",
6
6
  "typings": "lib/react.d.ts",
@@ -18,15 +18,15 @@
18
18
  "build-for-publish": "true"
19
19
  },
20
20
  "dependencies": {
21
- "@loadable/component": "^5.15.0",
21
+ "@loadable/component": "^5.15.2",
22
22
  "@types/loadable__component": "^5.13.4",
23
+ "@tramvai/types-actions-state-context": "2.21.0",
23
24
  "hoist-non-react-statics": "^3.3.0"
24
25
  },
25
26
  "peerDependencies": {
26
- "@tinkoff/dippy": "0.7.45",
27
+ "@tinkoff/dippy": "0.8.2",
27
28
  "@tinkoff/utils": "^2.1.2",
28
- "@tinkoff/url": "0.7.39",
29
- "@tramvai/core": "2.11.0",
29
+ "@tinkoff/url": "0.8.2",
30
30
  "react": ">=16.14.0",
31
31
  "react-dom": ">=16.14.0",
32
32
  "tslib": "^2.0.3"