From 71e189ffc570e3e1c62e5f65c14e6391d53363ef Mon Sep 17 00:00:00 2001 From: Roman Hotsiy Date: Thu, 8 Feb 2018 11:40:43 +0200 Subject: [PATCH] chore: refactor ObjectSchema and Field, less rerenders on expand --- src/components/Fields/Field.tsx | 46 ++++++++++----- src/components/Schema/ObjectSchema.tsx | 79 +++++++++----------------- src/components/Schema/Schema.tsx | 7 ++- 3 files changed, 66 insertions(+), 66 deletions(-) diff --git a/src/components/Fields/Field.tsx b/src/components/Fields/Field.tsx index 77cd4fed..670e63bb 100644 --- a/src/components/Fields/Field.tsx +++ b/src/components/Fields/Field.tsx @@ -7,15 +7,17 @@ import { PropertyBullet, PropertyDetailsCell, PropertyNameCell, + InnerPropertiesWrap, + PropertyCellWithInner, } from '../../common-elements/fields-layout'; import { ShelfIcon } from '../../common-elements/'; import { FieldModel } from '../../services/models'; +import { Schema, SchemaOptions } from '../Schema/Schema'; -export interface FieldProps { +export interface FieldProps extends SchemaOptions { className?: string; - onClick?: () => void; isLast?: boolean; showExamples?: boolean; @@ -25,15 +27,16 @@ export interface FieldProps { } export class Field extends React.PureComponent { + toggle = () => { + this.props.field.toggle(); + }; render() { const { className, field, isLast } = this.props; const { name, expanded, deprecated, required } = field; + const withSubSchema = !field.schema.isPrimitive && !field.schema.isCircular; - const paramName = this.props.onClick ? ( - + const paramName = withSubSchema ? ( + {name} @@ -47,12 +50,29 @@ export class Field extends React.PureComponent { ); return ( - - {paramName} - - - - + <> + + {paramName} + + + + + {field.expanded && + withSubSchema && ( + + + + + + + + )} + ); } } diff --git a/src/components/Schema/ObjectSchema.tsx b/src/components/Schema/ObjectSchema.tsx index 1f0dde68..5f426095 100644 --- a/src/components/Schema/ObjectSchema.tsx +++ b/src/components/Schema/ObjectSchema.tsx @@ -1,17 +1,12 @@ import { observer } from 'mobx-react'; import * as React from 'react'; -import { FieldModel, SchemaModel } from '../../services/models'; +import { SchemaModel } from '../../services/models'; -import { - InnerPropertiesWrap, - PropertiesTable, - PropertiesTableCaption, - PropertyCellWithInner, -} from '../../common-elements/fields-layout'; +import { PropertiesTable, PropertiesTableCaption } from '../../common-elements/fields-layout'; import { Field } from '../Fields/Field'; import { DiscriminatorDropdown } from './DiscriminatorDropdown'; -import { Schema, SchemaProps } from './Schema'; +import { SchemaProps } from './Schema'; import { mapWithLast } from '../../utils'; @@ -28,42 +23,6 @@ export class ObjectSchema extends React.Component { return this.props.discriminator!.parentSchema; } - renderField(field: FieldModel, isLast: boolean, isDiscriminator: boolean = false) { - const withSubSchema = !field.schema.isPrimitive && !field.schema.isCircular; - return [ - field.toggle())) || undefined} - renderDiscriminatorSwitch={ - (isDiscriminator && - (() => ( - - ))) || - undefined - } - className={field.expanded ? 'expanded' : undefined} - showExamples={false} - />, - field.expanded && - withSubSchema && ( - - - - - - - - ), - ]; - } - render() { const { schema: { fields = [] }, showTitle, discriminator } = this.props; @@ -82,13 +41,31 @@ export class ObjectSchema extends React.Component { {showTitle && {this.props.schema.title}} - {mapWithLast(filteredFields, (field, isLast) => - this.renderField( - field, - isLast, - discriminator && discriminator.fieldName === field.name, - ), - )} + {mapWithLast(filteredFields, (field, isLast) => { + return ( + ( + + ))) || + undefined + } + className={field.expanded ? 'expanded' : undefined} + showExamples={false} + skipReadOnly={this.props.skipReadOnly} + skipWriteOnly={this.props.skipWriteOnly} + showTitle={this.props.showTitle} + /> + ); + })} ); diff --git a/src/components/Schema/Schema.tsx b/src/components/Schema/Schema.tsx index 012164b0..3d3ab178 100644 --- a/src/components/Schema/Schema.tsx +++ b/src/components/Schema/Schema.tsx @@ -10,13 +10,16 @@ import { ArraySchema } from './ArraySchema'; import { ObjectSchema } from './ObjectSchema'; import { OneOfSchema } from './OneOfSchema'; -export interface SchemaProps { - schema: SchemaModel; +export interface SchemaOptions { showTitle?: boolean; skipReadOnly?: boolean; skipWriteOnly?: boolean; } +export interface SchemaProps extends SchemaOptions { + schema: SchemaModel; +} + @observer export class Schema extends React.Component> { render() {