您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

AspectRatioAware.js 2.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // @flow
  2. import PropTypes from 'prop-types';
  3. import React, { Component } from 'react';
  4. import { connect } from 'react-redux';
  5. import { ASPECT_RATIO_NARROW, ASPECT_RATIO_WIDE } from '../constants';
  6. /**
  7. * Determines whether a specific React {@code Component} decorated into an
  8. * {@link AspectRatioAware} has {@link ASPECT_RATIO_NARROW} as the value of its
  9. * {@code aspectRatio} React prop.
  10. *
  11. * @param {AspectRatioAware} component - An {@link AspectRatioAware} which may
  12. * have an {@code aspectRatio} React prop.
  13. * @returns {boolean}
  14. */
  15. export function isNarrowAspectRatio(component: React$Component<*>) {
  16. return component.props.aspectRatio === ASPECT_RATIO_NARROW;
  17. }
  18. /**
  19. * Decorates a specific React {@code Component} class into an
  20. * {@link AspectRatioAware} which provides the React prop {@code aspectRatio}
  21. * updated on each redux state change.
  22. *
  23. * @param {Class<React$Component>} WrappedComponent - A React {@code Component}
  24. * class to be wrapped.
  25. * @returns {AspectRatioAwareWrapper}
  26. */
  27. export function makeAspectRatioAware(
  28. WrappedComponent: Class<React$Component<*>>
  29. ): Class<React$Component<*>> {
  30. /**
  31. * Renders {@code WrappedComponent} with the React prop {@code aspectRatio}.
  32. */
  33. class AspectRatioAware extends Component<*> {
  34. /**
  35. * Properties of the aspect ratio aware wrapper.
  36. */
  37. static propTypes = {
  38. /**
  39. * Either {@link ASPECT_RATIO_NARROW} or {@link ASPECT_RATIO_WIDE}.
  40. */
  41. aspectRatio: PropTypes.oneOf([
  42. ASPECT_RATIO_NARROW,
  43. ASPECT_RATIO_WIDE
  44. ])
  45. }
  46. /**
  47. * Implement's React render method to wrap the nested component.
  48. *
  49. * @returns {React$Element}
  50. */
  51. render(): React$Element<*> {
  52. return <WrappedComponent { ...this.props } />;
  53. }
  54. }
  55. return connect(_mapStateToProps)(AspectRatioAware);
  56. }
  57. /**
  58. * Maps (parts of) the redux state to {@link AspectRatioAware} props.
  59. *
  60. * @param {Object} state - The whole redux state.
  61. * @private
  62. * @returns {{
  63. * aspectRatio: Symbol
  64. * }}
  65. */
  66. function _mapStateToProps(state) {
  67. return {
  68. aspectRatio: state['features/base/responsive-ui'].aspectRatio
  69. };
  70. }