Skip to content

Commit fc34e61

Browse files
authored
Merge pull request #424 from belltalion/fix-input-value
Fix input handleClear
2 parents dfb714d + ea70553 commit fc34e61

File tree

6 files changed

+139
-44
lines changed

6 files changed

+139
-44
lines changed

.changeset/ten-apples-check.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+
Fix input hadnClear

packages/components/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
"rollup-plugin-preserve-directives": "^0.4.0",
5858
"storybook": "^9.1.10",
5959
"typescript": "^5.9.3",
60-
"vite": "^7.1.7",
60+
"vite": "7.1.10",
6161
"vite-plugin-dts": "^4.5.4",
6262
"vitest": "^3.2.4"
6363
},

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ exports[`Input > should pass props to ClearButton component 1`] = `
114114
<button
115115
aria-label="clear-button"
116116
class="align-items-0-center--1 background-0-var(--negative20,light-dark(#0003,#FFF6))--1 border-0-none--1 border-radius-0-50%--1 height-0-20px--1 width-0-20px--1 color-0-var(--base,light-dark(#FFF,#000))--1 cursor-0-pointer--1 display-0-flex--1 justify-content-0-center--1 padding-0-2px--1 position-0-absolute--1 right-0-12px--1 top-0-50%--1 transform-0-translateY(-50%)--1 "
117+
type="button"
117118
>
118119
<svg
119120
fill="none"

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ describe('Input', () => {
165165
})
166166
fireEvent.click(container.querySelector('[aria-label="clear-button"]')!)
167167
expect(onClear).toHaveBeenCalled()
168+
expect(container.querySelector('input')!.value).toBe('')
168169
})
169170
})
170171

@@ -181,4 +182,13 @@ describe('Controlled Input', () => {
181182
})
182183
expect(container.querySelector('input')!.value).toBe('test')
183184
})
185+
186+
it('should clear value when clear button is clicked', () => {
187+
const { container } = render(<Controlled />)
188+
fireEvent.change(container.querySelector('input')!, {
189+
target: { value: 'test' },
190+
})
191+
fireEvent.click(container.querySelector('[aria-label="clear-button"]')!)
192+
expect(container.querySelector('input')!.value).toBe('')
193+
})
184194
})

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export function Input({
5050
disabled,
5151
className,
5252
classNames,
53-
ref,
53+
readOnly,
5454
onClear,
5555
...props
5656
}: InputProps) {
@@ -63,12 +63,16 @@ export function Input({
6363

6464
const handleClear = () => {
6565
setValue('')
66+
onChangeProp?.({
67+
target: { value: '' },
68+
} as React.ChangeEvent<HTMLInputElement>)
6669
onClear?.()
6770
}
6871

6972
const innerValue = valueProp ?? value
7073

71-
const clearButtonVisible = !!innerValue && !disabled && allowClear
74+
const clearButtonVisible =
75+
!!innerValue && !disabled && allowClear && !readOnly
7276

7377
return (
7478
<Box
@@ -97,7 +101,6 @@ export function Input({
97101
</Center>
98102
)}
99103
<DevupInput
100-
ref={ref}
101104
_disabled={{
102105
_placeholder: {
103106
color: 'var(--inputDisabledText, light-dark(#D6D7DE, #373737))',
@@ -189,6 +192,7 @@ export function ClearButton(props: ComponentProps<'button'>) {
189192
styleOrder={1}
190193
top="50%"
191194
transform="translateY(-50%)"
195+
type="button"
192196
{...props}
193197
>
194198
<svg

0 commit comments

Comments
 (0)