You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FormSectionHeader.native.js 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // @flow
  2. import React, { Component } from 'react';
  3. import { Text, View } from 'react-native';
  4. import { connect } from 'react-redux';
  5. import { ASPECT_RATIO_WIDE } from '../../base/aspect-ratio';
  6. import { translate } from '../../base/i18n';
  7. import { getSafetyOffset } from '../../base/react';
  8. import styles, { CONTAINER_PADDING } from './styles';
  9. /**
  10. * The type of the React {@code Component} props of {@link FormSectionHeader}
  11. */
  12. type Props = {
  13. /**
  14. * The current aspect ratio of the screen.
  15. */
  16. _aspectRatio: Symbol,
  17. /**
  18. * The i18n key of the text label of the section.
  19. */
  20. i18nLabel: string,
  21. /**
  22. * An external style object passed to the component.
  23. */
  24. style: Object,
  25. /**
  26. * Invoked to obtain translated strings.
  27. */
  28. t: Function
  29. }
  30. /**
  31. * Implements a React {@code Component} which renders a section header on a
  32. * form. This calculates the available safe view as well.
  33. */
  34. class FormSectionHeader extends Component<Props> {
  35. /**
  36. * Initializes a new {@code FormSectionHeader} instance.
  37. *
  38. * @param {Object} props - Component properties.
  39. */
  40. constructor(props) {
  41. super(props);
  42. this._getSafetyMargin = this._getSafetyMargin.bind(this);
  43. }
  44. /**
  45. * Implements React's {@link Component#render()}.
  46. *
  47. * @inheritdoc
  48. * @override
  49. * @returns {ReactElement}
  50. */
  51. render() {
  52. const { t } = this.props;
  53. return (
  54. <View
  55. style = { [
  56. styles.formSectionTitle,
  57. this.props.style,
  58. this._getSafetyMargin()
  59. ] } >
  60. <Text>
  61. { t(this.props.i18nLabel) }
  62. </Text>
  63. </View>
  64. );
  65. }
  66. _getSafetyMargin: () => Object;
  67. /**
  68. * Calculates the safety margin for this header. See comment in
  69. * functions.js.
  70. *
  71. * @private
  72. * @returns {Object}
  73. */
  74. _getSafetyMargin() {
  75. if (this.props._aspectRatio === ASPECT_RATIO_WIDE) {
  76. const safeOffset
  77. = Math.max(getSafetyOffset() - CONTAINER_PADDING, 0);
  78. return {
  79. marginLeft: safeOffset,
  80. marginRight: safeOffset
  81. };
  82. }
  83. return undefined;
  84. }
  85. }
  86. /**
  87. * Maps (parts of) the redux state to the React {@code Component} props of
  88. * {@code FormSectionHeader}.
  89. *
  90. * @param {Object} state - The redux state.
  91. * @protected
  92. * @returns {Object}
  93. */
  94. export function _mapStateToProps(state: Object) {
  95. return {
  96. _aspectRatio: state['features/base/aspect-ratio'].aspectRatio
  97. };
  98. }
  99. export default translate(connect(_mapStateToProps)(FormSectionHeader));