react-tag-input-component icon indicating copy to clipboard operation
react-tag-input-component copied to clipboard

Feature Request: maxLength

Open clipartinc opened this issue 1 year ago • 2 comments

Add the maxLength attribute.

clipartinc avatar Nov 03 '23 23:11 clipartinc

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}
  />
)

tomyedlp avatar Dec 13 '23 03:12 tomyedlp

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"]}

                />
        </>

    );
};```

freitassdev avatar Sep 09 '24 17:09 freitassdev