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

InputField.js 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. }
  78. /**
  79. * Implements {@code PureComponent#render}.
  80. *
  81. * @inheritdoc
  82. */
  83. render() {
  84. return (
  85. <input
  86. className = { `field ${this.state.focused ? 'focused' : ''} ${this.props.className || ''}` }
  87. onBlur = { this._onBlur }
  88. onChange = { this._onChange }
  89. onFocus = { this._onFocus }
  90. onKeyDown = { this._onKeyDown }
  91. placeholder = { this.props.placeHolder }
  92. type = { this.props.type }
  93. value = { this.state.value } />
  94. );
  95. }
  96. _onBlur: () => void;
  97. /**
  98. * Callback for the onBlur event of the field.
  99. *
  100. * @returns {void}
  101. */
  102. _onBlur() {
  103. this.setState({
  104. focused: false
  105. });
  106. }
  107. _onChange: Object => void;
  108. /**
  109. * Callback for the onChange event of the field.
  110. *
  111. * @param {Object} evt - The static event.
  112. * @returns {void}
  113. */
  114. _onChange(evt) {
  115. const value = getFieldValue(evt);
  116. this.setState({
  117. value
  118. });
  119. const { onChange } = this.props;
  120. onChange && onChange(value);
  121. }
  122. _onFocus: () => void;
  123. /**
  124. * Callback for the onFocus event of the field.
  125. *
  126. * @returns {void}
  127. */
  128. _onFocus() {
  129. this.setState({
  130. focused: true
  131. });
  132. }
  133. _onKeyDown: Object => void;
  134. /**
  135. * Joins the conference on 'Enter'.
  136. *
  137. * @param {Event} event - Key down event object.
  138. * @returns {void}
  139. */
  140. _onKeyDown(event) {
  141. const { onSubmit } = this.props;
  142. onSubmit && event.key === 'Enter' && onSubmit();
  143. }
  144. }