Skip to content

Commit

Permalink
chore: refactor ObjectSchema and Field, less rerenders on expand
Browse files Browse the repository at this point in the history
  • Loading branch information
RomanHotsiy committed Feb 8, 2018
1 parent 1f7fc44 commit 71e189f
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 66 deletions.
46 changes: 33 additions & 13 deletions src/components/Fields/Field.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -25,15 +27,16 @@ export interface FieldProps {
}

export class Field extends React.PureComponent<FieldProps> {
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 ? (
<ClickablePropertyNameCell
onClick={this.props.onClick}
className={deprecated ? 'deprecated' : ''}
>
const paramName = withSubSchema ? (
<ClickablePropertyNameCell onClick={this.toggle} className={deprecated ? 'deprecated' : ''}>
<PropertyBullet />
{name}
<ShelfIcon size={'1.2em'} direction={expanded ? 'down' : 'right'} />
Expand All @@ -47,12 +50,29 @@ export class Field extends React.PureComponent<FieldProps> {
</PropertyNameCell>
);
return (
<tr className={isLast ? 'last ' + className : className}>
{paramName}
<PropertyDetailsCell>
<FieldDetails {...this.props} />
</PropertyDetailsCell>
</tr>
<>
<tr className={isLast ? 'last ' + className : className}>
{paramName}
<PropertyDetailsCell>
<FieldDetails {...this.props} />
</PropertyDetailsCell>
</tr>
{field.expanded &&
withSubSchema && (
<tr key={field.name + 'inner'}>
<PropertyCellWithInner colSpan={2}>
<InnerPropertiesWrap>
<Schema
schema={field.schema}
skipReadOnly={this.props.skipReadOnly}
skipWriteOnly={this.props.skipWriteOnly}
showTitle={this.props.showTitle}
/>
</InnerPropertiesWrap>
</PropertyCellWithInner>
</tr>
)}
</>
);
}
}
79 changes: 28 additions & 51 deletions src/components/Schema/ObjectSchema.tsx
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -28,42 +23,6 @@ export class ObjectSchema extends React.Component<ObjectSchemaProps> {
return this.props.discriminator!.parentSchema;
}

renderField(field: FieldModel, isLast: boolean, isDiscriminator: boolean = false) {
const withSubSchema = !field.schema.isPrimitive && !field.schema.isCircular;
return [
<Field
key={field.name}
isLast={isLast}
field={field}
onClick={(withSubSchema && (() => field.toggle())) || undefined}
renderDiscriminatorSwitch={
(isDiscriminator &&
(() => (
<DiscriminatorDropdown parent={this.parentSchema} enumValues={field.schema.enum} />
))) ||
undefined
}
className={field.expanded ? 'expanded' : undefined}
showExamples={false}
/>,
field.expanded &&
withSubSchema && (
<tr key={field.name + 'inner'}>
<PropertyCellWithInner colSpan={2}>
<InnerPropertiesWrap>
<Schema
schema={field.schema}
skipReadOnly={this.props.skipReadOnly}
skipWriteOnly={this.props.skipWriteOnly}
showTitle={this.props.showTitle}
/>
</InnerPropertiesWrap>
</PropertyCellWithInner>
</tr>
),
];
}

render() {
const { schema: { fields = [] }, showTitle, discriminator } = this.props;

Expand All @@ -82,13 +41,31 @@ export class ObjectSchema extends React.Component<ObjectSchemaProps> {
<PropertiesTable>
{showTitle && <PropertiesTableCaption>{this.props.schema.title}</PropertiesTableCaption>}
<tbody>
{mapWithLast(filteredFields, (field, isLast) =>
this.renderField(
field,
isLast,
discriminator && discriminator.fieldName === field.name,
),
)}
{mapWithLast(filteredFields, (field, isLast) => {
return (
<Field
key={field.name}
isLast={isLast}
field={field}
renderDiscriminatorSwitch={
(discriminator &&
discriminator.fieldName === field.name &&
(() => (
<DiscriminatorDropdown
parent={this.parentSchema}
enumValues={field.schema.enum}
/>
))) ||
undefined
}
className={field.expanded ? 'expanded' : undefined}
showExamples={false}
skipReadOnly={this.props.skipReadOnly}
skipWriteOnly={this.props.skipWriteOnly}
showTitle={this.props.showTitle}
/>
);
})}
</tbody>
</PropertiesTable>
);
Expand Down
7 changes: 5 additions & 2 deletions src/components/Schema/Schema.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<Partial<SchemaProps>> {
render() {
Expand Down

0 comments on commit 71e189f

Please sign in to comment.