Last active
December 13, 2017 00:37
-
-
Save iurii-kyrylenko/92c067543a9f990cd1ddcb1ae958873a to your computer and use it in GitHub Desktop.
neoform: Validation out of sync with input. Solution.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/****************************************************** | |
* Validation out of sync with input. | |
* https://www.webpackbin.com/bins/-L0C88aBjR-vv57klNfU | |
******************************************************/ | |
import React from 'react'; | |
import { field } from 'neoform'; | |
import { fieldValidation } from 'neoform-validation'; | |
const renderError = (status, message) => { | |
if (status !== false) { | |
return null; | |
} | |
return ( | |
<span>{message}</span> | |
); | |
}; | |
const MyInput = ({ | |
value = '', | |
onChange, | |
validate, | |
validationStatus, | |
validationMessage, | |
...props | |
}) => { | |
const style = { | |
backgroundColor: validationStatus === false ? 'red' : 'white' | |
}; | |
return ( | |
<span> | |
<input | |
{...props} | |
style={style} | |
value={value} | |
onChange={e => { | |
onChange(e.target.value) | |
validate() | |
}} | |
/> | |
{renderError(validationStatus, validationMessage)} | |
</span> | |
); | |
}; | |
export default field(fieldValidation(MyInput)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/****************************************************** | |
* Fixed issue: Now validation in sync with input. | |
* https://www.webpackbin.com/bins/-L0BnQRlkKqnJwIeHrzl | |
******************************************************/ | |
import React from 'react'; | |
import { field } from 'neoform'; | |
import { fieldValidation } from 'neoform-validation'; | |
const renderError = (status, message) => { | |
if (status !== false) { | |
return null; | |
} | |
return ( | |
<span>{message}</span> | |
); | |
}; | |
const MyInput = ({ | |
value = '', | |
onChange, | |
validate, | |
validationStatus, | |
validationMessage, | |
...props | |
}) => { | |
const style = { | |
backgroundColor: validationStatus === false ? 'red' : 'white' | |
}; | |
return ( | |
<span> | |
<input | |
{...props} | |
style={style} | |
value={value} | |
onChange={(e) => { | |
onChange(e.target.value) | |
setTimeout(() => validate(), 0) | |
}} | |
/> | |
{renderError(validationStatus, validationMessage)} | |
</span> | |
); | |
}; | |
export default field(fieldValidation(MyInput)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/****************************************************** | |
* onBlur/OnChange example. | |
* Validation in sync with input. | |
* https://www.webpackbin.com/bins/-L0Bprgrd2Ik5l4FFPAY | |
******************************************************/ | |
import React from 'react'; | |
import { field } from 'neoform'; | |
import { fieldValidation } from 'neoform-validation'; | |
const renderError = (status, message) => { | |
if (status !== false) { | |
return null; | |
} | |
return ( | |
<span>{message}</span> | |
); | |
}; | |
const MyInput = ({ | |
value = '', | |
onChange, | |
validate, | |
validationStatus, | |
validationMessage, | |
...props | |
}) => { | |
const style = { | |
backgroundColor: validationStatus === false ? 'red' : 'white' | |
}; | |
return ( | |
<span> | |
<input | |
{...props} | |
style={style} | |
value={value} | |
onBlur={validate} | |
onChange={(e) => { | |
onChange(e.target.value) | |
if (!validationStatus) { | |
setTimeout(() => validate(), 0) | |
} | |
}} | |
/> | |
{renderError(validationStatus, validationMessage)} | |
</span> | |
); | |
}; | |
export default field(fieldValidation(MyInput)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Exploring neoform validation
Proposed solution
setTimeout(() => validate(), 0)
: perform validation at the next turn of event loop.It would be nice to solve the
out of sync issue
inside the neoform package.