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

FormRow.tsx 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import React, { Component } from 'react';
  2. import { WithTranslation } from 'react-i18next';
  3. import { Text, View, ViewStyle } from 'react-native';
  4. import { translate } from '../../../base/i18n/functions';
  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. interface IProps extends WithTranslation {
  10. /**
  11. * Component's children.
  12. */
  13. children: React.ReactElement;
  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. /**
  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<IProps> {
  32. /**
  33. * Initializes a new {@code FormRow} instance.
  34. *
  35. * @param {Object} props - Component properties.
  36. */
  37. constructor(props: IProps) {
  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 as ViewStyle } >
  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 as ViewStyle } >
  71. { newChild }
  72. </View>
  73. </View>
  74. );
  75. }
  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. * @param {Object} field - The field (child) component.
  85. * @private
  86. * @returns {Object}
  87. */
  88. _getDefaultFieldProps(field?: React.ReactElement) {
  89. if (field?.type) { // @ts-ignore
  90. switch (field.type.displayName) {
  91. case 'TextInput':
  92. return {
  93. placeholderTextColor: PLACEHOLDER_COLOR,
  94. style: [
  95. styles.textInputField,
  96. this.props.layout === 'column' ? styles.textInputFieldColumn : undefined
  97. ],
  98. underlineColorAndroid: ANDROID_UNDERLINE_COLOR
  99. };
  100. }
  101. }
  102. return {};
  103. }
  104. /**
  105. * Assembles the row style array based on the row's props.
  106. *
  107. * @private
  108. * @returns {Array<Object>}
  109. */
  110. _getRowStyle() {
  111. const { fieldSeparator, layout } = this.props;
  112. const rowStyle: ViewStyle[] = [
  113. styles.fieldContainer as ViewStyle
  114. ];
  115. if (fieldSeparator) {
  116. rowStyle.push(styles.fieldSeparator);
  117. }
  118. if (layout === 'column') {
  119. rowStyle.push(
  120. styles.fieldContainerColumn as ViewStyle
  121. );
  122. }
  123. return rowStyle;
  124. }
  125. }
  126. export default translate(FormRow);