Skip to content
This repository has been archived by the owner on Sep 9, 2022. It is now read-only.

Commit

Permalink
fix(Input): onChange handler is required
Browse files Browse the repository at this point in the history
  • Loading branch information
cecilia-sanare committed Aug 21, 2021
1 parent 2d4c2d9 commit 1e7329d
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions stories/Input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ export interface InputProps {
/**
* Invoked when the checkbox checked value is modified internally.
*/
onChange?: (value: string) => void;
onBlur?: (value: string) => void;
onChange: (value: string) => void;

updateOn?: UpdateOn;
}
Expand All @@ -30,14 +29,6 @@ export function Input({
setInternalValue(value);
}, [value]);

const updateValue = (event, fn: (value: string) => void) => {
const updatedValue = event.target.value;
if (updatedValue === internalValue) return;

setInternalValue(updatedValue);
if (fn) fn(updatedValue);
}

return (
<div
className={classNames(
Expand All @@ -48,12 +39,19 @@ export function Input({
<input
placeholder={placeholder}
value={internalValue || ''}
onBlur={updateOn === UpdateOn.Blur ? (event) => updateValue(event, onChange) : null}
onInput={updateOn === UpdateOn.Input ? (event) => updateValue(event, onChange) : null}
onBlur={updateOn === UpdateOn.Blur ? () => onChange(internalValue) : null}
onChange={(event) => {
const updatedValue = event.target.value;
setInternalValue(updatedValue);

if (updateOn === UpdateOn.Input) {
onChange(updatedValue);
}
}}
onKeyDown={updateOn === UpdateOn.Blur ? (event) => {
if (event.key !== 'Enter') return;

updateValue(event, onChange);
onChange(internalValue);
} : null}
/>
</div>
Expand Down

0 comments on commit 1e7329d

Please sign in to comment.