Skip to content

Commit e6b84ca

Browse files
committed
Add checks + update type to allow props.value to be null | undefined
also, remove empty string default for props.value to enable alternating between passing undefined and empty string as props.value to clear the component input values (because {value: undefined} no longer gets defaulted to empty string)
1 parent 1236d1f commit e6b84ca

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
lines changed

src/ReactCodeInput.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export interface ReactCodeInputProps {
2121
placeholder?: string
2222

2323
// Value of the input
24-
value?: string
24+
value?: string | null
2525

2626
// Get the full value of the input on every change
2727
onChange?: (value: string) => void

src/ReactCodeInput.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class ReactCodeInput extends Component {
3333
super(props);
3434

3535
const { fields, forceUppercase } = props;
36-
let { value } = props;
36+
let value = props.value || '';
3737

3838
if (forceUppercase) {
3939
value = value.toUpperCase();
@@ -55,6 +55,10 @@ class ReactCodeInput extends Component {
5555
}
5656

5757
UNSAFE_componentWillReceiveProps(nextProps) {
58+
if (nextProps.value == null) {
59+
return;
60+
}
61+
5862
this.setState({
5963
value: nextProps.value,
6064
});
@@ -291,7 +295,6 @@ ReactCodeInput.defaultProps = {
291295
disabled: false,
292296
forceUppercase: false,
293297
fields: 4,
294-
value: '',
295298
type: 'text',
296299
filterKeyCodes: [189, 190],
297300
filterChars: ['-', '.'],

0 commit comments

Comments
 (0)