Skip to content

Commit

Permalink
Add initialValue prop to UncontrolledTextInput
Browse files Browse the repository at this point in the history
Fixes #65
  • Loading branch information
vadimdemedes committed Feb 3, 2022
1 parent e3f12a9 commit 1dd0f9f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ Function to call when `Enter` is pressed, where first argument is a value of the
## Uncontrolled usage

This component also exposes an [uncontrolled](https://reactjs.org/docs/uncontrolled-components.html) version, which handles `value` changes for you. To receive the final input value, use `onSubmit` prop.
Initial value can be specified via `initialValue` prop, which is supported only in `UncontrolledTextInput` component.

```jsx
import React from 'react';
Expand Down
19 changes: 14 additions & 5 deletions source/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,18 @@ const TextInput: FC<Props> = ({

export default TextInput;

export const UncontrolledTextInput: FC<Except<Props, 'value' | 'onChange'>> =
props => {
const [value, setValue] = useState('');
interface UncontrolledProps extends Except<Props, 'value' | 'onChange'> {
/**
* Initial value.
*/
initialValue?: string;
}

return <TextInput {...props} value={value} onChange={setValue} />;
};
export const UncontrolledTextInput: FC<UncontrolledProps> = ({
initialValue = '',
...props
}) => {
const [value, setValue] = useState(initialValue);

return <TextInput {...props} value={value} onChange={setValue} />;
};

0 comments on commit 1dd0f9f

Please sign in to comment.