Giter Site home page Giter Site logo

Comments (7)

bluebill1049 avatar bluebill1049 commented on July 18, 2024

you are right! want to send us a PR to fix it?

from documentation.

torian257x avatar torian257x commented on July 18, 2024

@bluebill1049 sorry I planned to actually create a PR but somehow it went forgotten

there is still an issue with your commit, there is still
if (!errors.name) return null;

which imho is still wrong

cheers

from documentation.

bluebill1049 avatar bluebill1049 commented on July 18, 2024

please send a PR to help @jossnaz :)

from documentation.

torian257x avatar torian257x commented on July 18, 2024

please send a PR to help @jossnaz :)

I will gladly help. The problem I have, and that's me, is that I don't think the recommended solution is complete. Even with the tipo fixed.

The question I have, how do you use your library for error components?

because what I ended up doing is maybe not optimal.

What I realized is that I need access to the error state on different levels of the component tree, otherwise it's bad. So I exposed the function to access my error message... I read about the FormContext in the comment, but a google search auto corrected my search to formviks solution, so I didn't find the actual formcontext referenced in the code, so i quickly gave up on that formcontext (...)

So maybe even a little hint and with link. And I honestly don't think it's good to hide that component in advanced usage at all. That should be, honestly, the last step of the getting started.

What I have written so far, and I am only using the required error so far, is this:

Example component making use of it:


import React from 'react';
import PropTypes from 'prop-types';
import myappInput from "./myappInput";
import ErrorMessage, {getErrorMessage} from "../ErrorMessage";

const myappGDPRagree = React.forwardRef((props, ref) => {
  return (
     <>
       <myappInput id="gdpr_agree_el" type="checkbox" name="GDPR_Agreement__c" ref={ref}/>
       <label
           className={"myapp-input-checkbox-label " + (getErrorMessage(props.errors, "GDPR_Agreement__c") ? ' myapp-error ' : '')}
           htmlFor="gdpr_agree_el">
         <span>I consent to having MYAPP store my submitted information and sending me future communications on phishing threats and solutions</span>
       </label>
       <ErrorMessage errors={props.errors} name="GDPR_Agreement__c"/>
     </>
  )
});

myappGDPRagree.propTypes = {
  errors: PropTypes.object,
};

export default myappGDPRagree;

Basic error component:

import React from 'react';
const messages = {
  test: {
    min: "This field required minLength of 8"
  },
  required: {
    message: '* This field is required.'
  }
};

export const getErrorMessage = (errors, name) => {
  if(!errors){
    return null;
  }

  if(errors[name] &&  messages[errors[name].type] && messages[errors[name].type].message){
    return messages[errors[name].type].message;
  }else{
    return null;
  }
};

const ErrorMessage = ({ errors, name }) => {


  // Note: if you are using FormContext, then you can use Errors without props eg:
  // const { errors } = useFormContext();
  if (!errors || !errors[name]) return null;

  if(errors[name] &&  messages[errors[name].type] && messages[errors[name].type].message){
    return <span className="myapp-field-error">{messages[errors[name].type].message}</span>;
  }else{
    return null;
  }
};


export default ErrorMessage;

from documentation.

bluebill1049 avatar bluebill1049 commented on July 18, 2024

The question I have, how do you use your library for error components?

What's the confusion in here? I thought our errors object is flat? you just need the name of your input.

https://react-hook-form.com/api#errors

from documentation.

torian257x avatar torian257x commented on July 18, 2024

@bluebill1049

What's the confusion in here? I thought our errors object is flat? you just need the name of your input.

yes, the question basically was whether and if yes, how do you wrap the error message? Don't you create an 'errormessage' component? because right, error messages should look basically the same over the whole app, and as well style the inputs depending on whether there is an error or not. Like, make a red ! and wrap it in a red border or something the like.... Or maybe I'm over complicating things? what would be the best approach in your opinion to do that? because how I do it is using the ErrorMessage component from that docs, but that isn't enough. I have to style the parent to get the right design. That's why I added a function getErrorMessage which I can use to peak if there is an error, then add a class to the parent to get the error styling right for the parent components. But I'm not entirely convinced, that that is the best way.

from documentation.

bluebill1049 avatar bluebill1049 commented on July 18, 2024

It's really up to you on how to handle the errors.

at work, we accept error as a prop and display the error with style and message (which inside the error object). so basically baked inside the input/select component.

from documentation.

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.