feat: 渲染器包裹支持外层自定义 (#11138)

This commit is contained in:
liaoxuezhi 2024-10-30 14:37:36 +08:00 committed by GitHub
parent 887d860f96
commit 1b17c395ae
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 116 additions and 123 deletions

View File

@ -11,7 +11,11 @@ import {ThemeContext} from './theme';
import {Schema, SchemaNode} from './types';
import {autobind, isEmpty} from './utils/helper';
import {RootStoreContext} from './WithRootStore';
import {StatusScoped, StatusScopedProps} from './StatusScoped';
import {
StatusScoped,
StatusScopedWrapper,
StatusScopedProps
} from './StatusScoped';
export interface RootRenderProps {
location?: Location;
@ -154,8 +158,6 @@ export interface renderChildProps
}
export type ReactElement = React.ReactNode[] | JSX.Element | null | false;
const StatusScopedSchemaRenderer = StatusScoped(SchemaRenderer);
export function renderChildren(
prefix: string,
node: SchemaNode,
@ -206,6 +208,8 @@ export function renderChild(
props = transform(props);
}
const Comp = props.env.SchemaRenderer || SchemaRenderer;
if (
['dialog', 'drawer'].includes(schema?.type) &&
!schema?.component &&
@ -216,19 +220,26 @@ export function renderChild(
// 所以这里先根据 type 来处理一下
// 等后续把状态处理再抽一层,可以把此处放到 SchemaRenderer 里面去
return (
<StatusScopedSchemaRenderer
render={renderChild as any}
{...props}
key={props.key ?? schema.key}
schema={schema}
propKey={schema.key}
$path={`${prefix ? `${prefix}/` : ''}${(schema && schema.type) || ''}`}
/>
<StatusScopedWrapper>
{({statusStore}) => (
<Comp
render={renderChild as any}
{...props}
key={props.key ?? schema.key}
schema={schema}
propKey={schema.key}
$path={`${prefix ? `${prefix}/` : ''}${
(schema && schema.type) || ''
}`}
statusStore={statusStore}
/>
)}
</StatusScopedWrapper>
);
}
return (
<SchemaRenderer
<Comp
render={renderChild as any}
{...props}
key={props.key ?? schema.key}

View File

@ -108,7 +108,7 @@ export class SchemaRenderer extends React.Component<SchemaRendererProps, any> {
exit?: string;
} = {};
reaction: any;
toDispose: Array<() => any> = [];
unbindEvent: (() => void) | undefined = undefined;
isStatic: any = undefined;
@ -142,22 +142,24 @@ export class SchemaRenderer extends React.Component<SchemaRendererProps, any> {
this.removeAnimationAttention = this.removeAnimationAttention.bind(this);
// 监听statusStore更新
this.reaction = reaction(
() => {
const id = filter(props.schema.id, props.data);
const name = filter(props.schema.name, props.data);
return `${
props.statusStore.visibleState[id] ??
props.statusStore.visibleState[name]
}${
props.statusStore.disableState[id] ??
props.statusStore.disableState[name]
}${
props.statusStore.staticState[id] ??
props.statusStore.staticState[name]
}`;
},
() => this.forceUpdate()
this.toDispose.push(
reaction(
() => {
const id = filter(props.schema.id, props.data);
const name = filter(props.schema.name, props.data);
return `${
props.statusStore.visibleState[id] ??
props.statusStore.visibleState[name]
}${
props.statusStore.disableState[id] ??
props.statusStore.disableState[name]
}${
props.statusStore.staticState[id] ??
props.statusStore.staticState[name]
}`;
},
() => this.forceUpdate()
)
);
}
@ -170,7 +172,8 @@ export class SchemaRenderer extends React.Component<SchemaRendererProps, any> {
}
componentWillUnmount() {
this.reaction?.();
this.toDispose.forEach(fn => fn());
this.toDispose = [];
this.unbindEvent?.();
this.removeAnimationStyle();
}

View File

@ -8,80 +8,43 @@ export interface StatusScopedProps {
statusStore: IStatusStore;
}
export interface StatusScopedWrapperProps {
children: (props: {statusStore: IStatusStore}) => JSX.Element;
}
export function StatusScopedWrapper({children}: StatusScopedWrapperProps) {
const store = React.useMemo(() => StatusStore.create({}), []);
React.useEffect(() => {
return () => {
destroy(store);
};
}, []);
return children({statusStore: store});
}
export function StatusScoped<
T extends React.ComponentType<React.ComponentProps<T> & StatusScopedProps>
>(ComposedComponent: T) {
type OuterProps = JSX.LibraryManagedAttributes<
T,
Omit<React.ComponentProps<T>, keyof StatusScopedProps>
> & {};
const result = hoistNonReactStatic(
class extends React.Component<OuterProps> {
static displayName = `StatusScoped(${
ComposedComponent.displayName || ComposedComponent.name
})`;
static ComposedComponent = ComposedComponent as React.ComponentType<T>;
store?: IStatusStore;
constructor(props: OuterProps) {
super(props);
this.childRef = this.childRef.bind(this);
this.getWrappedInstance = this.getWrappedInstance.bind(this);
this.store = StatusStore.create({});
}
ref: any;
childRef(ref: any) {
while (ref && ref.getWrappedInstance) {
ref = ref.getWrappedInstance();
}
this.ref = ref;
}
getWrappedInstance() {
return this.ref;
}
componentWillUnmount(): void {
this.store && destroy(this.store);
delete this.store;
}
render() {
const injectedProps: {
statusStore: IStatusStore;
} = {
statusStore: this.store!
};
const refConfig =
ComposedComponent.prototype?.isReactComponent ||
(ComposedComponent as any).$$typeof ===
Symbol.for('react.forward_ref')
? {ref: this.childRef}
: {forwardedRef: this.childRef};
return (
const wrapped = (
props: JSX.LibraryManagedAttributes<
T,
Omit<React.ComponentProps<T>, keyof StatusScopedProps>
> & {},
ref: any
) => {
return (
<StatusScopedWrapper>
{({statusStore}) => (
<ComposedComponent
{...(this.props as JSX.LibraryManagedAttributes<
T,
React.ComponentProps<T>
> as any)}
{...injectedProps}
{...refConfig}
{...(props as any)}
statusStore={statusStore}
ref={ref}
/>
);
}
},
ComposedComponent
);
return result as typeof result & {
ComposedComponent: T;
)}
</StatusScopedWrapper>
);
};
return React.forwardRef(wrapped as any) as typeof wrapped;
}

View File

@ -163,6 +163,11 @@ export interface RendererEnv {
action: ICmptAction,
event: RendererEvent<any, any>
) => Promise<void | boolean>;
/**
*
*/
SchemaRenderer?: React.ComponentType<any>;
}
export const EnvContext = React.createContext<RendererEnv | void>(undefined);

View File

@ -119,7 +119,10 @@ import {
} from './utils/index';
import type {OnEventProps} from './utils/index';
import {valueMap as styleMap} from './utils/style-helper';
import {RENDERER_TRANSMISSION_OMIT_PROPS} from './SchemaRenderer';
import {
RENDERER_TRANSMISSION_OMIT_PROPS,
SchemaRenderer
} from './SchemaRenderer';
import type {IItem} from './store/list';
import CustomStyle from './components/CustomStyle';
import {StatusScoped} from './StatusScoped';
@ -232,7 +235,8 @@ export {
disableDebug,
envOverwrite,
getGlobalOptions,
setGlobalOptions
setGlobalOptions,
SchemaRenderer
};
export function render(

View File

@ -240,7 +240,11 @@ export interface ApiObject extends BaseApiObject {
operationName?: string;
body?: PlainObject;
query?: PlainObject;
mockResponse?: PlainObject;
mockResponse?: {
status: number;
data?: any;
delay?: number;
};
adaptor?: ResponseAdaptor;
requestAdaptor?: RequestAdaptor;
/**
@ -268,7 +272,7 @@ export interface fetcherResult {
[propName: string]: any; // 为了兼容其他返回格式
};
status: number;
headers: object;
headers?: object;
}
export interface fetchOptions {

View File

@ -517,6 +517,14 @@ export function responseAdaptor(ret: fetcherResult, api: ApiObject) {
return payload;
}
function lazyResolve<T = any>(value: T, waitFor = 1000) {
return new Promise<T>(resolve => {
setTimeout(() => {
resolve(value);
}, waitFor);
});
}
export function wrapFetcher(
fn: (config: FetcherConfig) => Promise<fetcherResult>,
tracker?: (eventTrack: EventTrack, data: any) => void
@ -614,7 +622,11 @@ export function wrapFetcher(
api.mockResponse,
api
);
return wrapAdaptor(Promise.resolve(api.mockResponse) as any, api, data);
return wrapAdaptor(
lazyResolve(api.mockResponse, api.mockResponse?.delay ?? 100),
api,
data
);
}
if (!isValidApi(api.url)) {

View File

@ -38,7 +38,7 @@ import {BadgeObject} from 'amis-ui';
import {RemoteOptionsProps, withRemoteConfig} from 'amis-ui';
import {Spinner, Menu} from 'amis-ui';
import {ScopedContext, IScopedContext} from 'amis-core';
import type {NavigationItem} from 'amis-ui/lib/components/menu';
import type {NavigationItem} from 'amis-ui/lib/components/menu/index';
import type {MenuItemProps} from 'amis-ui/lib/components/menu/MenuItem';
import type {Payload} from 'amis-core';
@ -1415,15 +1415,8 @@ const ConditionBuilderWithRemoteOptions = withRemoteConfig({
}
async handleSelect(link: Link, depth: number) {
const {
onSelect,
env,
data,
level,
dispatchEvent,
updateConfig,
config
} = this.props;
const {onSelect, env, data, level, dispatchEvent, updateConfig, config} =
this.props;
const rendererEvent = await dispatchEvent(
'click',
@ -1466,14 +1459,8 @@ const ConditionBuilderWithRemoteOptions = withRemoteConfig({
}
render() {
const {
disabled,
loading,
config,
deferLoad,
updateConfig,
...rest
} = this.props;
const {disabled, loading, config, deferLoad, updateConfig, ...rest} =
this.props;
const currentLink = this.getCurrentLink(this.state.currentKey);
return (

View File

@ -37,6 +37,7 @@ import {IIRendererStore} from 'amis-core';
import type {ListenerAction} from 'amis-core';
import type {ScopedComponentType} from 'amis-core';
import isPlainObject from 'lodash/isPlainObject';
import {isAlive} from 'mobx-state-tree';
export const eventTypes = [
/* 初始化时执行,默认 */
@ -529,6 +530,9 @@ export default class Service extends React.Component<ServiceProps> {
// 保存 ajax 请求的时候返回时数据部分。
const data = result?.hasOwnProperty('ok') ? result.data ?? {} : result;
const {onBulkChange, dispatchEvent, store, formStore} = this.props;
if (!isAlive(store)) {
return;
}
dispatchEvent?.(
'fetchInited',