Skip to content

Commit

Permalink
feat: new option expandSingleSchemaField
Browse files Browse the repository at this point in the history
  • Loading branch information
RomanHotsiy committed Mar 16, 2020
1 parent 373f018 commit 7608800
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ You can use all of the following options with standalone version on <redoc> tag
* `hideHostname` - if set, the protocol and hostname is not shown in the operation definition.
* `hideLoading` - do not show loading animation. Useful for small docs.
* `hideSingleRequestSampleTab` - do not show the request sample tab for requests with only one sample.
* `expandSingleSchemaField` - automatically expand single field in a schema
* `jsonSampleExpandLevel` - set the default expand level for JSON payload samples (responses and request body). Special value 'all' expands all levels. The default value is `2`.
* `lazyRendering` - _Not implemented yet_ ~~if set, enables lazy rendering mode in ReDoc. This mode is useful for APIs with big number of operations (e.g. > 50). In this mode ReDoc shows initial screen ASAP and then renders the rest operations asynchronously while showing progress bar on the top. Check out the [demo](\\redocly.github.io/redoc) for the example.~~
* `menuToggle` - if true clicking second time on expanded menu item will collapse it, default `false`.
Expand Down
15 changes: 11 additions & 4 deletions src/components/Fields/Field.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,27 @@ export interface FieldProps extends SchemaOptions {
showExamples?: boolean;

field: FieldModel;
expandByDefault?: boolean;

renderDiscriminatorSwitch?: (opts: FieldProps) => JSX.Element;
}

@observer
export class Field extends React.Component<FieldProps> {
toggle = () => {
this.props.field.toggle();
if (this.props.field.expanded === undefined && this.props.expandByDefault) {
this.props.field.expanded = false;
} else {
this.props.field.toggle();
}
};
render() {
const { className, field, isLast } = this.props;
const { name, expanded, deprecated, required, kind } = field;
const { className, field, isLast, expandByDefault } = 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 paramName = withSubSchema ? (
<ClickablePropertyNameCell
onClick={this.toggle}
Expand Down Expand Up @@ -65,7 +72,7 @@ export class Field extends React.Component<FieldProps> {
<FieldDetails {...this.props} />
</PropertyDetailsCell>
</tr>
{field.expanded && withSubSchema && (
{expanded && withSubSchema && (
<tr key={field.name + 'inner'}>
<PropertyCellWithInner colSpan={2}>
<InnerPropertiesWrap>
Expand Down
6 changes: 6 additions & 0 deletions src/components/Schema/ObjectSchema.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { DiscriminatorDropdown } from './DiscriminatorDropdown';
import { SchemaProps } from './Schema';

import { mapWithLast } from '../../utils';
import { OptionsContext } from '../OptionsProvider';

export interface ObjectSchemaProps extends SchemaProps {
discriminator?: {
Expand All @@ -19,6 +20,8 @@ export interface ObjectSchemaProps extends SchemaProps {

@observer
export class ObjectSchema extends React.Component<ObjectSchemaProps> {
static contextType = OptionsContext;

get parentSchema() {
return this.props.discriminator!.parentSchema;
}
Expand All @@ -41,6 +44,8 @@ export class ObjectSchema extends React.Component<ObjectSchemaProps> {
})
: fields;

const expandByDefault = this.context.expandSingleSchemaField && filteredFields.length === 1;

return (
<PropertiesTable>
{showTitle && <PropertiesTableCaption>{this.props.schema.title}</PropertiesTableCaption>}
Expand All @@ -51,6 +56,7 @@ export class ObjectSchema extends React.Component<ObjectSchemaProps> {
key={field.name}
isLast={isLast}
field={field}
expandByDefault={expandByDefault}
renderDiscriminatorSwitch={
(discriminator &&
discriminator.fieldName === field.name &&
Expand Down
3 changes: 3 additions & 0 deletions src/services/RedocNormalizedOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export interface RedocRawOptions {
jsonSampleExpandLevel?: number | string | 'all';
hideSchemaTitles?: boolean | string;
payloadSampleIdx?: number;
expandSingleSchemaField?: boolean | string;

unstable_ignoreMimeParameters?: boolean;

Expand Down Expand Up @@ -160,6 +161,7 @@ export class RedocNormalizedOptions {
enumSkipQuotes: boolean;
hideSchemaTitles: boolean;
payloadSampleIdx: number;
expandSingleSchemaField: boolean;

/* tslint:disable-next-line */
unstable_ignoreMimeParameters: boolean;
Expand Down Expand Up @@ -200,6 +202,7 @@ export class RedocNormalizedOptions {
this.enumSkipQuotes = argValueToBoolean(raw.enumSkipQuotes);
this.hideSchemaTitles = argValueToBoolean(raw.hideSchemaTitles);
this.payloadSampleIdx = RedocNormalizedOptions.normalizePayloadSampleIdx(raw.payloadSampleIdx);
this.expandSingleSchemaField = argValueToBoolean(raw.expandSingleSchemaField);

this.unstable_ignoreMimeParameters = argValueToBoolean(raw.unstable_ignoreMimeParameters);

Expand Down
2 changes: 1 addition & 1 deletion src/services/models/Field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function getDefaultStyleValue(parameterLocation: OpenAPIParameterLocation): Open
*/
export class FieldModel {
@observable
expanded: boolean = false;
expanded: boolean | undefined;

schema: SchemaModel;
name: string;
Expand Down

0 comments on commit 7608800

Please sign in to comment.