Skip to content

Commit

Permalink
feat: add support hidePropertiesPrefix
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexVarchuk committed Jan 21, 2025
1 parent 349a194 commit 93d6b6c
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 7 deletions.
20 changes: 17 additions & 3 deletions src/components/Fields/Field.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import { Schema } from '../Schema/Schema';

import type { SchemaOptions } from '../Schema/Schema';
import type { FieldModel } from '../../services/models';
import { OptionsContext } from '../OptionsProvider';
import { RedocNormalizedOptions } from '../../services/RedocNormalizedOptions';

export interface FieldProps extends SchemaOptions {
className?: string;
Expand All @@ -27,12 +29,15 @@ export interface FieldProps extends SchemaOptions {

field: FieldModel;
expandByDefault?: boolean;

fieldParentsName?: string[];
renderDiscriminatorSwitch?: (opts: FieldProps) => JSX.Element;
}

@observer
export class Field extends React.Component<FieldProps> {
static contextType = OptionsContext;
context: RedocNormalizedOptions;

toggle = () => {
if (this.props.field.expanded === undefined && this.props.expandByDefault) {
this.props.field.collapse();
Expand All @@ -49,12 +54,12 @@ export class Field extends React.Component<FieldProps> {
};

render() {
const { className = '', field, isLast, expandByDefault } = this.props;
const { hidePropertiesPrefix } = this.context;
const { className = '', field, isLast, expandByDefault, fieldParentsName = [] } = this.props;
const { name, deprecated, required, kind } = field;
const withSubSchema = !field.schema.isPrimitive && !field.schema.isCircular;

const expanded = field.expanded === undefined ? expandByDefault : field.expanded;

const labels = (
<>
{kind === 'additionalProperties' && <PropertyLabel>additional property</PropertyLabel>}
Expand All @@ -75,6 +80,10 @@ export class Field extends React.Component<FieldProps> {
onKeyPress={this.handleKeyPress}
aria-label={`expand ${name}`}
>
{!hidePropertiesPrefix &&
fieldParentsName.map(
name => name + '.\u200B', // zero-width space, a special character is used for correct line breaking
)}
<span className="property-name">{name}</span>
<ShelfIcon direction={expanded ? 'down' : 'right'} />
</button>
Expand All @@ -83,6 +92,10 @@ export class Field extends React.Component<FieldProps> {
) : (
<PropertyNameCell className={deprecated ? 'deprecated' : undefined} kind={kind} title={name}>
<PropertyBullet />
{!hidePropertiesPrefix &&
fieldParentsName.map(
name => name + '.\u200B', // zero-width space, a special character is used for correct line breaking
)}
<span className="property-name">{name}</span>
{labels}
</PropertyNameCell>
Expand All @@ -102,6 +115,7 @@ export class Field extends React.Component<FieldProps> {
<InnerPropertiesWrap>
<Schema
schema={field.schema}
fieldParentsName={[...(fieldParentsName || []), field.name]}
skipReadOnly={this.props.skipReadOnly}
skipWriteOnly={this.props.skipWriteOnly}
showTitle={this.props.showTitle}
Expand Down
14 changes: 12 additions & 2 deletions src/components/Schema/ArraySchema.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,24 @@ export class ArraySchema extends React.PureComponent<SchemaProps> {
render() {
const schema = this.props.schema;
const itemsSchema = schema.items;
const fieldParentsName = this.props.fieldParentsName;

const minMaxItems =
schema.minItems === undefined && schema.maxItems === undefined
? ''
: `(${humanizeConstraints(schema)})`;

const updatedParentsArray = fieldParentsName
? [...fieldParentsName.slice(0, -1), fieldParentsName[fieldParentsName.length - 1] + '[]']
: fieldParentsName;
if (schema.fields) {
return <ObjectSchema {...(this.props as any)} level={this.props.level} />;
return (
<ObjectSchema
{...(this.props as any)}
level={this.props.level}
fieldParentsName={updatedParentsArray}
/>
);
}
if (schema.displayType && !itemsSchema && !minMaxItems.length) {
return (
Expand All @@ -37,7 +47,7 @@ export class ArraySchema extends React.PureComponent<SchemaProps> {
<div>
<ArrayOpenningLabel> Array {minMaxItems}</ArrayOpenningLabel>
<PaddedSchema>
<Schema {...this.props} schema={itemsSchema} />
<Schema {...this.props} schema={itemsSchema} fieldParentsName={updatedParentsArray} />
</PaddedSchema>
<ArrayClosingLabel />
</div>
Expand Down
3 changes: 3 additions & 0 deletions src/components/Schema/ObjectSchema.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export interface ObjectSchemaProps extends SchemaProps {
fieldName: string;
parentSchema: SchemaModel;
};
fieldParentsName?: string[];
}

export const ObjectSchema = observer(
Expand All @@ -26,6 +27,7 @@ export const ObjectSchema = observer(
skipReadOnly,
skipWriteOnly,
level,
fieldParentsName,
}: ObjectSchemaProps) => {
const { expandSingleSchemaField, showObjectSchemaExamples, schemasExpansionLevel } =
React.useContext(OptionsContext);
Expand Down Expand Up @@ -58,6 +60,7 @@ export const ObjectSchema = observer(
isLast={isLast}
field={field}
expandByDefault={expandByDefault}
fieldParentsName={Number(level) > 1 ? fieldParentsName : []}
renderDiscriminatorSwitch={
discriminator?.fieldName === field.name
? () => (
Expand Down
1 change: 1 addition & 0 deletions src/components/Schema/Schema.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export interface SchemaOptions {

export interface SchemaProps extends SchemaOptions {
schema: SchemaModel;
fieldParentsName?: string[];
}

@observer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
"hideDownloadButtons": false,
"hideFab": false,
"hideHostname": false,
"hidePropertiesPrefix": true,
"hideRequestPayloadSample": false,
"hideSchemaPattern": false,
"hideSchemaTitles": false,
Expand Down Expand Up @@ -363,6 +364,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
"hideDownloadButtons": false,
"hideFab": false,
"hideHostname": false,
"hidePropertiesPrefix": true,
"hideRequestPayloadSample": false,
"hideSchemaPattern": false,
"hideSchemaTitles": false,
Expand Down Expand Up @@ -625,6 +627,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
"hideDownloadButtons": false,
"hideFab": false,
"hideHostname": false,
"hidePropertiesPrefix": true,
"hideRequestPayloadSample": false,
"hideSchemaPattern": false,
"hideSchemaTitles": false,
Expand Down Expand Up @@ -949,6 +952,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
"hideDownloadButtons": false,
"hideFab": false,
"hideHostname": false,
"hidePropertiesPrefix": true,
"hideRequestPayloadSample": false,
"hideSchemaPattern": false,
"hideSchemaTitles": false,
Expand Down Expand Up @@ -1236,6 +1240,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
"hideDownloadButtons": false,
"hideFab": false,
"hideHostname": false,
"hidePropertiesPrefix": true,
"hideRequestPayloadSample": false,
"hideSchemaPattern": false,
"hideSchemaTitles": false,
Expand Down Expand Up @@ -1494,6 +1499,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
"hideDownloadButtons": false,
"hideFab": false,
"hideHostname": false,
"hidePropertiesPrefix": true,
"hideRequestPayloadSample": false,
"hideSchemaPattern": false,
"hideSchemaTitles": false,
Expand Down Expand Up @@ -1777,6 +1783,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
"hideDownloadButtons": false,
"hideFab": false,
"hideHostname": false,
"hidePropertiesPrefix": true,
"hideRequestPayloadSample": false,
"hideSchemaPattern": false,
"hideSchemaTitles": false,
Expand Down Expand Up @@ -2090,6 +2097,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
"hideDownloadButtons": false,
"hideFab": false,
"hideHostname": false,
"hidePropertiesPrefix": true,
"hideRequestPayloadSample": false,
"hideSchemaPattern": false,
"hideSchemaTitles": false,
Expand Down Expand Up @@ -2365,6 +2373,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
"hideDownloadButtons": false,
"hideFab": false,
"hideHostname": false,
"hidePropertiesPrefix": true,
"hideRequestPayloadSample": false,
"hideSchemaPattern": false,
"hideSchemaTitles": false,
Expand Down Expand Up @@ -2627,6 +2636,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
"hideDownloadButtons": false,
"hideFab": false,
"hideHostname": false,
"hidePropertiesPrefix": true,
"hideRequestPayloadSample": false,
"hideSchemaPattern": false,
"hideSchemaTitles": false,
Expand Down Expand Up @@ -2955,6 +2965,7 @@ exports[`Components SchemaView discriminator should correctly render discriminat
},
}
}
fieldParentsName={[]}
isLast={false}
key="packSize"
showExamples={false}
Expand Down Expand Up @@ -3029,6 +3040,7 @@ exports[`Components SchemaView discriminator should correctly render discriminat
},
}
}
fieldParentsName={[]}
isLast={true}
key="type"
renderDiscriminatorSwitch={[Function]}
Expand Down
4 changes: 2 additions & 2 deletions src/services/MenuBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class MenuBuilder {
const hasAutogenerated = [...(spec.tags || [])].find(
tag => tag?.name === schemaDefinitionsTagName,
);
console.log('hasAutogenerated', hasAutogenerated, schemaDefinitionsTagName);

if (!hasAutogenerated && schemaDefinitionsTagName) {
tags.push({ name: schemaDefinitionsTagName });
}
Expand All @@ -38,7 +38,7 @@ export class MenuBuilder {
} else {
items.push(...MenuBuilder.getTagsItems(parser, tagsMap, undefined, undefined, options));
}
console.log('items', items);

return items;
}

Expand Down
3 changes: 3 additions & 0 deletions src/services/RedocNormalizedOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export interface RedocRawOptions {
hideFab?: boolean;
minCharacterLengthToInitSearch?: number;
showWebhookVerb?: boolean;
hidePropertiesPrefix?: boolean;
}

export function argValueToBoolean(val?: string | boolean, defaultValue?: boolean): boolean {
Expand Down Expand Up @@ -273,6 +274,7 @@ export class RedocNormalizedOptions {
hideFab: boolean;
minCharacterLengthToInitSearch: number;
showWebhookVerb: boolean;
hidePropertiesPrefix?: boolean;

nonce?: string;

Expand Down Expand Up @@ -358,5 +360,6 @@ export class RedocNormalizedOptions {
this.hideFab = argValueToBoolean(raw.hideFab);
this.minCharacterLengthToInitSearch = argValueToNumber(raw.minCharacterLengthToInitSearch) || 3;
this.showWebhookVerb = argValueToBoolean(raw.showWebhookVerb);
this.hidePropertiesPrefix = argValueToBoolean(raw.hidePropertiesPrefix, true);
}
}

0 comments on commit 93d6b6c

Please sign in to comment.