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

PollCreate.js 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. import React, { useCallback, useEffect, useRef, useState } from 'react';
  2. import { FlatList, Platform, View } from 'react-native';
  3. import { Divider } from 'react-native-paper';
  4. import Button from '../../../base/ui/components/native/Button';
  5. import Input from '../../../base/ui/components/native/Input';
  6. import { BUTTON_TYPES } from '../../../base/ui/constants.native';
  7. import styles
  8. from '../../../settings/components/native/styles';
  9. import { ANSWERS_LIMIT, CHAR_LIMIT } from '../../constants';
  10. import AbstractPollCreate from '../AbstractPollCreate';
  11. import type { AbstractProps } from '../AbstractPollCreate';
  12. import { chatStyles, dialogStyles } from './styles';
  13. const PollCreate = (props: AbstractProps) => {
  14. const {
  15. addAnswer,
  16. answers,
  17. isSubmitDisabled,
  18. onSubmit,
  19. question,
  20. removeAnswer,
  21. setAnswer,
  22. setCreateMode,
  23. setQuestion,
  24. t
  25. } = props;
  26. const answerListRef = useRef(null);
  27. /*
  28. * This ref stores the Array of answer input fields, allowing us to focus on them.
  29. * This array is maintained by registerFieldRef and the useEffect below.
  30. */
  31. const answerInputs = useRef([]);
  32. const registerFieldRef = useCallback((i, input) => {
  33. if (input === null) {
  34. return;
  35. }
  36. answerInputs.current[i] = input;
  37. }, [ answerInputs ]);
  38. useEffect(() => {
  39. answerInputs.current = answerInputs.current.slice(0, answers.length);
  40. }, [ answers ]);
  41. /*
  42. * This state allows us to requestFocus asynchronously, without having to worry
  43. * about whether a newly created input field has been rendered yet or not.
  44. */
  45. const [ lastFocus, requestFocus ] = useState(null);
  46. const { PRIMARY, SECONDARY, TERTIARY } = BUTTON_TYPES;
  47. useEffect(() => {
  48. if (lastFocus === null) {
  49. return;
  50. }
  51. const input = answerInputs.current[lastFocus];
  52. if (input === undefined) {
  53. return;
  54. }
  55. input.focus();
  56. }, [ answerInputs, lastFocus ]);
  57. const onQuestionKeyDown = useCallback(() => {
  58. answerInputs.current[0].focus();
  59. });
  60. // Called on keypress in answer fields
  61. const onAnswerKeyDown = useCallback((index: number, ev) => {
  62. const { key } = ev.nativeEvent;
  63. const currentText = answers[index];
  64. if (key === 'Backspace' && currentText === '' && answers.length > 1) {
  65. removeAnswer(index);
  66. requestFocus(index > 0 ? index - 1 : 0);
  67. }
  68. }, [ answers, addAnswer, removeAnswer, requestFocus ]);
  69. /* eslint-disable react/no-multi-comp */
  70. const createRemoveOptionButton = onPress => (
  71. <Button
  72. labelKey = 'polls.create.removeOption'
  73. labelStyle = { dialogStyles.optionRemoveButtonText }
  74. onClick = { onPress }
  75. style = { dialogStyles.optionRemoveButton }
  76. type = { TERTIARY } />
  77. );
  78. /* eslint-disable react/jsx-no-bind */
  79. const renderListItem = ({ index }: { index: number }) =>
  80. // padding to take into account the two default options
  81. (
  82. <View
  83. style = { dialogStyles.optionContainer }>
  84. <Input
  85. blurOnSubmit = { false }
  86. label = { t('polls.create.pollOption', { index: index + 1 }) }
  87. maxLength = { CHAR_LIMIT }
  88. multiline = { true }
  89. onChange = { text => setAnswer(index, text) }
  90. onKeyPress = { ev => onAnswerKeyDown(index, ev) }
  91. placeholder = { t('polls.create.answerPlaceholder', { index: index + 1 }) }
  92. ref = { input => registerFieldRef(index, input) }
  93. value = { answers[index] } />
  94. {
  95. answers.length > 2
  96. && createRemoveOptionButton(() => removeAnswer(index))
  97. }
  98. </View>
  99. );
  100. const pollCreateButtonsContainerStyles = Platform.OS === 'android'
  101. ? chatStyles.pollCreateButtonsContainerAndroid : chatStyles.pollCreateButtonsContainerIos;
  102. return (
  103. <View style = { chatStyles.pollCreateContainer }>
  104. <View style = { chatStyles.pollCreateSubContainer }>
  105. <Input
  106. autoFocus = { true }
  107. blurOnSubmit = { false }
  108. customStyles = {{ container: dialogStyles.customContainer }}
  109. label = { t('polls.create.pollQuestion') }
  110. maxLength = { CHAR_LIMIT }
  111. multiline = { true }
  112. onChange = { setQuestion }
  113. onSubmitEditing = { onQuestionKeyDown }
  114. placeholder = { t('polls.create.questionPlaceholder') }
  115. value = { question } />
  116. <Divider style = { styles.fieldSeparator } />
  117. <FlatList
  118. blurOnSubmit = { true }
  119. data = { answers }
  120. extraData = { answers }
  121. keyExtractor = { (item, index) => index.toString() }
  122. ref = { answerListRef }
  123. renderItem = { renderListItem } />
  124. <View style = { pollCreateButtonsContainerStyles }>
  125. <Button
  126. accessibilityLabel = 'polls.create.addOption'
  127. disabled = { answers.length >= ANSWERS_LIMIT }
  128. labelKey = 'polls.create.addOption'
  129. onClick = { () => {
  130. // adding and answer
  131. addAnswer();
  132. requestFocus(answers.length);
  133. } }
  134. style = { chatStyles.pollCreateAddButton }
  135. type = { SECONDARY } />
  136. <View
  137. style = { chatStyles.buttonRow }>
  138. <Button
  139. accessibilityLabel = 'polls.create.cancel'
  140. labelKey = 'polls.create.cancel'
  141. onClick = { () => setCreateMode(false) }
  142. style = { chatStyles.pollCreateButton }
  143. type = { SECONDARY } />
  144. <Button
  145. accessibilityLabel = 'polls.create.send'
  146. disabled = { isSubmitDisabled }
  147. labelKey = 'polls.create.send'
  148. onClick = { onSubmit }
  149. style = { chatStyles.pollCreateButton }
  150. type = { PRIMARY } />
  151. </View>
  152. </View>
  153. </View>
  154. </View>
  155. );
  156. };
  157. /*
  158. * We apply AbstractPollCreate to fill in the AbstractProps common
  159. * to both the web and native implementations.
  160. */
  161. // eslint-disable-next-line new-cap
  162. export default AbstractPollCreate(PollCreate);