feat: add schemaExpansionLevel

This commit is contained in:
Alex Varchuk 2022-01-25 14:35:03 +02:00
parent b74dcde42b
commit a7534ff0ca
5 changed files with 35 additions and 8 deletions

View File

@ -32,7 +32,7 @@ export interface FieldProps extends SchemaOptions {
export class Field extends React.Component<FieldProps> {
toggle = () => {
if (this.props.field.expanded === undefined && this.props.expandByDefault) {
this.props.field.expanded = false;
this.props.field.collapse();
} else {
this.props.field.toggle();
}
@ -94,6 +94,7 @@ export class Field extends React.Component<FieldProps> {
skipReadOnly={this.props.skipReadOnly}
skipWriteOnly={this.props.skipWriteOnly}
showTitle={this.props.showTitle}
level={this.props.level}
/>
</InnerPropertiesWrap>
</PropertyCellWithInner>

View File

@ -25,8 +25,10 @@ export const ObjectSchema = observer(
discriminator,
skipReadOnly,
skipWriteOnly,
level,
}: ObjectSchemaProps) => {
const { expandSingleSchemaField, showObjectSchemaExamples } = React.useContext(OptionsContext);
const { expandSingleSchemaField, showObjectSchemaExamples, schemaExpansionLevel } =
React.useContext(OptionsContext);
const filteredFields = React.useMemo(
() =>
@ -42,7 +44,8 @@ export const ObjectSchema = observer(
[skipReadOnly, skipWriteOnly, fields],
);
const expandByDefault = expandSingleSchemaField && filteredFields.length === 1;
const expandByDefault =
(expandSingleSchemaField && filteredFields.length === 1) || schemaExpansionLevel >= level!;
return (
<PropertiesTable>
@ -70,6 +73,7 @@ export const ObjectSchema = observer(
skipReadOnly={skipReadOnly}
skipWriteOnly={skipWriteOnly}
showTitle={showTitle}
level={level}
/>
);
})}

View File

@ -16,6 +16,7 @@ export interface SchemaOptions {
showTitle?: boolean;
skipReadOnly?: boolean;
skipWriteOnly?: boolean;
level?: number;
}
export interface SchemaProps extends SchemaOptions {
@ -25,7 +26,9 @@ export interface SchemaProps extends SchemaOptions {
@observer
export class Schema extends React.Component<Partial<SchemaProps>> {
render() {
const { schema } = this.props;
const { schema, ...rest } = this.props;
const level = (rest.level || 0) + 1;
if (!schema) {
return <em> Schema not provided </em>;
}
@ -50,7 +53,7 @@ export class Schema extends React.Component<Partial<SchemaProps>> {
}
return (
<ObjectSchema
{...{ ...this.props, schema: oneOf![schema.activeOneOf] }}
{...{ ...this.props, schema: oneOf![schema.activeOneOf], level }}
discriminator={{
fieldName: discriminatorProp,
parentSchema: schema,
@ -66,10 +69,10 @@ export class Schema extends React.Component<Partial<SchemaProps>> {
const types = Array.isArray(type) ? type : [type];
if (types.includes('object')) {
if (schema.fields?.length) {
return <ObjectSchema {...(this.props as any)} />;
return <ObjectSchema {...{ ...(this.props as any), level }} />;
}
} else if (types.includes('array')) {
return <ArraySchema {...(this.props as any)} />;
return <ArraySchema {...{ ...(this.props as any), level }} />;
}
// TODO: maybe adjust FieldDetails to accept schema

View File

@ -37,6 +37,7 @@ export interface RedocRawOptions {
simpleOneOfTypeLabel?: boolean | string;
payloadSampleIdx?: number;
expandSingleSchemaField?: boolean | string;
schemaExpansionLevel?: number | string | 'all';
showObjectSchemaExamples?: boolean | string;
unstable_ignoreMimeParameters?: boolean;
@ -74,6 +75,12 @@ function argValueToNumber(value: number | string | undefined): number | undefine
}
}
function argValueToExpandLevel(value?: number | string | undefined, defaultValue = 0): number {
if (value === 'all') return Infinity;
return argValueToNumber(value) || defaultValue;
}
export class RedocNormalizedOptions {
static normalizeExpandResponses(value: RedocRawOptions['expandResponses']) {
if (value === 'all') {
@ -225,6 +232,7 @@ export class RedocNormalizedOptions {
simpleOneOfTypeLabel: boolean;
payloadSampleIdx: number;
expandSingleSchemaField: boolean;
schemaExpansionLevel: number;
showObjectSchemaExamples: boolean;
/* tslint:disable-next-line */
@ -289,6 +297,7 @@ export class RedocNormalizedOptions {
this.simpleOneOfTypeLabel = argValueToBoolean(raw.simpleOneOfTypeLabel);
this.payloadSampleIdx = RedocNormalizedOptions.normalizePayloadSampleIdx(raw.payloadSampleIdx);
this.expandSingleSchemaField = argValueToBoolean(raw.expandSingleSchemaField);
this.schemaExpansionLevel = argValueToExpandLevel(raw.schemaExpansionLevel);
this.showObjectSchemaExamples = argValueToBoolean(raw.showObjectSchemaExamples);
this.unstable_ignoreMimeParameters = argValueToBoolean(raw.unstable_ignoreMimeParameters);

View File

@ -41,7 +41,7 @@ const DEFAULT_SERIALIZATION: Record<
*/
export class FieldModel {
@observable
expanded: boolean | undefined = false;
expanded: boolean | undefined;
schema: SchemaModel;
name: string;
@ -120,4 +120,14 @@ export class FieldModel {
toggle() {
this.expanded = !this.expanded;
}
@action
collapse(): void {
this.expanded = false;
}
@action
expand(): void {
this.expanded = true;
}
}