@@ -19,20 +19,19 @@ import { GlobalHotKeysHandler } from '../hotkeys/GlobalHotKeysHandler';
1919
2020import { HotKeyBoundActions } from '../hotkeys/HotKeyBoundActions' ;
2121
22- import { ComponentViewerDropDown } from './ComponentViewerDropDown' ;
23-
2422import { ComponentViewerHelp } from './help/ComponentViewerHelp' ;
2523import { globalActionDefaultKeys } from './GlobalActions' ;
2624
2725import { VisualizedActions } from '../actions/VisualizedActions' ;
2826
29- import './ComponentViewer.css' ;
27+ import { labelToKey } from './dropdown/labelUtils' ;
28+ import { DropDowns } from './dropdown/DropDowns' ;
3029
31- import { labelToKey } from './toolbar/labelUtils ' ;
30+ import './ComponentViewer.css ' ;
3231
3332export interface Props {
3433 registries : Registries ;
35- dropDowns : ComponentViewerDropDown [ ] ;
34+ dropDowns : DropDowns ;
3635}
3736
3837class ComponentViewer extends Component < Props , ComponentViewerState > {
@@ -67,7 +66,7 @@ class ComponentViewer extends Component<Props, ComponentViewerState> {
6766 const registry = this . findSelectedRegistry ( ) ;
6867 const demoEntry = registry ? registry . findDemoByName ( demoName ) : null ;
6968
70- const rendered = demoEntry && ( demoEntry . isMiniApp ( ) || isFullScreen ) ?
69+ const rendered = demoEntry && isFullScreen ?
7170 this . renderDemo ( demoEntry , true ) :
7271 this . renderSelectionPanelAndDemo ( demoEntry ) ;
7372
@@ -173,33 +172,23 @@ class ComponentViewer extends Component<Props, ComponentViewerState> {
173172
174173 triggerDropDownSelection ( ) {
175174 const { dropDowns} = this . props ;
176- if ( dropDowns . length === 0 ) {
175+ if ( dropDowns . isEmpty ( ) ) {
177176 return ;
178177 }
179178
180179 const { selectedToolbarItems} = this . state ;
181180 Object . keys ( selectedToolbarItems ) . forEach ( dropDownKey => {
182- const dropDown = dropDownByLabelKey ( dropDownKey ) ;
183- const itemKey = selectedToolbarItems [ dropDownKey ] ;
181+ const dropDown = dropDowns . findDropDownByLabelKey ( dropDownKey ) ;
184182 if ( ! dropDown ) {
185183 return ;
186184 }
187185
188- const itemLabel = dropItemLabelByKey ( ) ;
189- if ( itemLabel ) {
190- dropDown . onSelect ( itemLabel ) ;
191- }
192-
193- function dropItemLabelByKey ( ) {
194- const found = dropDown ! . items . filter ( item => labelToKey ( item . label ) === itemKey ) ;
195- return found . length > 0 ? found [ 0 ] . label : undefined ;
186+ const itemKey = selectedToolbarItems [ dropDownKey ] ;
187+ const item = dropDown . findItemByLabelKey ( itemKey ) ;
188+ if ( item ) {
189+ dropDown . triggerSelectHandler ( item . label ) ;
196190 }
197191 } ) ;
198-
199- function dropDownByLabelKey ( labelKey : string ) {
200- const found = dropDowns . filter ( dd => labelToKey ( dd . label ) === labelKey ) ;
201- return found . length ? found [ 0 ] : undefined ;
202- }
203192 }
204193
205194 componentWillUnmount ( ) {
@@ -210,11 +199,11 @@ class ComponentViewer extends Component<Props, ComponentViewerState> {
210199 const result : HotKeyBoundActions = { } ;
211200
212201 const { dropDowns} = this . props ;
213- if ( dropDowns . length === 0 ) {
202+ if ( dropDowns . isEmpty ( ) ) {
214203 return result ;
215204 }
216205
217- dropDowns . forEach ( dropDown =>
206+ dropDowns . list . forEach ( dropDown =>
218207 dropDown . items
219208 . filter ( item => ! ! item . hotKey )
220209 . forEach ( item => result [ item . hotKey ! ] =
@@ -232,11 +221,11 @@ class ComponentViewer extends Component<Props, ComponentViewerState> {
232221 }
233222
234223 private onFullScreenToggle = ( ) => {
235- this . pushUrl ( { isFullScreen : ! this . state . isFullScreen } ) ;
224+ this . pushUrl ( { partialState : { isFullScreen : ! this . state . isFullScreen } } ) ;
236225 }
237226
238227 private onQuestionMarkToggle = ( ) => {
239- this . pushUrl ( { isHelpOn : ! this . state . isHelpOn } ) ;
228+ this . pushUrl ( { partialState : { isHelpOn : ! this . state . isHelpOn } } ) ;
240229 }
241230
242231 private onNextDemo = ( ) => {
@@ -291,35 +280,35 @@ class ComponentViewer extends Component<Props, ComponentViewerState> {
291280 }
292281
293282 private selectRegistry = ( registryName : string ) => {
294- this . pushUrl ( {
283+ this . pushUrl ( { partialState : {
295284 registryName,
296285 demoName : '' ,
297286 entryTitle : ''
298- } ) ;
287+ } , clearPath : true } ) ;
299288 }
300289
301290 private selectDemo = ( demoName : string ) => {
302- this . pushUrl ( { demoName, entryTitle : '' } ) ;
291+ this . pushUrl ( { partialState : { demoName, entryTitle : '' } , clearPath : true } ) ;
303292 }
304293
305294 private selectInstanceByTitle = ( title : string ) => {
306- this . pushUrl ( { entryTitle : title } ) ;
295+ this . pushUrl ( { partialState : { entryTitle : title } , clearPath : true } ) ;
307296 }
308297
309298 private selectToolbarItem = ( dropDownLabel : string , itemLabel : string ) => {
310299 const { dropDowns} = this . props ;
311300 const { selectedToolbarItems} = this . state ;
312301
313- this . pushUrl ( {
302+ this . pushUrl ( { partialState : {
314303 selectedToolbarItems : {
315304 ...selectedToolbarItems ,
316305 [ labelToKey ( dropDownLabel ) ] : labelToKey ( itemLabel )
317306 }
318- } ) ;
307+ } } ) ;
319308
320- const found = dropDowns . filter ( dd => dd . label === dropDownLabel ) ;
321- if ( found . length > 0 ) {
322- found [ 0 ] . onSelect ( itemLabel ) ;
309+ const found = dropDowns . findDropDownByLabel ( dropDownLabel ) ;
310+ if ( found ) {
311+ found . triggerSelectHandler ( itemLabel ) ;
323312 }
324313 }
325314
@@ -339,10 +328,10 @@ class ComponentViewer extends Component<Props, ComponentViewerState> {
339328 [ ] ;
340329 }
341330
342- private pushUrl ( newState : Partial < ComponentViewerState > ) {
343- const fullState = { ...this . state , ...newState } ;
331+ private pushUrl ( { partialState , clearPath } : { partialState : Partial < ComponentViewerState > , clearPath ?: boolean } ) {
332+ const fullState = { ...this . state , ...partialState } ;
344333 const searchParams = this . stateCreator . buildUrlSearchParams ( { ...fullState , filterText : '' } ) ;
345- const newUrl = '?' + searchParams ;
334+ const newUrl = ( clearPath ? '/' : '' ) + '?' + searchParams ;
346335
347336 window . history . pushState ( { } , '' , newUrl ) ;
348337 this . updateStateFromUrl ( ) ;
0 commit comments