Git Product home page Git Product logo

Comments (2)

ryanhyslop avatar ryanhyslop commented on May 20, 2024 1

Brilliant thanks, I went with a custom variation of your bootstrap example!

from react-form-with-constraints.

tkrotoff avatar tkrotoff commented on May 20, 2024

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).

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.