react-native-navigation的迁移库

OptionsProcessor.ts 1.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. }
  34. });
  35. }
  36. }
  37. private processComponent(key, value, options) {
  38. if (_.isEqual(key, 'component')) {
  39. value.componentId = value.id ? value.id : this.uniqueIdProvider.generate('CustomComponent');
  40. if (value.passProps) {
  41. this.store.setPropsForId(value.componentId, value.passProps);
  42. }
  43. options[key] = _.omit(value, 'passProps');
  44. }
  45. }
  46. }