Skip to content

Commit 317dbf6

Browse files
authored
fix: tagInput paste (#5746)
1 parent 56915f1 commit 317dbf6

File tree

3 files changed

+16
-31
lines changed

3 files changed

+16
-31
lines changed

.changeset/curly-years-listen.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@ultraviolet/ui": patch
3+
---
4+
5+
`TagInput`: "paste" shoud not automatically create a new tag

packages/ui/src/components/TagInput/__tests__/index.test.tsx

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { fireEvent, screen, waitFor } from '@testing-library/react'
1+
import { screen } from '@testing-library/react'
22
import { userEvent } from '@testing-library/user-event'
33
import { renderWithTheme, shouldMatchEmotionSnapshot } from '@utils/test'
44
import { describe, expect, it, vi } from 'vitest'
@@ -106,7 +106,7 @@ describe('tagInput', () => {
106106
expect(mockOnChange).toHaveBeenCalledWith(['hello'])
107107
})
108108

109-
it('should add tag on paste', async () => {
109+
it('should not add tag on paste', async () => {
110110
const mockOnChange = vi.fn()
111111
renderWithTheme(
112112
<TagInput
@@ -117,10 +117,11 @@ describe('tagInput', () => {
117117
/>,
118118
)
119119
const input = screen.getByRole<HTMLInputElement>('textbox')
120-
fireEvent.paste(input, {
121-
clipboardData: { getData: () => 'test' },
122-
})
123-
await waitFor(() => expect(input.value).toBe(''))
124-
expect(mockOnChange).toHaveBeenCalledWith(['hello', 'world', 'test'])
120+
121+
await userEvent.click(input)
122+
await userEvent.paste('test=')
123+
await userEvent.type(input, 'new ')
124+
125+
expect(mockOnChange).toHaveBeenCalledWith(['hello', 'world', 'test=new'])
125126
})
126127
})

packages/ui/src/components/TagInput/index.tsx

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,7 @@ import {
55
CheckCircleOutlineIcon,
66
CloseIcon,
77
} from '@ultraviolet/icons'
8-
import type {
9-
ChangeEvent,
10-
ClipboardEventHandler,
11-
KeyboardEventHandler,
12-
ReactNode,
13-
} from 'react'
8+
import type { ChangeEvent, KeyboardEventHandler, ReactNode } from 'react'
149
import { useEffect, useId, useMemo, useRef, useState } from 'react'
1510
import { getUUID } from '../../utils'
1611
import { Button } from '../Button'
@@ -124,8 +119,9 @@ export const TagInput = ({
124119
}
125120
}
126121

127-
const onInputChange = (e: ChangeEvent<HTMLInputElement>) =>
122+
const onInputChange = (e: ChangeEvent<HTMLInputElement>) => {
128123
setInput(e.target.value)
124+
}
129125

130126
const addTag = () => {
131127
const newTagInput = input
@@ -179,22 +175,6 @@ export const TagInput = ({
179175
}
180176
}
181177

182-
const handlePaste: ClipboardEventHandler<HTMLInputElement> = e => {
183-
e.preventDefault()
184-
const newTagInput = [
185-
...tagInputState,
186-
{ index: getUUID('tag'), label: e?.clipboardData?.getData('Text') },
187-
]
188-
setTagInput(newTagInput)
189-
setStatus({ [newTagInput.length - 1]: STATUS.LOADING })
190-
try {
191-
dispatchOnChange(newTagInput)
192-
setStatus({ [newTagInput.length - 1]: STATUS.IDLE })
193-
} catch {
194-
setTagInput(tagInputState)
195-
}
196-
}
197-
198178
const clearAll = () => {
199179
setInput('')
200180
setTagInput([])
@@ -272,7 +252,6 @@ export const TagInput = ({
272252
onBlur={addTag}
273253
onChange={onInputChange}
274254
onKeyDown={handleInputKeydown}
275-
onPaste={handlePaste}
276255
placeholder={tagInputState.length === 0 ? placeholder : ''}
277256
readOnly={readOnly}
278257
ref={inputRef}

0 commit comments

Comments
 (0)