Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions src/lib/unstable/core/Entity/Entity.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import React from 'react';

import {SchemaRendererContext} from '../SchemaRendererContext';
import type {JsonSchema} from '../types';
import {useSchemaRendererField} from '../useSchemaRendererField';

import {getRenderKit} from './utils';

export interface EntityProps {
name: string;
schema: JsonSchema;
}

const EntityComponent: React.FC<EntityProps> = ({name, schema}) => {
const {config, mode} = React.useContext(SchemaRendererContext);

const renderKit = React.useMemo(
() => getRenderKit({config, mode, schema}),
[config, mode, schema],
);

const options = React.useMemo(
() => ({
data: {schema},
defaultValue: schema.default,
subscription: {
error: true,
submitFailed: true,
touched: true,
validating: true,
value: true,
},
}),
[schema],
);

const field = useSchemaRendererField(name, options);

if (!renderKit.View) {
return null;
}

let content = null;

if (renderKit.independent) {
content = (
<renderKit.View
{...field}
schema={schema}
Wrapper={renderKit.Wrapper}
viewProps={renderKit.viewProps}
wrapperProps={renderKit.wrapperProps}
/>
);
} else {
content = <renderKit.View {...field} schema={schema} viewProps={renderKit.viewProps} />;

if (renderKit.Wrapper) {
content = (
<renderKit.Wrapper {...field} schema={schema} wrapperProps={renderKit.wrapperProps}>
{content}
</renderKit.Wrapper>
);
}
}

return <div>{content}</div>;
};

export const Entity = React.memo(EntityComponent);
30 changes: 30 additions & 0 deletions src/lib/unstable/core/Entity/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import type {SchemaRendererMode} from '../constants';
import type {
IndependentView,
JsonSchema,
SchemaRendererConfig,
SimpleView,
Wrapper,
} from '../types';

export type GetRenderKitParams<Schema extends JsonSchema> = {
config: SchemaRendererConfig;
mode: SchemaRendererMode;
schema: Schema;
};

export type GetRenderKitReturn<Schema extends JsonSchema> =
| {
View: SimpleView<Schema> | undefined;
Wrapper: Wrapper<Schema> | undefined;
independent: false | undefined;
viewProps: Record<string, any>;
wrapperProps: Record<string, any>;
}
| {
View: IndependentView<Schema>;
Wrapper: Wrapper<Schema> | undefined;
independent: true;
viewProps: Record<string, any>;
wrapperProps: Record<string, any>;
};
39 changes: 39 additions & 0 deletions src/lib/unstable/core/Entity/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import get from 'lodash/get';

import {EMPTY_OBJECT} from '../constants';
import type {JsonSchema, View, Wrapper} from '../types';

import type {GetRenderKitParams, GetRenderKitReturn} from './types';

export const getRenderKit = <Schema extends JsonSchema>({
config,
mode,
schema,
}: GetRenderKitParams<Schema>): GetRenderKitReturn<Schema> => {
const viewType: string | undefined = get(schema, 'entityParameters.viewType');
const wrapperType: string | undefined = get(schema, 'entityParameters.wrapperType');

const viewProps = get(schema, 'entityParameters.viewProps', EMPTY_OBJECT);
const wrapperProps = get(schema, 'entityParameters.wrapperProps', EMPTY_OBJECT);

const ViewComponent: View<Schema> | undefined = get(
config,
`${schema.type}.views.${viewType}.${mode}.Component`,
);
const WrapperComponent: Wrapper<Schema> | undefined = get(
config,
`${schema.type}.wrappers.${wrapperType}`,
);
const independent: boolean | undefined = get(
config,
`${schema.type}.views.${viewType}.${mode}.independent`,
);

return {
View: ViewComponent,
viewProps,
Wrapper: WrapperComponent,
wrapperProps,
independent,
};
};
51 changes: 51 additions & 0 deletions src/lib/unstable/core/SchemaRenderer/SchemaRenderer.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import React from 'react';

import {Entity} from '../Entity';
import {SchemaRendererContext, type SchemaRendererContextType} from '../SchemaRendererContext';
import {SchemaRendererServiceField} from '../SchemaRendererServiceField';
import type {SchemaRendererMode} from '../constants';
import type {ErrorMessages, JsonSchema, SchemaRendererConfig} from '../types';

export interface SchemaRendererProps {
config: SchemaRendererConfig;
errorMessages?: ErrorMessages;
mode: SchemaRendererMode;
name: string;
schema: JsonSchema;
}

const SchemaRendererComponent: React.FC<SchemaRendererProps> = ({
config,
errorMessages,
mode,
name,
schema,
}) => {
const serviceFieldName = `DYNAMIC_FORMS_SERVICE_FIELD/${name}`;

const context: SchemaRendererContextType = React.useMemo(
() => ({
config,
mode,
name,
schema,
serviceFieldName,
}),
[config, mode, name, schema, serviceFieldName],
);

return (
<SchemaRendererContext.Provider value={context}>
<SchemaRendererServiceField
config={config}
errorMessages={errorMessages}
name={name}
mainSchema={schema}
serviceFieldName={serviceFieldName}
/>
<Entity name={name} schema={schema} />
</SchemaRendererContext.Provider>
);
};

export const SchemaRenderer = React.memo(SchemaRendererComponent);
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export * from './SchemaRenderer';
export * from './context';
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';

import type {SchemaRendererContextType} from '../../types';
import type {SchemaRendererContextType} from './types';

export const SchemaRendererContext = React.createContext<SchemaRendererContextType>(
null as unknown as SchemaRendererContextType,
Expand Down
2 changes: 2 additions & 0 deletions src/lib/unstable/core/SchemaRendererContext/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './SchemaRendererContext';
export * from './types';
10 changes: 10 additions & 0 deletions src/lib/unstable/core/SchemaRendererContext/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import type {SchemaRendererMode} from '../constants';
import type {JsonSchema, SchemaRendererConfig} from '../types';

export interface SchemaRendererContextType {
config: SchemaRendererConfig;
mode: SchemaRendererMode;
name: string;
schema: JsonSchema;
serviceFieldName: string;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import React from 'react';

import {useField, useForm} from 'react-final-form';

import type {ErrorMessages, JsonSchema, SchemaRendererConfig} from '../types';

import {getValidate} from './utils';

export interface SchemaRendererProps {
config: SchemaRendererConfig;
errorMessages?: ErrorMessages;
name: string;
mainSchema: JsonSchema;
serviceFieldName: string;
}

const SchemaRendererServiceFieldComponent: React.FC<SchemaRendererProps> = ({
config,
errorMessages,
name,
mainSchema,
serviceFieldName,
}) => {
const {setValidationCache, setValidationWaiters} = useForm().mutators;

const validate = React.useMemo(
() =>
getValidate({
config,
errorMessages,
name,
mainSchema,
serviceFieldName,
setValidationCache,
setValidationWaiters,
}),
[
config,
errorMessages,
name,
mainSchema,
serviceFieldName,
setValidationCache,
setValidationWaiters,
],
);

useField(serviceFieldName, {data: {schema: mainSchema}, subscription: {}, validate});

return null;
};

export const SchemaRendererServiceField = React.memo(SchemaRendererServiceFieldComponent);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './SchemaRendererServiceField';
53 changes: 53 additions & 0 deletions src/lib/unstable/core/SchemaRendererServiceField/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import type {ErrorObject, ValidateFunction} from 'ajv';
import type {FieldValidator} from 'final-form';

import type {SetValidationCacheMutator, SetValidationWaitersMutator} from '../mutators';
import type {
ErrorMessages,
FieldValue,
JsonSchema,
SchemaRendererConfig,
SchemaToValueType,
Validator,
} from '../types';

export type EntityParametersError = ErrorObject<
'entityParameters',
{
schema: JsonSchema;
validator: Validator<JsonSchema>;
value: FieldValue | null | undefined;
}
>;

export type GetAjvValidateParams = {
config: SchemaRendererConfig;
mainSchema: JsonSchema;
};

export interface GetAjvValidateReturn extends ValidateFunction {
errors?: (ErrorObject | EntityParametersError)[];
}

export interface GetAjvErrorMessageParams {
ajvErrorMessage?: string;
errorMessages?: ErrorMessages;
instancePath: string;
keyword: string;
mainSchema: JsonSchema;
schemaPath: string;
}

export type GetValidateParams<Schema extends JsonSchema> = {
config: SchemaRendererConfig;
errorMessages?: ErrorMessages;
name: string;
mainSchema: Schema;
serviceFieldName: string;
setValidationCache: SetValidationCacheMutator;
setValidationWaiters: SetValidationWaitersMutator;
};

export type GetValidateReturn<Schema extends JsonSchema> = FieldValidator<
SchemaToValueType<Schema>
>;
Loading