Skip to content

Commit 0cdc818

Browse files
authored
Merge pull request #296 from belltalion/fix-input-clear
Fix input clear
2 parents 793111d + 9d7b342 commit 0cdc818

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed

.changeset/late-plants-bet.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@devup-ui/components": patch
3+
---
4+
5+
Add onClear prop in input component

packages/components/src/components/Input/__tests__/index.browser.test.tsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,16 @@ describe('Input', () => {
155155
})
156156
expect(onChange).toHaveBeenCalledWith(expect.any(Object))
157157
})
158+
159+
it('should call onClear props when click clear button', () => {
160+
const onClear = vi.fn()
161+
const { container } = render(<Input onClear={onClear} />)
162+
fireEvent.change(container.querySelector('input')!, {
163+
target: { value: 'test' },
164+
})
165+
fireEvent.click(container.querySelector('[aria-label="clear-button"]')!)
166+
expect(onClear).toHaveBeenCalled()
167+
})
158168
})
159169

160170
describe('Controlled Input', () => {

packages/components/src/components/Input/index.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ interface InputProps extends Omit<ComponentProps<'input'>, 'type'> {
2121
icon?: string
2222
errorMessage?: string
2323
}
24+
onClear?: () => void
2425
colors?: {
2526
primary?: string
2627
error?: string
@@ -49,6 +50,7 @@ export function Input({
4950
className,
5051
classNames,
5152
ref,
53+
onClear,
5254
...props
5355
}: InputProps) {
5456
const [value, setValue] = useState(defaultValue || '')
@@ -58,6 +60,7 @@ export function Input({
5860
}
5961
const handleClear = () => {
6062
setValue('')
63+
onClear?.()
6164
}
6265
const clearButtonVisible = value && !disabled && allowClear
6366

0 commit comments

Comments
 (0)