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.js 3.9KB

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