Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/late-plants-bet.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@devup-ui/components": patch
---

Add onClear prop in input component
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,16 @@ describe('Input', () => {
})
expect(onChange).toHaveBeenCalledWith(expect.any(Object))
})

it('should call onClear props when click clear button', () => {
const onClear = vi.fn()
const { container } = render(<Input onClear={onClear} />)
fireEvent.change(container.querySelector('input')!, {
target: { value: 'test' },
})
fireEvent.click(container.querySelector('[aria-label="clear-button"]')!)
expect(onClear).toHaveBeenCalled()
})
})

describe('Controlled Input', () => {
Expand Down
3 changes: 3 additions & 0 deletions packages/components/src/components/Input/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ interface InputProps extends Omit<ComponentProps<'input'>, 'type'> {
icon?: string
errorMessage?: string
}
onClear?: () => void
colors?: {
primary?: string
error?: string
Expand Down Expand Up @@ -49,6 +50,7 @@ export function Input({
className,
classNames,
ref,
onClear,
...props
}: InputProps) {
const [value, setValue] = useState(defaultValue || '')
Expand All @@ -58,6 +60,7 @@ export function Input({
}
const handleClear = () => {
setValue('')
onClear?.()
}
const clearButtonVisible = value && !disabled && allowClear

Expand Down