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

FormRow.tsx 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 { layout, 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. layout === 'column' ? styles.fieldLabelTextColumn : undefined
  67. ] } >
  68. { t(this.props.label) }
  69. </Text>
  70. </View>
  71. <View style = { styles.fieldValueContainer as ViewStyle } >
  72. { newChild }
  73. </View>
  74. </View>
  75. );
  76. }
  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?: React.ReactElement) {
  90. if (field?.type) { // @ts-ignore
  91. switch (field.type.displayName) {
  92. case 'TextInput':
  93. return {
  94. placeholderTextColor: PLACEHOLDER_COLOR,
  95. style: [
  96. styles.textInputField,
  97. this.props.layout === 'column' ? styles.textInputFieldColumn : undefined
  98. ],
  99. underlineColorAndroid: ANDROID_UNDERLINE_COLOR
  100. };
  101. }
  102. }
  103. return {};
  104. }
  105. /**
  106. * Assembles the row style array based on the row's props.
  107. *
  108. * @private
  109. * @returns {Array<Object>}
  110. */
  111. _getRowStyle() {
  112. const { fieldSeparator, layout } = this.props;
  113. const rowStyle: ViewStyle[] = [
  114. styles.fieldContainer as ViewStyle
  115. ];
  116. if (fieldSeparator) {
  117. rowStyle.push(styles.fieldSeparator);
  118. }
  119. if (layout === 'column') {
  120. rowStyle.push(
  121. styles.fieldContainerColumn as ViewStyle
  122. );
  123. }
  124. return rowStyle;
  125. }
  126. }
  127. export default translate(FormRow);