changeReactMentionsInput.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. const changeReactMentionsInput = (wrapper, value) => {
  2. const oldActiveElement = document.activeElement;
  3. // Need to do this because of how react-mentions works,
  4. // checks that event object is === document.activeElement
  5. let el = wrapper.find('textarea').getDOMNode();
  6. // We need a non-zero width selection for `react-mentions`
  7. el.selectionStart = 2;
  8. el.selectionEnd = 3;
  9. wrapper.find('textarea').simulate('select', {target: el});
  10. // Finally update element value
  11. el = wrapper.find('textarea').getDOMNode();
  12. el.value = value;
  13. el.selectionEnd = value.length;
  14. // We need to make document.activeElement == event.target (event being the
  15. // change event), otherwise react-mentions will not propagate the change event
  16. Object.defineProperty(document, 'activeElement', {
  17. get() {
  18. return el;
  19. },
  20. configurable: true,
  21. });
  22. wrapper.find('textarea').simulate('change', {target: el});
  23. // reset activeElement
  24. Object.defineProperty(document, 'activeElement', {
  25. get() {
  26. return oldActiveElement;
  27. },
  28. configurable: true,
  29. });
  30. };
  31. export default changeReactMentionsInput;