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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 } 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. i18nLabel: string,
  22. /**
  23. * Invoked to obtain translated strings.
  24. */
  25. t: Function
  26. }
  27. /**
  28. * Implements a React {@code Component} which renders a standardized row on a
  29. * form. The component should have exactly one child component.
  30. */
  31. class FormRow extends Component<Props> {
  32. /**
  33. * Initializes a new {@code FormRow} instance.
  34. *
  35. * @param {Object} props - Component properties.
  36. */
  37. constructor(props) {
  38. super(props);
  39. React.Children.only(this.props.children);
  40. this._getDefaultFieldProps = this._getDefaultFieldProps.bind(this);
  41. this._getRowStyle = this._getRowStyle.bind(this);
  42. }
  43. /**
  44. * Implements React's {@link Component#render()}.
  45. *
  46. * @inheritdoc
  47. * @override
  48. * @returns {ReactElement}
  49. */
  50. render() {
  51. const { t } = this.props;
  52. // Some field types need additional props to look good and standardized
  53. // on a form.
  54. const newChild = React.cloneElement(
  55. this.props.children,
  56. this._getDefaultFieldProps(this.props.children)
  57. );
  58. return (
  59. <View
  60. style = { this._getRowStyle() } >
  61. <View style = { styles.fieldLabelContainer } >
  62. <Text
  63. style = { [
  64. styles.text, styles.fieldLabelText
  65. ] } >
  66. { t(this.props.i18nLabel) }
  67. </Text>
  68. </View>
  69. <View style = { styles.fieldValueContainer } >
  70. { newChild }
  71. </View>
  72. </View>
  73. );
  74. }
  75. _getDefaultFieldProps: (field: Component<*, *>) => Object;
  76. /**
  77. * Assembles the default props to the field child component of this form
  78. * row.
  79. *
  80. * Currently tested/supported field types:
  81. * - TextInput
  82. * - Switch (needs no addition props ATM).
  83. *
  84. * @private
  85. * @param {Object} field - The field (child) component.
  86. * @returns {Object}
  87. */
  88. _getDefaultFieldProps(field: Object) {
  89. if (field && field.type) {
  90. switch (field.type.displayName) {
  91. case 'TextInput':
  92. return {
  93. style: styles.textInputField,
  94. underlineColorAndroid: ANDROID_UNDERLINE_COLOR
  95. };
  96. }
  97. }
  98. return {};
  99. }
  100. _getRowStyle: () => Array<Object>;
  101. /**
  102. * Assembles the row style array based on the row's props.
  103. *
  104. * @private
  105. * @returns {Array<Object>}
  106. */
  107. _getRowStyle() {
  108. const rowStyle = [
  109. styles.fieldContainer
  110. ];
  111. if (this.props.fieldSeparator) {
  112. rowStyle.push(styles.fieldSeparator);
  113. }
  114. return rowStyle;
  115. }
  116. }
  117. export default translate(FormRow);