Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

External validation misrender #154

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions example/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {Customizable} from './Customizable';
import {UndoRedo} from './UndoRedo';
import {Textarea} from './Textarea';
import {Ref} from './Ref';
import {MinMax} from './MinMax';
import {DefaultValue} from './DefaultValue';


Expand Down Expand Up @@ -40,6 +41,12 @@ export function App() {
<h2>Example 6. Create with default value</h2>
<DefaultValue />
</section>

<section className="section">
<h2>Example 7. With External Validation</h2>
<MinMax />
<p>Accepts values between 10 and 100</p>
</section>
</div>
);
}
44 changes: 44 additions & 0 deletions example/MinMax.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import React from 'react';
import {DebounceInput} from '../src';


export class MinMax extends React.Component {
state = {
value: null
};

render() {
const {value} = this.state;

const min = 10;
const max = 100;

const handleChange = e => {
let newValue = Number(e.target.value);
if (newValue === 0) {
newValue = null;
} else if (newValue < min) {
newValue = min;
} else if (newValue > max) {
newValue = max;
}
this.setState({value: newValue});
};

return (
<div>
<div className="config">
<DebounceInput
className="input"
type="number"
min={min}
max={max}
value={value}
debounceTimeout={500}
onChange={handleChange} />
<p>Value: {JSON.stringify(value)}</p>
</div>
</div>
);
}
}
8 changes: 4 additions & 4 deletions src/Component.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ export class DebounceInput extends React.PureComponent {
}
const {value, debounceTimeout} = this.props;

const {debounceTimeout: oldTimeout, value: oldValue} = prevProps;
const {debounceTimeout: oldTimeout} = prevProps;
const {value: stateValue} = this.state;

if (typeof value !== 'undefined' && oldValue !== value && stateValue !== value) {
if (typeof value !== 'undefined' && stateValue !== value) {
// Update state.value if new value passed via props, yep re-render should happen
// eslint-disable-next-line react/no-did-update-set-state
this.setState({value});
Expand All @@ -78,8 +78,9 @@ export class DebounceInput extends React.PureComponent {
event.persist();

const {value: oldValue} = this.state;
const {minLength} = this.props;
const {debounceTimeout, minLength} = this.props;

if (debounceTimeout > 0) this.isDebouncing = true;
this.setState({value: event.target.value}, () => {
const {value} = this.state;

Expand Down Expand Up @@ -132,7 +133,6 @@ export class DebounceInput extends React.PureComponent {
}, debounceTimeout);

this.notify = event => {
this.isDebouncing = true;
debouncedChangeFunc(event);
};

Expand Down