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.

InputField.js 4.0KB

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