mirror of
https://github.com/Redocly/redoc.git
synced 2024-11-11 03:16:48 +03:00
feat: support examples in object schema (#1832)
* feat: support examples in object schema * revert pure components used with observer * rename option * update test
This commit is contained in:
parent
6c41e95aa0
commit
c986f0ef1a
|
@ -18,37 +18,35 @@ export interface ObjectSchemaProps extends SchemaProps {
|
|||
};
|
||||
}
|
||||
|
||||
@observer
|
||||
export class ObjectSchema extends React.Component<ObjectSchemaProps> {
|
||||
static contextType = OptionsContext;
|
||||
|
||||
get parentSchema() {
|
||||
return this.props.discriminator!.parentSchema;
|
||||
}
|
||||
|
||||
render() {
|
||||
const {
|
||||
schema: { fields = [] },
|
||||
export const ObjectSchema = observer(
|
||||
({
|
||||
schema: { fields = [], title },
|
||||
showTitle,
|
||||
discriminator,
|
||||
} = this.props;
|
||||
skipReadOnly,
|
||||
skipWriteOnly,
|
||||
}: ObjectSchemaProps) => {
|
||||
const { expandSingleSchemaField, showObjectSchemaExamples } = React.useContext(OptionsContext);
|
||||
|
||||
const needFilter = this.props.skipReadOnly || this.props.skipWriteOnly;
|
||||
|
||||
const filteredFields = needFilter
|
||||
? fields.filter(item => {
|
||||
return !(
|
||||
(this.props.skipReadOnly && item.schema.readOnly) ||
|
||||
(this.props.skipWriteOnly && item.schema.writeOnly)
|
||||
const filteredFields = React.useMemo(
|
||||
() =>
|
||||
skipReadOnly || skipWriteOnly
|
||||
? fields.filter(
|
||||
item =>
|
||||
!(
|
||||
(skipReadOnly && item.schema.readOnly) ||
|
||||
(skipWriteOnly && item.schema.writeOnly)
|
||||
),
|
||||
)
|
||||
: fields,
|
||||
[skipReadOnly, skipWriteOnly, fields],
|
||||
);
|
||||
})
|
||||
: fields;
|
||||
|
||||
const expandByDefault = this.context.expandSingleSchemaField && filteredFields.length === 1;
|
||||
const expandByDefault = expandSingleSchemaField && filteredFields.length === 1;
|
||||
|
||||
return (
|
||||
<PropertiesTable>
|
||||
{showTitle && <PropertiesTableCaption>{this.props.schema.title}</PropertiesTableCaption>}
|
||||
{showTitle && <PropertiesTableCaption>{title}</PropertiesTableCaption>}
|
||||
<tbody>
|
||||
{mapWithLast(filteredFields, (field, isLast) => {
|
||||
return (
|
||||
|
@ -58,26 +56,25 @@ export class ObjectSchema extends React.Component<ObjectSchemaProps> {
|
|||
field={field}
|
||||
expandByDefault={expandByDefault}
|
||||
renderDiscriminatorSwitch={
|
||||
(discriminator &&
|
||||
discriminator.fieldName === field.name &&
|
||||
(() => (
|
||||
discriminator?.fieldName === field.name
|
||||
? () => (
|
||||
<DiscriminatorDropdown
|
||||
parent={this.parentSchema}
|
||||
parent={discriminator!.parentSchema}
|
||||
enumValues={field.schema.enum}
|
||||
/>
|
||||
))) ||
|
||||
undefined
|
||||
)
|
||||
: undefined
|
||||
}
|
||||
className={field.expanded ? 'expanded' : undefined}
|
||||
showExamples={false}
|
||||
skipReadOnly={this.props.skipReadOnly}
|
||||
skipWriteOnly={this.props.skipWriteOnly}
|
||||
showTitle={this.props.showTitle}
|
||||
showExamples={showObjectSchemaExamples}
|
||||
skipReadOnly={skipReadOnly}
|
||||
skipWriteOnly={skipWriteOnly}
|
||||
showTitle={showTitle}
|
||||
/>
|
||||
);
|
||||
})}
|
||||
</tbody>
|
||||
</PropertiesTable>
|
||||
);
|
||||
}
|
||||
}
|
||||
},
|
||||
);
|
||||
|
|
|
@ -26,7 +26,7 @@ describe('Components', () => {
|
|||
options,
|
||||
);
|
||||
const schemaViewElement = shallow(<Schema schema={schema} />).getElement();
|
||||
expect(schemaViewElement.type).toEqual(ObjectSchema);
|
||||
expect(schemaViewElement).toMatchSnapshot();
|
||||
expect(schemaViewElement.props.discriminator).toBeDefined();
|
||||
expect(schemaViewElement.props.discriminator.parentSchema).toBeDefined();
|
||||
expect(schemaViewElement.props.discriminator.fieldName).toEqual('type');
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -35,6 +35,7 @@ export interface RedocRawOptions {
|
|||
simpleOneOfTypeLabel?: boolean | string;
|
||||
payloadSampleIdx?: number;
|
||||
expandSingleSchemaField?: boolean | string;
|
||||
showObjectSchemaExamples?: boolean | string;
|
||||
|
||||
unstable_ignoreMimeParameters?: boolean;
|
||||
|
||||
|
@ -220,6 +221,7 @@ export class RedocNormalizedOptions {
|
|||
simpleOneOfTypeLabel: boolean;
|
||||
payloadSampleIdx: number;
|
||||
expandSingleSchemaField: boolean;
|
||||
showObjectSchemaExamples: boolean;
|
||||
|
||||
/* tslint:disable-next-line */
|
||||
unstable_ignoreMimeParameters: boolean;
|
||||
|
@ -281,6 +283,7 @@ export class RedocNormalizedOptions {
|
|||
this.simpleOneOfTypeLabel = argValueToBoolean(raw.simpleOneOfTypeLabel);
|
||||
this.payloadSampleIdx = RedocNormalizedOptions.normalizePayloadSampleIdx(raw.payloadSampleIdx);
|
||||
this.expandSingleSchemaField = argValueToBoolean(raw.expandSingleSchemaField);
|
||||
this.showObjectSchemaExamples = argValueToBoolean(raw.showObjectSchemaExamples);
|
||||
|
||||
this.unstable_ignoreMimeParameters = argValueToBoolean(raw.unstable_ignoreMimeParameters);
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user