diff --git a/lib/src/commands/OptionsProcessor.test.ts b/lib/src/commands/OptionsProcessor.test.ts index 0e85221cefc..2cad86097ac 100644 --- a/lib/src/commands/OptionsProcessor.test.ts +++ b/lib/src/commands/OptionsProcessor.test.ts @@ -3,7 +3,7 @@ import { UniqueIdProvider } from '../adapters/UniqueIdProvider'; import { Store } from '../components/Store'; import { OptionProcessorsStore } from '../processors/OptionProcessorsStore'; import { Options, OptionsModalPresentationStyle } from '../interfaces/Options'; -import { mock, when, anyString, instance, anyNumber, verify } from 'ts-mockito'; +import { mock, when, instance, anyNumber, verify } from 'ts-mockito'; import { ColorService } from '../adapters/ColorService'; import { AssetService } from '../adapters/AssetResolver'; import { Deprecations } from './Deprecations'; @@ -25,7 +25,9 @@ describe('navigation options', () => { const assetService = instance(mockedAssetService); const mockedColorService = mock(ColorService) as ColorService; - when(mockedColorService.toNativeColor(anyString())).thenReturn(666); + when(mockedColorService.toNativeColor('red')).thenReturn(0xffff0000); + when(mockedColorService.toNativeColor('green')).thenReturn(0xff00ff00); + when(mockedColorService.toNativeColor('blue')).thenReturn(0xff0000ff); const colorService = instance(mockedColorService); optionProcessorsRegistry = new OptionProcessorsStore(); uut = new OptionsProcessor( @@ -172,8 +174,8 @@ describe('navigation options', () => { }; uut.processOptions(options, CommandName.SetRoot); expect(options).toEqual({ - statusBar: { backgroundColor: 666 }, - topBar: { background: { color: 666 } }, + statusBar: { backgroundColor: 0xffff0000 }, + topBar: { background: { color: 0xff0000ff } }, }); }); @@ -279,4 +281,41 @@ describe('navigation options', () => { } ); }); + + it('transform searchBar bool to object', () => { + const options = { topBar: { searchBar: true as any } }; + uut.processOptions(options, CommandName.SetRoot); + expect(options.topBar.searchBar).toStrictEqual({ + visible: true, + hideOnScroll: false, + hideTopBarOnFocus: false, + obscuresBackgroundDuringPresentation: false, + backgroundColor: null, + tintColor: null, + placeholder: '', + }); + }); + + it('transform searchBar bool to object and merges in deprecated values', () => { + const options = { + topBar: { + searchBar: true as any, + searchBarHiddenWhenScrolling: true, + hideNavBarOnFocusSearchBar: true, + searchBarBackgroundColor: 'red', + searchBarTintColor: 'green', + searchBarPlaceholder: 'foo', + }, + }; + uut.processOptions(options, CommandName.SetRoot); + expect(options.topBar.searchBar).toStrictEqual({ + visible: true, + hideOnScroll: true, + hideTopBarOnFocus: true, + obscuresBackgroundDuringPresentation: false, + backgroundColor: 0xffff0000, + tintColor: 0xff00ff00, + placeholder: 'foo', + }); + }); }); diff --git a/lib/src/commands/OptionsProcessor.ts b/lib/src/commands/OptionsProcessor.ts index 9b0799b6d32..1b5ad122fdd 100644 --- a/lib/src/commands/OptionsProcessor.ts +++ b/lib/src/commands/OptionsProcessor.ts @@ -10,7 +10,7 @@ import { Store } from '../components/Store'; import { UniqueIdProvider } from '../adapters/UniqueIdProvider'; import { ColorService } from '../adapters/ColorService'; import { AssetService } from '../adapters/AssetResolver'; -import { Options } from '../interfaces/Options'; +import { Options, OptionsSearchBar, OptionsTopBar } from '../interfaces/Options'; import { Deprecations } from './Deprecations'; import { OptionProcessorsStore } from '../processors/OptionProcessorsStore'; import { CommandName } from '../interfaces/CommandName'; @@ -49,7 +49,7 @@ export class OptionsProcessor { } private processObject( - objectToProcess: object, + objectToProcess: Record, parentOptions: object, onProcess: (key: string, parentOptions: object) => void, commandName: CommandName, @@ -72,8 +72,9 @@ export class OptionsProcessor { onProcess(key, parentOptions); - if (!isEqual(key, 'passProps') && (isObject(value) || isArray(value))) { - this.processObject(value, parentOptions, onProcess, commandName, objectPath); + const processedValue = objectToProcess[key]; + if (!isEqual(key, 'passProps') && (isObject(processedValue) || isArray(processedValue))) { + this.processObject(processedValue, parentOptions, onProcess, commandName, objectPath); } }); } @@ -138,30 +139,34 @@ export class OptionsProcessor { } } - private processSearchBar(key: string, value: any, options: Record) { - if (isEqual(key, 'searchBar')) { - typeof value === 'boolean' && this.deprecations.onProcessOptions(key, options, ''); + private processSearchBar(key: string, value: OptionsSearchBar | boolean, options: OptionsTopBar) { + if (key !== 'searchBar') { + return; + } + + const deprecatedSearchBarOptions: OptionsSearchBar = { + visible: false, + hideOnScroll: options.searchBarHiddenWhenScrolling ?? false, + hideTopBarOnFocus: options.hideNavBarOnFocusSearchBar ?? false, + obscuresBackgroundDuringPresentation: false, + backgroundColor: options.searchBarBackgroundColor, + tintColor: options.searchBarTintColor, + placeholder: options.searchBarPlaceholder ?? '', + }; + + if (typeof value === 'boolean') { + // Deprecated + this.deprecations.onProcessOptions(key, options, ''); + + options[key] = { + ...deprecatedSearchBarOptions, + visible: value, + }; + } else { options[key] = { - ...options[key], - visible: options[key].visible ?? value, - hiddenWhenScrolling: - options[key].hiddenWhenScrolling ?? options.searchBarHiddenWhenScrolling ?? false, - hideTopBarOnFocus: - options[key].hideTopBarOnFocus ?? options.hideNavBarOnFocusSearchBar ?? false, - obscuresBackgroundDuringPresentation: - options[key].obscuresBackgroundDuringPresentation ?? false, - placeholder: options[key].placeholder ?? options.searchBarPlaceholder ?? '', + ...deprecatedSearchBarOptions, + ...value, }; - this.processColor( - 'backgroundColor', - options[key].backgroundColor ?? options.searchBarBackgroundColor, - options[key] - ); - this.processColor( - 'tintColor', - options[key].tintColor ?? options.searchBarTintColor, - options[key] - ); } }