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.

FormRow.native.js 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. // @flow
  2. import React, { Component } from 'react';
  3. import { Text, View } from 'react-native';
  4. import { connect } from 'react-redux';
  5. import { translate } from '../../base/i18n';
  6. import { getSafetyOffset } from '../../base/react';
  7. import { ASPECT_RATIO_WIDE } from '../../base/responsive-ui';
  8. import styles, { ANDROID_UNDERLINE_COLOR, CONTAINER_PADDING } from './styles';
  9. /**
  10. * The type of the React {@code Component} props of {@link FormRow}
  11. */
  12. type Props = {
  13. /**
  14. * The current aspect ratio of the screen.
  15. */
  16. _aspectRatio: Symbol,
  17. /**
  18. *
  19. */
  20. children: Object,
  21. /**
  22. * Prop to decide if a row separator is to be rendered.
  23. */
  24. fieldSeparator: boolean,
  25. /**
  26. * The i18n key of the text label of the form field.
  27. */
  28. i18nLabel: string,
  29. /**
  30. * Invoked to obtain translated strings.
  31. */
  32. t: Function
  33. }
  34. /**
  35. * Implements a React {@code Component} which renders a standardized row on a
  36. * form. The component should have exactly one child component.
  37. */
  38. class FormRow extends Component<Props> {
  39. /**
  40. * Initializes a new {@code FormRow} instance.
  41. *
  42. * @param {Object} props - Component properties.
  43. */
  44. constructor(props) {
  45. super(props);
  46. React.Children.only(this.props.children);
  47. this._getDefaultFieldProps = this._getDefaultFieldProps.bind(this);
  48. this._getRowStyle = this._getRowStyle.bind(this);
  49. }
  50. /**
  51. * Implements React's {@link Component#render()}.
  52. *
  53. * @inheritdoc
  54. * @override
  55. * @returns {ReactElement}
  56. */
  57. render() {
  58. const { t } = this.props;
  59. // Some field types need additional props to look good and standardized
  60. // on a form.
  61. const newChild = React.cloneElement(
  62. this.props.children,
  63. this._getDefaultFieldProps(this.props.children)
  64. );
  65. return (
  66. <View
  67. style = { this._getRowStyle() } >
  68. <View style = { styles.fieldLabelContainer } >
  69. <Text style = { styles.text } >
  70. { t(this.props.i18nLabel) }
  71. </Text>
  72. </View>
  73. <View style = { styles.fieldValueContainer } >
  74. { newChild }
  75. </View>
  76. </View>
  77. );
  78. }
  79. _getDefaultFieldProps: (field: Component<*, *>) => Object;
  80. /**
  81. * Assembles the default props to the field child component of this form
  82. * row.
  83. *
  84. * Currently tested/supported field types:
  85. * - TextInput
  86. * - Switch (needs no addition props ATM).
  87. *
  88. * @private
  89. * @param {Object} field - The field (child) component.
  90. * @returns {Object}
  91. */
  92. _getDefaultFieldProps(field: Object) {
  93. if (field && field.type) {
  94. switch (field.type.displayName) {
  95. case 'TextInput':
  96. return {
  97. style: styles.textInputField,
  98. underlineColorAndroid: ANDROID_UNDERLINE_COLOR
  99. };
  100. }
  101. }
  102. return {};
  103. }
  104. _getRowStyle: () => Array<Object>;
  105. /**
  106. * Assembles the row style array based on the row's props. For padding, see
  107. * comment in functions.js.
  108. *
  109. * @private
  110. * @returns {Array<Object>}
  111. */
  112. _getRowStyle() {
  113. const rowStyle = [
  114. styles.fieldContainer
  115. ];
  116. if (this.props.fieldSeparator) {
  117. rowStyle.push(styles.fieldSeparator);
  118. }
  119. if (this.props._aspectRatio === ASPECT_RATIO_WIDE) {
  120. const safeOffset = Math.max(
  121. getSafetyOffset() - CONTAINER_PADDING, 0
  122. );
  123. rowStyle.push({
  124. marginLeft: safeOffset,
  125. marginRight: safeOffset
  126. });
  127. }
  128. return rowStyle;
  129. }
  130. }
  131. /**
  132. * Maps (parts of) the redux state to the React {@code Component} props of
  133. * {@code FormRow}.
  134. *
  135. * @param {Object} state - The redux state.
  136. * @protected
  137. * @returns {Object}
  138. */
  139. export function _mapStateToProps(state: Object) {
  140. return {
  141. _aspectRatio: state['features/base/responsive-ui'].aspectRatio
  142. };
  143. }
  144. export default translate(connect(_mapStateToProps)(FormRow));