Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

InputField.js 3.8KB

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