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

InputField.js 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. // @flow
  2. import React, { PureComponent } from 'react';
  3. import { getFieldValue } from '../../../react';
  4. type Props = {
  5. /**
  6. * Class name to be appended to the default class list.
  7. */
  8. className?: string,
  9. /**
  10. * TestId of the button. Can be used to locate element when testing UI.
  11. */
  12. testId?: string,
  13. /**
  14. * Callback for the onChange event of the field.
  15. */
  16. onChange: Function,
  17. /**
  18. * Callback to be used when the user hits Enter in the field.
  19. */
  20. onSubmit?: Function,
  21. /**
  22. * Placeholder text for the field.
  23. */
  24. placeHolder: string,
  25. /**
  26. * The field type (e.g. text, password...etc).
  27. */
  28. type: string,
  29. /**
  30. * Externally provided value.
  31. */
  32. value?: string
  33. };
  34. type State = {
  35. /**
  36. * True if the field is focused, false otherwise.
  37. */
  38. focused: boolean,
  39. /**
  40. * The current value of the field.
  41. */
  42. value: string
  43. }
  44. /**
  45. * Implements a pre-styled input field to be used on pre-meeting screens.
  46. */
  47. export default class InputField extends PureComponent<Props, State> {
  48. static defaultProps: {
  49. className: '',
  50. type: 'text'
  51. };
  52. /**
  53. * Instantiates a new component.
  54. *
  55. * @inheritdoc
  56. */
  57. constructor(props: Props) {
  58. super(props);
  59. this.state = {
  60. focused: false,
  61. value: props.value || ''
  62. };
  63. this._onBlur = this._onBlur.bind(this);
  64. this._onChange = this._onChange.bind(this);
  65. this._onFocus = this._onFocus.bind(this);
  66. this._onKeyDown = this._onKeyDown.bind(this);
  67. }
  68. /**
  69. * Implements {@code PureComponent.getDerivedStateFromProps}.
  70. *
  71. * @inheritdoc
  72. */
  73. static getDerivedStateFromProps(props: Props, state: State) {
  74. const { value } = props;
  75. if (state.value !== value) {
  76. return {
  77. ...state,
  78. value
  79. };
  80. }
  81. return null;
  82. }
  83. /**
  84. * Implements {@code PureComponent#render}.
  85. *
  86. * @inheritdoc
  87. */
  88. render() {
  89. return (
  90. <input
  91. className = { `field ${this.state.focused ? 'focused' : ''} ${this.props.className || ''}` }
  92. data-testid = { this.props.testId ? this.props.testId : undefined }
  93. onBlur = { this._onBlur }
  94. onChange = { this._onChange }
  95. onFocus = { this._onFocus }
  96. onKeyDown = { this._onKeyDown }
  97. placeholder = { this.props.placeHolder }
  98. type = { this.props.type }
  99. value = { this.state.value } />
  100. );
  101. }
  102. _onBlur: () => void;
  103. /**
  104. * Callback for the onBlur event of the field.
  105. *
  106. * @returns {void}
  107. */
  108. _onBlur() {
  109. this.setState({
  110. focused: false
  111. });
  112. }
  113. _onChange: Object => void;
  114. /**
  115. * Callback for the onChange event of the field.
  116. *
  117. * @param {Object} evt - The static event.
  118. * @returns {void}
  119. */
  120. _onChange(evt) {
  121. const value = getFieldValue(evt);
  122. this.setState({
  123. value
  124. });
  125. const { onChange } = this.props;
  126. onChange && onChange(value);
  127. }
  128. _onFocus: () => void;
  129. /**
  130. * Callback for the onFocus event of the field.
  131. *
  132. * @returns {void}
  133. */
  134. _onFocus() {
  135. this.setState({
  136. focused: true
  137. });
  138. }
  139. _onKeyDown: Object => void;
  140. /**
  141. * Joins the conference on 'Enter'.
  142. *
  143. * @param {Event} event - Key down event object.
  144. * @returns {void}
  145. */
  146. _onKeyDown(event) {
  147. const { onSubmit } = this.props;
  148. onSubmit && event.key === 'Enter' && onSubmit();
  149. }
  150. }