Guest User

Untitled

a guest
Mar 18th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.26 KB | None | 0 0
  1. import React, { Component } from 'react';
  2. import { render } from 'react-dom';
  3. import InputTrigger from 'react-input-trigger';
  4.  
  5. class App extends Component {
  6. constructor() {
  7. this.state = {
  8. top: null,
  9. left: null,
  10. showSuggestor: false,
  11. startPosition: null,
  12.  
  13. users: [
  14. 'Charmander',
  15. 'Squirtle',
  16. 'Bulbasaur',
  17. 'Pikachu'
  18. ],
  19.  
  20. text: null,
  21.  
  22. currentSelection: 0,
  23. }
  24.  
  25. this.toggleSuggestor = this.toggleSuggestor.bind(this);
  26. this.handleInput = this.handleInput.bind(this);
  27. this.handleKeyDown = this.handleKeyDown.bind(this);
  28. this.handleTextareaInput = this.handleTextareaInput.bind(this);
  29. }
  30.  
  31. toggleSuggestor(metaInformation) {
  32. const { hookType, cursor } = metaInformation;
  33.  
  34. if (hookType === 'start') {
  35. this.setState({
  36. showSuggestor: true,
  37. left: cursor.left,
  38. top: cursor.top + cursor.height, // we need to add the cursor height so that the dropdown doesn't overlap with the `@`.
  39. startPosition: cursor.selectionStart,
  40. });
  41. }
  42.  
  43. if (hookType === 'cancel') {
  44. // reset the state
  45.  
  46. this.setState({
  47. showSuggestor: false,
  48. left: null,
  49. top: null,
  50. text: null,
  51. startPosition: null,
  52. });
  53. }
  54. }
  55.  
  56. handleInput(metaInformation) {
  57. this.setState({
  58. text: metaInformation.text,
  59. });
  60. }
  61.  
  62. handleKeyDown(event) {
  63. const { which } = event;
  64. const { currentSelection, users } = this.state;
  65.  
  66. if (which === 40 ) { // 40 is the character code of the down arrow
  67. event.preventDefault();
  68.  
  69. this.setState({
  70. currentSelection: (currentSelection + 1) % users.length,
  71. });
  72. }
  73.  
  74. if (which === 13) { // 13 is the character code for enter
  75. event.preventDefault();
  76.  
  77. const { users, currentSelection, startPosition, textareaValue } = this.state;
  78. const user = users[currentSelection];
  79.  
  80. const newText = `${textareaValue.slice(0, startPosition - 1)}${user}${textareaValue.slice(startPosition + user.length, textareaValue.length)}`
  81.  
  82. // reset the state and set new text
  83.  
  84. this.setState({
  85. showSuggestor: false,
  86. left: null,
  87. top: null,
  88. text: null,
  89. startPosition: null,
  90.  
  91. textareaValue: newText,
  92. });
  93.  
  94. this.endHandler();
  95. }
  96. }
  97.  
  98. handleTextareaInput(event) {
  99. const { value } = event.target;
  100.  
  101. this.setState({
  102. textareaValue: value,
  103. })
  104. }
  105.  
  106. render() {
  107. return (
  108. <div
  109. style={{
  110. position: 'relative'
  111. }}
  112.  
  113. onKeyDown={this.handleKeyDown}
  114. >
  115. <InputTrigger
  116. trigger={{
  117. keyCode: 50,
  118. shiftKey: true,
  119. }}
  120. onStart={(metaData) => { this.toggleSuggestor(metaData); }}
  121. onCancel={(metaData) => { this.toggleSuggestor(metaData); }}
  122. onType={(metaData) => { this.handleInput(metaData); }}
  123. endTrigger={(endHandler) => { this.endHandler = endHandler; }}
  124. >
  125. <textarea
  126. style={{
  127. height: '100px',
  128. width: '400px',
  129. lineHeight: '1em',
  130. }}
  131.  
  132. onChange={this.handleTextareaInput}
  133. value={this.state.textareaValue}
  134. />
  135. </InputTrigger>
  136.  
  137. <div
  138. id="dropdown"
  139. style={{
  140. position: "absolute",
  141. width: "200px",
  142. borderRadius: "6px",
  143. background: "white",
  144. boxShadow: "rgba(0, 0, 0, 0.4) 0px 1px 4px",
  145.  
  146. display: this.state.showSuggestor ? "block" : "none",
  147. top: this.state.top,
  148. left: this.state.left,
  149. }}
  150. >
  151. {
  152. this.state.users
  153. .filter(user => user.indexOf(this.state.text) !== -1)
  154. .map((user, index) => (
  155. <div
  156. style={{
  157. padding: '10px 20px',
  158. background: index === this.state.currentSelection ? '#eee' : ''
  159. }}
  160. >
  161. { user }
  162. </div>
  163. ))
  164. }
  165. </div>
  166. </div>
  167. );
  168. }
  169. }
  170.  
  171. render(<App/>, document.getElementById('app'));
Add Comment
Please, Sign In to add comment