react-native-navigation的迁移库

OptionsProcessor.ts 1.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import * as _ from 'lodash';
  2. import { processColor } from 'react-native';
  3. import * as resolveAssetSource from 'react-native/Libraries/Image/resolveAssetSource';
  4. export class OptionsProcessor {
  5. constructor(public store, public uniqueIdProvider) { }
  6. public processOptions(options) {
  7. _.forEach(options, (value, key) => {
  8. if (!value) { return; }
  9. this.processComponent(key, value, options);
  10. this.processColor(key, value, options);
  11. this.processImage(key, value, options);
  12. this.processButtonsPassProps(key, value);
  13. if (!_.isEqual(key, 'passProps') && (_.isObject(value) || _.isArray(value))) {
  14. this.processOptions(value);
  15. }
  16. });
  17. }
  18. private processColor(key, value, options) {
  19. if (_.isEqual(key, 'color') || _.endsWith(key, 'Color')) {
  20. options[key] = processColor(value);
  21. }
  22. }
  23. private processImage(key, value, options) {
  24. if (_.isEqual(key, 'icon') || _.isEqual(key, 'image') || _.endsWith(key, 'Icon') || _.endsWith(key, 'Image')) {
  25. options[key] = resolveAssetSource(value);
  26. }
  27. }
  28. private processButtonsPassProps(key, value) {
  29. if (_.endsWith(key, 'Buttons')) {
  30. _.forEach(value, (button) => {
  31. if (button.passProps && button.id) {
  32. this.store.setPropsForId(button.id, button.passProps);
  33. button.passProps = undefined;
  34. }
  35. });
  36. }
  37. }
  38. private processComponent(key, value, options) {
  39. if (_.isEqual(key, 'component')) {
  40. value.componentId = value.id ? value.id : this.uniqueIdProvider.generate('CustomComponent');
  41. if (value.passProps) {
  42. this.store.setPropsForId(value.componentId, value.passProps);
  43. }
  44. options[key] = _.omit(value, 'passProps');
  45. }
  46. }
  47. }