Comments (2)
Brilliant thanks, I went with a custom variation of your bootstrap example!
from react-form-with-constraints.
Good question!
I've just released v0.8.0. Took me a lot of time because it's a full rewrite (multiple rewrites actually, to find the good recipe). A few things have changed since v0.7.0.
I do change the input border color for Bootstrap with react-form-with-constraints-bootstrap4, example: https://github.com/tkrotoff/react-form-with-constraints/blob/v0.8.0/examples/Bootstrap4/App.jsx#L88
FormInput
implementation is here: https://github.com/tkrotoff/react-form-with-constraints/blob/v0.8.0/packages/react-form-with-constraints-bootstrap4/src/Bootstrap.tsx#L19
You have 2 events: FieldWillValidate
and FieldDidValidate
, then you change the state
based on them (and then change the input className or whatever).
- Your approach (based on examples/Password):
getInputBorderColor(fieldName: string) {
let color = undefined;
if (this.form !== null) { // On first render, this.form is null
const field = this.form.fieldsStore.getField(fieldName);
if (field.hasAnyFeedbacks()) { // More or less tells if the field is dirty (e.g. has been touched by the user)
color = field.isValid() ? 'green' : 'red';
}
}
return color;
}
// ...
<FormWithConstraints ref={formWithConstraints => this.form = formWithConstraints}
onSubmit={this.handleSubmit} noValidate>
<input type="email" name="username" id="username"
value={this.state.username} onChange={this.handleChange}
required minLength={5}
style={{borderWidth: '1em', borderColor: this.getInputBorderColor('username')}}
/>
<input type="password" name="password" id="password"
ref={password => this.password = password}
value={this.state.password} onChange={this.handlePasswordChange}
required pattern=".{5,}"
style={{borderWidth: '1em', borderColor: this.getInputBorderColor('password')}}
/>
</FormWithConstraints>
It works because there is a re-render each time an input is changed (this.state.username
)
- There is another approach by playing with
FormWithConstraints.validateFields()
:
const field = await this.form.validateFields(myField); // "field" being identical to "const field = this.form.fieldsStore.getField(...)"
this.setState({myFieldIsValid: field.isValid()});
Why not move FormInput
from react-form-with-constraints-bootstrap4 to react-form-with-constraints.
Also isValid()
is confusing because it does not check if the field is dirty.
from react-form-with-constraints.
Related Issues (20)
- Use webpack tree shaking HOT 1
- Rename username to email in the examples HOT 1
- Remove TSLint HOT 1
- Required field validation does not work properly with class-based components HOT 2
- Rework examples/PlainOldReact?
- Show something while a field is untouched, when dirty replace by feedbacks HOT 2
- Returns HTMLElement in Field HOT 1
- Return the input state and form state using hooks HOT 1
- Crash "Can't perform a React state update on an unmounted component"
- lib/ never seems to be used, only lib-es5/ HOT 1
- componentWillMount is deprecated HOT 1
- Use Array.flat(Infinity)?
- In .vscode/launch.json, replace workspaceRoot by workspaceFolder HOT 1
- Remove IE 10 support
- Issues with warning and info FieldFeedback type HOT 3
- "Multiple elements matching 'name' inside the form" error HOT 1
- Async await on emitEvent HOT 1
- Validation errors not disappearing HOT 1
- Add unit tests to examples Bootstrap4 and Password
- Form with constraints components dont work with react 18 HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-form-with-constraints.