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

FormRow.js 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. label: 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
  55. = React.cloneElement(
  56. this.props.children,
  57. this._getDefaultFieldProps(this.props.children));
  58. return (
  59. <View
  60. style = { this._getRowStyle() } >
  61. <View style = { styles.fieldLabelContainer } >
  62. <Text
  63. style = { [
  64. styles.text,
  65. styles.fieldLabelText
  66. ] } >
  67. { t(this.props.label) }
  68. </Text>
  69. </View>
  70. <View style = { styles.fieldValueContainer } >
  71. { newChild }
  72. </View>
  73. </View>
  74. );
  75. }
  76. _getDefaultFieldProps: (field: Component<*, *>) => Object;
  77. /**
  78. * Assembles the default props to the field child component of this form
  79. * row.
  80. *
  81. * Currently tested/supported field types:
  82. * - TextInput
  83. * - Switch (needs no addition props ATM).
  84. *
  85. * @param {Object} field - The field (child) component.
  86. * @private
  87. * @returns {Object}
  88. */
  89. _getDefaultFieldProps(field: Object) {
  90. if (field && field.type) {
  91. switch (field.type.displayName) {
  92. case 'TextInput':
  93. return {
  94. style: styles.textInputField,
  95. underlineColorAndroid: ANDROID_UNDERLINE_COLOR
  96. };
  97. }
  98. }
  99. return {};
  100. }
  101. _getRowStyle: () => Array<Object>;
  102. /**
  103. * Assembles the row style array based on the row's props.
  104. *
  105. * @private
  106. * @returns {Array<Object>}
  107. */
  108. _getRowStyle() {
  109. const rowStyle = [
  110. styles.fieldContainer
  111. ];
  112. if (this.props.fieldSeparator) {
  113. rowStyle.push(styles.fieldSeparator);
  114. }
  115. return rowStyle;
  116. }
  117. }
  118. export default translate(FormRow);