Skip to content

Commit b79c7bc

Browse files
committed
Fix input
1 parent 14a16e8 commit b79c7bc

File tree

3 files changed

+23
-8
lines changed

3 files changed

+23
-8
lines changed

.changeset/breezy-bees-help.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 to controlled

packages/components/src/components/Input/Controlled.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,11 @@ import { Input } from '.'
55
export function Controlled() {
66
const [value, setValue] = useState('')
77

8-
return <Input onChange={(e) => setValue(e.target.value)} value={value} />
8+
return (
9+
<Input
10+
onChange={(e) => setValue(e.target.value)}
11+
onClear={() => setValue('')}
12+
value={value}
13+
/>
14+
)
915
}

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

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ interface InputProps extends Omit<ComponentProps<'input'>, 'type'> {
3838
}
3939

4040
export function Input({
41-
defaultValue,
41+
defaultValue = '',
4242
value: valueProp,
4343
onChange: onChangeProp,
4444
typography,
@@ -48,22 +48,26 @@ export function Input({
4848
icon,
4949
colors,
5050
disabled,
51-
className,
52-
classNames,
51+
className = '',
52+
classNames = {},
5353
ref,
5454
onClear,
5555
...props
5656
}: InputProps) {
57-
const [value, setValue] = useState(defaultValue || '')
57+
const [value, setValue] = useState(defaultValue)
5858
const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
5959
setValue(e.target.value)
6060
onChangeProp?.(e)
6161
}
62+
6263
const handleClear = () => {
6364
setValue('')
6465
onClear?.()
6566
}
66-
const clearButtonVisible = value && !disabled && allowClear
67+
68+
const finalValue = valueProp ?? value
69+
70+
const clearButtonVisible = finalValue && !disabled && allowClear
6771

6872
return (
6973
<Box
@@ -122,7 +126,7 @@ export function Input({
122126
borderRadius="8px"
123127
borderStyle="solid"
124128
borderWidth="1px"
125-
className={`${className || ''} ${classNames?.input || ''}`.trim()}
129+
className={`${className} ${classNames.input}`.trim()}
126130
disabled={disabled}
127131
onChange={handleChange}
128132
pl={icon ? '36px' : '12px'}
@@ -142,7 +146,7 @@ export function Input({
142146
}}
143147
transition="all 0.1s ease-in-out"
144148
typography={typography}
145-
value={valueProp ?? value}
149+
value={finalValue}
146150
{...props}
147151
/>
148152
{clearButtonVisible && <ClearButton onClick={handleClear} />}

0 commit comments

Comments
 (0)