Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: maxLength #55

Open
clipartinc opened this issue Nov 3, 2023 · 2 comments
Open

Feature Request: maxLength #55

clipartinc opened this issue Nov 3, 2023 · 2 comments

Comments

@clipartinc
Copy link

Add the maxLength attribute.

@tomyedlp
Copy link

tomyedlp commented Dec 13, 2023

Not the best case but you can do this:

const [availableKeywords, setAvailableKeywords] = useState<string>("Max. 10 keywords")

useEffect(() => {
   setAvailableKeywords(keywords.length === 0 ? `Max. 10 keywords` : `${10-keywords.length} keywords left`)
}, [keywords])

const checkMaxLengthkeywords = () => {
   return keywords.length < 10 ? true : false
}

return (
  <TagsInput
      classNames={{tag: 'keywordTag', input: 'inputImg'}}
      value={keywords}
      onChange={setKeywords}
      name="keywords"
      separators={["Enter",","]}
      placeHolder={availableKeywords}
      beforeAddValidate={checkMaxLengthkeywords}
  />
)

@freitassdev
Copy link

freitassdev commented Sep 9, 2024

you can use something like:

import React, { useCallback, useEffect, useState } from 'react';
import { TagsInput } from "react-tag-input-component";
import toast from 'react-hot-toast';

export default function YourComponent() {
    const [tags, setTags] = useState(["dev", "nextjs"]);

    useEffect(() => {
        if(tags.length > 8) {
            setTags(tags.slice(0, 8));
            toast.error("max 8 tags."); //you can use other toast lib or other way to display the message to user
        }
    }, [tags]);

    return (
        <>
                <TagsInput
                    value={tags}
                    onChange={setTags}
                    name="tags"
                    placeHolder="write the tags"
                    separators={[",", "Enter"]}

                />
        </>

    );
};```

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants