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

Commit

Permalink
fix(Input): add blur event handler
Browse files Browse the repository at this point in the history
  • Loading branch information
Cecilia Woodward committed Aug 21, 2021
1 parent c1eecfb commit db8cc56
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
2 changes: 1 addition & 1 deletion stories/Card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export function Card({
)}
layout={layout}
>
{!loading && children}
{children}
</Grid>
<Spinner
className={styles.spinner}
Expand Down
13 changes: 8 additions & 5 deletions stories/Input.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import classNames from 'classnames';
import React, { ChangeEventHandler, useEffect, useState } from 'react';
import React, { useEffect, useState } from 'react';
import styles from './Input.scss';

export interface InputProps {
Expand All @@ -10,24 +10,26 @@ export interface InputProps {
/**
* Invoked when the checkbox checked value is modified internally.
*/
onChange?: (change: string) => void;
onChange?: (value: string) => void;
onBlur?: (value: string) => void;
}

export function Input({
className,
value,
placeholder,
onChange,
onBlur,
}: InputProps): JSX.Element {
const [internalValue, setInternalValue] = useState<string>();

useEffect(() => {
setInternalValue(value);
}, [value]);

const updateValue: ChangeEventHandler<HTMLInputElement> = (event) => {
const updateValue = (event, fn: (value: string) => void) => {
setInternalValue(event.target.value);
if (onChange) onChange(internalValue);
if (fn) fn(internalValue);
}

return (
Expand All @@ -40,7 +42,8 @@ export function Input({
<input
placeholder={placeholder}
value={internalValue || ''}
onChange={updateValue}
onBlur={(event) => updateValue(event, onBlur)}
onChange={(event) => updateValue(event, onChange)}
/>
</div>
);
Expand Down

0 comments on commit db8cc56

Please sign in to comment.