1
1
import React , { Component } from 'react' ;
2
- import { withRouter } from 'react-router-dom' ;
2
+ import { withRouter , Redirect } from 'react-router-dom' ;
3
3
import Grid from '@material-ui/core/Grid' ;
4
4
import FormRenderer , { componentTypes } from '@data-driven-forms/react-form-renderer' ;
5
5
import { formFieldsMapper , layoutMapper } from '@data-driven-forms/mui-component-mapper' ;
@@ -103,31 +103,13 @@ class ComponentExample extends Component {
103
103
constructor ( props ) {
104
104
super ( props ) ;
105
105
const baseStructure = baseExamples . find ( item => item . component === props . match . params . component ) ;
106
- this . state = {
107
- ...baseStructure ,
108
- variants : [
109
- ...baseStructure . variants ,
110
- { name : 'name' , title : 'Name' , value : baseStructure . value . fields [ 0 ] . name , component : 'input' } ,
111
- ...( baseStructure . canBeRequired ? [ {
112
- name : 'isRequired' ,
113
- title : 'Required' ,
114
- validate : [ {
115
- type : validatorTypes . REQUIRED ,
116
- } ] ,
117
- } ] : [ ] ) ,
118
- ] ,
119
- value : JSON . stringify ( baseStructure . value , null , 2 ) ,
120
- parsedSchema : baseStructure . value ,
121
- activeMapper : 'mui' ,
122
- frameHeight : 360 ,
123
- openTooltip : false ,
124
- } ;
125
- }
126
-
127
- componentDidUpdate ( { match : { params : { component } } } ) {
128
- if ( component !== this . props . match . params . component ) {
129
- const baseStructure = baseExamples . find ( item => item . component === this . props . match . params . component ) ;
130
- this . setState ( {
106
+ if ( ! baseStructure ) {
107
+ this . state = {
108
+ notFound : true ,
109
+ component : props . match . params . component ,
110
+ } ;
111
+ } else {
112
+ this . state = {
131
113
...baseStructure ,
132
114
variants : [
133
115
...baseStructure . variants ,
@@ -142,7 +124,39 @@ class ComponentExample extends Component {
142
124
] ,
143
125
value : JSON . stringify ( baseStructure . value , null , 2 ) ,
144
126
parsedSchema : baseStructure . value ,
145
- } ) ;
127
+ activeMapper : 'mui' ,
128
+ frameHeight : 360 ,
129
+ openTooltip : false ,
130
+ } ;
131
+ }
132
+ }
133
+
134
+ componentDidUpdate ( { match : { params : { component } } } ) {
135
+ if ( component !== this . props . match . params . component ) {
136
+ window . scrollTo ( { top : 0 , left : 0 , behavior : 'smooth' } ) ;
137
+ const baseStructure = baseExamples . find ( item => item . component === this . props . match . params . component ) ;
138
+ if ( baseStructure ) {
139
+ this . setState ( {
140
+ component : undefined ,
141
+ notFound : undefined ,
142
+ ...baseStructure ,
143
+ variants : [
144
+ ...baseStructure . variants ,
145
+ { name : 'name' , title : 'Name' , value : baseStructure . value . fields [ 0 ] . name , component : 'input' } ,
146
+ ...( baseStructure . canBeRequired ? [ {
147
+ name : 'isRequired' ,
148
+ title : 'Required' ,
149
+ validate : [ {
150
+ type : validatorTypes . REQUIRED ,
151
+ } ] ,
152
+ } ] : [ ] ) ,
153
+ ] ,
154
+ value : JSON . stringify ( baseStructure . value , null , 2 ) ,
155
+ parsedSchema : baseStructure . value ,
156
+ } ) ;
157
+ } else {
158
+ this . setState ( { notFound : true , component : this . props . match . params . component } ) ;
159
+ }
146
160
}
147
161
}
148
162
@@ -254,7 +268,10 @@ class ComponentExample extends Component {
254
268
255
269
}
256
270
render ( ) {
257
- const { value, parsedSchema, linkText, ContentText, activeMapper, component, openTooltip, variants } = this . state ;
271
+ const { value, parsedSchema, linkText, ContentText, activeMapper, component, openTooltip, variants, notFound } = this . state ;
272
+ if ( notFound || parsedSchema === undefined ) {
273
+ return < Redirect to = "/not-found" /> ;
274
+ }
258
275
259
276
const editedValue = value . replace ( / ^ { \n { 2 } " f i e l d s " : \[ \n / , '' )
260
277
. replace ( / { 2 } \] \n } $ / , '' )
@@ -266,7 +283,7 @@ class ComponentExample extends Component {
266
283
< Grid
267
284
container
268
285
direction = "row"
269
- spacing = { 4 }
286
+ spacing = { 4 }
270
287
>
271
288
< Grid item xs = { 12 } >
272
289
< Typography variant = "h4" gutterBottom >
0 commit comments