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

InputField.js 3.5KB

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