From 7cc10f74809c289fd359f58c34718761c63e68f7 Mon Sep 17 00:00:00 2001 From: Alex Varchuk Date: Tue, 25 Jan 2022 20:59:01 +0200 Subject: [PATCH 1/2] fix: expanded observable --- src/components/Fields/Field.tsx | 126 ++++++++++++++++---------------- 1 file changed, 63 insertions(+), 63 deletions(-) diff --git a/src/components/Fields/Field.tsx b/src/components/Fields/Field.tsx index 2fb5865fdc..c4449f1cde 100644 --- a/src/components/Fields/Field.tsx +++ b/src/components/Fields/Field.tsx @@ -28,79 +28,79 @@ export interface FieldProps extends SchemaOptions { renderDiscriminatorSwitch?: (opts: FieldProps) => JSX.Element; } -@observer -export class Field extends React.Component { - toggle = () => { - if (this.props.field.expanded === undefined && this.props.expandByDefault) { - this.props.field.collapse(); +export const Field = observer((fieldProps: FieldProps) => { + const { + className, + isLast, + field, + expandByDefault, + skipReadOnly, + skipWriteOnly, + showTitle, + level, + } = fieldProps; + const [expanded, setExpanded] = React.useState( + field.expanded === undefined ? expandByDefault : field.expanded, + ); + const toggle = () => { + if (field.expanded === undefined && expandByDefault) { + field.collapse(); } else { - this.props.field.toggle(); + field.toggle(); } + setExpanded(field.expanded); }; - handleKeyPress = e => { + const handleKeyPress = e => { if (e.key === 'Enter') { e.preventDefault(); - this.toggle(); + toggle(); } }; - render() { - const { className, field, isLast, expandByDefault } = this.props; - const { name, deprecated, required, kind } = field; - const withSubSchema = !field.schema.isPrimitive && !field.schema.isCircular; + 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 ? ( - - - - {required && required } - - ) : ( - - + const paramName = withSubSchema ? ( + + + + {required && required } + + ) : ( + + + {name} + {required && required } + + ); - return ( - <> - - {paramName} - - - + return ( + <> + + {paramName} + + + + + {expanded && withSubSchema && ( + + + + + + - {expanded && withSubSchema && ( - - - - - - - - )} - - ); - } -} + )} + + ); +}); From 42dc359ec54d804fc211a005302ac932260d84b0 Mon Sep 17 00:00:00 2001 From: Alex Varchuk Date: Tue, 25 Jan 2022 21:11:43 +0200 Subject: [PATCH 2/2] chore: update snapshot --- .../__snapshots__/DiscriminatorDropdown.test.tsx.snap | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/components/__tests__/__snapshots__/DiscriminatorDropdown.test.tsx.snap b/src/components/__tests__/__snapshots__/DiscriminatorDropdown.test.tsx.snap index 623b783e7d..dc71f95139 100644 --- a/src/components/__tests__/__snapshots__/DiscriminatorDropdown.test.tsx.snap +++ b/src/components/__tests__/__snapshots__/DiscriminatorDropdown.test.tsx.snap @@ -2474,7 +2474,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView exports[`Components SchemaView discriminator should correctly render discriminator dropdown 1`] = ` - -