react-multi-email icon indicating copy to clipboard operation
react-multi-email copied to clipboard

Support async-validate emails

Open jopemachine opened this issue 4 years ago • 4 comments

Changes

  • Support async-validate emails

Notes

I tested this after removing node-sass due to node-sass issue.

Please let me know if node-sass should be removed from dev-dependency.

Manual Test

Test code


// validateEmail={this.asyncTest}
  asyncTest <=> async () => {
    return new Promise<boolean>((resolve, reject) => {
      setTimeout(() => resolve(true), 1000);
    });
  };

...

<ReactMultiEmail
  placeholder="Input your email"
  emails={emails}
  onChange={(_emails: string[]) => {
    this.setState({ emails: _emails });
  }}
  validateEmail={this.asyncTest}
  getLabel={(
    email: string,
    index: number,
    removeEmail: (index: number) => void,
  ) => {
    return (
      <div data-tag key={index}>
        <div data-tag-item>{email}</div>
        <span data-tag-handle onClick={() => removeEmail(index)}>
          ×
        </span>
      </div>
    );
  }}
/>;

Result

Sep-17-2020 14-14-48

An email was added when the validation returned true one second after entering the enter.

jopemachine avatar Sep 17 '20 05:09 jopemachine

Great work! Thank you. but can you add a spinning state when validating emails?

thomasJang avatar Sep 23 '20 02:09 thomasJang

Great work! Thank you. but can you add a spinning state when validating emails?

Thanks for your feedback.

I think some users may not like that the spinner is in the EmailComponent.

And some users might want to change their spinner's style.

So, I created an enableSpinner flag so that the spinner does not come out when the value is false.

I think users can use their spinner in the way below.

  asyncTest = async () => {
    return new Promise<boolean>((resolve, reject) => {
      // Show user's spinner
      this.setState({ validating: true })
      setTimeout(() => {
        resolve(true);
        // Hide user's spinner
        this.setState({ validating: false })
      }, 1000);
    });
  };

...

          <ReactMultiEmail
            placeholder="Input your email"
            emails={emails}
            onChange={(_emails: string[]) => {
              this.setState({ emails: _emails });
            }}
            validateEmail={this.asyncTest}
            enableSpinner={false}
            getLabel={(
              email: string,
              index: number,
              removeEmail: (index: number) => void,
            ) => {
              return (
                <div data-tag key={index}>
                  <div data-tag-item>
                    {email}
                  </div>
                  <span data-tag-handle onClick={() => removeEmail(index)}>
                    ×
                  </span>
                </div>
              );
            }}
          />


And users who want to spinner of ReactMultiEmail,

just can write this.

          <ReactMultiEmail
            placeholder="Input your email"
            emails={emails}
            onChange={(_emails: string[]) => {
              this.setState({ emails: _emails });
            }}
            validateEmail={this.asyncTest}
            getLabel={(
              email: string,
              index: number,
              removeEmail: (index: number) => void,
            ) => {
              return (
                <div data-tag key={index}>
                  <div data-tag-item>
                    {email}
                  </div>
                  <span data-tag-handle onClick={() => removeEmail(index)}>
                    ×
                  </span>
                </div>
              );
            }}
          />


The below gif is the demo using the spinner that I made.

(I didn't use react-spinner or other libs because I think we should keep no-dependency. Otherwise, just let me know).

Demo

Please let me know if spinner's design doesn't suit here or if there are other issues.

jopemachine avatar Sep 23 '20 06:09 jopemachine

I'm not sure this repository is still managed.

So, I made a forked version of this repository.

https://www.npmjs.com/package/@jopemachine/react-multi-email

This version support below additional features

This version could be removed when the above PRs are merged or close.

jopemachine avatar Feb 12 '21 09:02 jopemachine

@jopemachine Can you update the package to capture the onChange of the value typed in the input box?

shibinNeoito avatar Jun 09 '22 13:06 shibinNeoito