Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

PollCreate.js 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. import React, { useCallback, useEffect, useRef, useState } from 'react';
  2. import { FlatList, Platform, Text, View } from 'react-native';
  3. import { Divider, TouchableRipple } 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 } = 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. <TouchableRipple
  72. onPress = { onPress }
  73. rippleColor = { 'transparent' } >
  74. <Text style = { dialogStyles.optionRemoveButtonText }>
  75. { t('polls.create.removeOption') }
  76. </Text>
  77. </TouchableRipple>
  78. );
  79. /* eslint-disable react/jsx-no-bind */
  80. const renderListItem = ({ index }: { index: number }) =>
  81. // padding to take into account the two default options
  82. (
  83. <View
  84. style = { dialogStyles.optionContainer }>
  85. <Input
  86. blurOnSubmit = { false }
  87. label = { t('polls.create.pollOption', { index: index + 1 }) }
  88. maxLength = { CHAR_LIMIT }
  89. multiline = { true }
  90. onChange = { text => setAnswer(index, text) }
  91. onKeyPress = { ev => onAnswerKeyDown(index, ev) }
  92. placeholder = { t('polls.create.answerPlaceholder', { index: index + 1 }) }
  93. ref = { input => registerFieldRef(index, input) }
  94. value = { answers[index] } />
  95. {
  96. answers.length > 2
  97. && createRemoveOptionButton(() => removeAnswer(index))
  98. }
  99. </View>
  100. );
  101. const buttonRowStyles = Platform.OS === 'android'
  102. ? chatStyles.buttonRowAndroid : chatStyles.buttonRowIos;
  103. return (
  104. <View style = { chatStyles.pollCreateContainer }>
  105. <View style = { chatStyles.pollCreateSubContainer }>
  106. <Input
  107. autoFocus = { true }
  108. blurOnSubmit = { false }
  109. customStyles = {{ container: dialogStyles.customContainer }}
  110. label = { t('polls.create.pollQuestion') }
  111. maxLength = { CHAR_LIMIT }
  112. multiline = { true }
  113. onChange = { setQuestion }
  114. onSubmitEditing = { onQuestionKeyDown }
  115. placeholder = { t('polls.create.questionPlaceholder') }
  116. value = { question } />
  117. <Divider style = { styles.fieldSeparator } />
  118. <FlatList
  119. blurOnSubmit = { true }
  120. data = { answers }
  121. extraData = { answers }
  122. keyExtractor = { (item, index) => index.toString() }
  123. ref = { answerListRef }
  124. renderItem = { renderListItem } />
  125. <View style = { chatStyles.pollCreateButtonsContainer }>
  126. <Button
  127. accessibilityLabel = 'polls.create.addOption'
  128. disabled = { answers.length >= ANSWERS_LIMIT }
  129. labelKey = 'polls.create.addOption'
  130. onClick = { () => {
  131. // adding and answer
  132. addAnswer();
  133. requestFocus(answers.length);
  134. } }
  135. style = { chatStyles.pollCreateAddButton }
  136. type = { SECONDARY } />
  137. <View
  138. style = { buttonRowStyles }>
  139. <Button
  140. accessibilityLabel = 'polls.create.cancel'
  141. labelKey = 'polls.create.cancel'
  142. onClick = { () => setCreateMode(false) }
  143. style = { chatStyles.pollCreateButton }
  144. type = { SECONDARY } />
  145. <Button
  146. accessibilityLabel = 'polls.create.send'
  147. disabled = { isSubmitDisabled }
  148. labelKey = 'polls.create.send'
  149. onClick = { onSubmit }
  150. style = { chatStyles.pollCreateButton }
  151. type = { PRIMARY } />
  152. </View>
  153. </View>
  154. </View>
  155. </View>
  156. );
  157. };
  158. /*
  159. * We apply AbstractPollCreate to fill in the AbstractProps common
  160. * to both the web and native implementations.
  161. */
  162. // eslint-disable-next-line new-cap
  163. export default AbstractPollCreate(PollCreate);