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.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. * Whether the input is read only or not.
  31. */
  32. readOnly?: boolean,
  33. /**
  34. * The field type (e.g. text, password...etc).
  35. */
  36. type: string,
  37. /**
  38. * Externally provided value.
  39. */
  40. value?: string,
  41. id?: string,
  42. autoComplete?: string
  43. };
  44. type State = {
  45. /**
  46. * True if the field is focused, false otherwise.
  47. */
  48. focused: boolean,
  49. /**
  50. * The current value of the field.
  51. */
  52. value: string
  53. }
  54. /**
  55. * Implements a pre-styled input field to be used on pre-meeting screens.
  56. */
  57. export default class InputField extends PureComponent<Props, State> {
  58. static defaultProps: {
  59. className: '',
  60. type: 'text'
  61. };
  62. /**
  63. * Instantiates a new component.
  64. *
  65. * @inheritdoc
  66. */
  67. constructor(props: Props) {
  68. super(props);
  69. this.state = {
  70. focused: false,
  71. value: props.value || ''
  72. };
  73. this._onBlur = this._onBlur.bind(this);
  74. this._onChange = this._onChange.bind(this);
  75. this._onFocus = this._onFocus.bind(this);
  76. this._onKeyDown = this._onKeyDown.bind(this);
  77. }
  78. /**
  79. * Implements {@code PureComponent.getDerivedStateFromProps}.
  80. *
  81. * @inheritdoc
  82. */
  83. static getDerivedStateFromProps(props: Props, state: State) {
  84. const { value } = props;
  85. if (state.value !== value) {
  86. return {
  87. ...state,
  88. value
  89. };
  90. }
  91. return null;
  92. }
  93. /**
  94. * Implements {@code PureComponent#render}.
  95. *
  96. * @inheritdoc
  97. */
  98. render() {
  99. return (
  100. <input
  101. autoComplete = { this.props.autoComplete }
  102. autoFocus = { this.props.autoFocus }
  103. className = { `field ${this.state.focused ? 'focused' : ''} ${this.props.className || ''}` }
  104. data-testid = { this.props.testId ? this.props.testId : undefined }
  105. id = { this.props.id }
  106. onBlur = { this._onBlur }
  107. onChange = { this._onChange }
  108. onFocus = { this._onFocus }
  109. onKeyDown = { this._onKeyDown }
  110. placeholder = { this.props.placeHolder }
  111. readOnly = { this.props.readOnly }
  112. type = { this.props.type }
  113. value = { this.state.value } />
  114. );
  115. }
  116. _onBlur: () => void;
  117. /**
  118. * Callback for the onBlur event of the field.
  119. *
  120. * @returns {void}
  121. */
  122. _onBlur() {
  123. this.setState({
  124. focused: false
  125. });
  126. }
  127. _onChange: Object => void;
  128. /**
  129. * Callback for the onChange event of the field.
  130. *
  131. * @param {Object} evt - The static event.
  132. * @returns {void}
  133. */
  134. _onChange(evt) {
  135. const value = getFieldValue(evt);
  136. this.setState({
  137. value
  138. });
  139. const { onChange } = this.props;
  140. onChange && onChange(value);
  141. }
  142. _onFocus: () => void;
  143. /**
  144. * Callback for the onFocus event of the field.
  145. *
  146. * @returns {void}
  147. */
  148. _onFocus() {
  149. this.setState({
  150. focused: true
  151. });
  152. }
  153. _onKeyDown: Object => void;
  154. /**
  155. * Joins the conference on 'Enter'.
  156. *
  157. * @param {Event} event - Key down event object.
  158. * @returns {void}
  159. */
  160. _onKeyDown(event) {
  161. const { onSubmit } = this.props;
  162. onSubmit && event.key === 'Enter' && onSubmit();
  163. }
  164. }