Advertisement
Guest User

Untitled

a guest
May 23rd, 2019
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.79 KB | None | 0 0
  1. import React, { useState, useRef } from 'react';
  2.  
  3. import AutoComplete from 'antd/lib/auto-complete';
  4. import Tag from 'antd/lib/tag';
  5.  
  6. import useEventListener from '../../components/use-event-listener';
  7.  
  8. function TagInput({ tags = [], autocompleteTags, onChange, size = 'small' }) {
  9. const [dataSource, setDataSource] = useState(autocompleteTags);
  10. const autoRef = useRef();
  11. const deletePrevious = useRef(false);
  12.  
  13. const tagStyles = {
  14. small: {
  15. marginTop: 3,
  16. },
  17. default: {
  18. marginTop: 3,
  19. padding: 5,
  20. fontSize: 14,
  21. },
  22. };
  23.  
  24. const addTag = tag => {
  25. if (tag && tags.indexOf(tag) < 0) {
  26. deletePrevious.current = false;
  27. onChange([...tags, tag]);
  28. handleSearchTag('');
  29. }
  30. };
  31.  
  32. const getInputValue = () => {
  33. return autoRef.current.querySelector('input').value;
  34. };
  35.  
  36. const handleKeyDown = e => {
  37. if (e.key === 'Enter') {
  38. const tag = getInputValue();
  39. addTag(tag);
  40. } else if (
  41. e.key === 'Backspace' &&
  42. tags.length > 0 &&
  43. getInputValue() === ''
  44. ) {
  45. if (deletePrevious.current) {
  46. onChange(tags.slice(0, tags.length - 1));
  47. deletePrevious.current = false;
  48. } else {
  49. deletePrevious.current = true;
  50. }
  51. }
  52. };
  53.  
  54. const handleSelectTag = t => {
  55. addTag(t);
  56. };
  57.  
  58. const handleDeleteTag = tag => {
  59. deletePrevious.current = false;
  60. onChange(tags.filter(t => t !== tag));
  61. };
  62.  
  63. const handleSearchTag = tag => {
  64. const tagUpper = tag.toUpperCase();
  65. let newDataSource = autocompleteTags;
  66. if (tag) {
  67. newDataSource = autocompleteTags.filter(t =>
  68. t.toUpperCase().includes(tagUpper),
  69. );
  70. }
  71. setDataSource(newDataSource);
  72. };
  73.  
  74. useEventListener(
  75. 'keydown',
  76. handleKeyDown,
  77. autoRef,
  78. ref => {
  79. return ref.current.querySelector('input');
  80. },
  81. [tags.length],
  82. );
  83.  
  84. return (
  85. <div ref={autoRef} style={{ display: 'inline-block' }}>
  86. {tags.map((t, i) => (
  87. <span key={t} style={{ display: 'inline-block' }}>
  88. <Tag
  89. closable
  90. onClose={() => handleDeleteTag(t)}
  91. style={tagStyles[size]}
  92. >
  93. {t}
  94. </Tag>
  95. </span>
  96. ))}
  97. <AutoComplete
  98. key={`Autocomplete${tags.length}`} // Reset state after delete/add tag
  99. backfill={true} // Fill the input with the autocomplete selection
  100. autoFocus={true} // Smart autofocus managment
  101. autoClearSearchValue={true} // No work (at least how I want)
  102. type="text"
  103. size={size}
  104. placeholder="tag"
  105. style={{ width: 78 }}
  106. dataSource={dataSource}
  107. onSelect={handleSelectTag}
  108. onSearch={handleSearchTag}
  109. dropdownMatchSelectWidth={false} // Allow autocomplete popup adjust its contente
  110. />
  111. </div>
  112. );
  113. }
  114.  
  115. export default TagInput;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement