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

Add a custom function to generate tag id #89

Merged
merged 3 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,9 @@ type TagInputProps = {

// Use a popover to display tags instead of inline.
usePopoverForTags?: boolean; // default: false

// A callback function that generates an id for a newly created tag.
generateTagId?: () => string; // default: crypto.getRandomValues(new Uint32Array(1))[0].toString
};
```

Expand Down
3 changes: 3 additions & 0 deletions packages/emblor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,9 @@ type TagInputProps = {

// Use a popover to display tags instead of inline.
usePopoverForTags?: boolean; // default: false

// A callback function to generate an id for a newly created tag
generateTagId?: () => string; // default: utils/uuid
};
```

Expand Down
8 changes: 5 additions & 3 deletions packages/emblor/src/tag/tag-input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ export interface TagInputProps extends OmittedInputProps, VariantProps<typeof ta
usePortal?: boolean;
addOnPaste?: boolean;
addTagsOnBlur?: boolean;
generateTagId?: () => string;
}

const TagInput = React.forwardRef<HTMLInputElement, TagInputProps>((props, ref) => {
Expand Down Expand Up @@ -146,6 +147,7 @@ const TagInput = React.forwardRef<HTMLInputElement, TagInputProps>((props, ref)
disabled = false,
usePortal = false,
addOnPaste = false,
generateTagId = uuid,
} = props;

const [inputValue, setInputValue] = React.useState('');
Expand Down Expand Up @@ -191,7 +193,7 @@ const TagInput = React.forwardRef<HTMLInputElement, TagInputProps>((props, ref)
return;
}

const newTagId = uuid();
const newTagId = generateTagId();

// Add tag if duplicates are allowed or tag does not already exist
if (allowDuplicates || !tags.some((tag) => tag.text === newTagText)) {
Expand Down Expand Up @@ -241,7 +243,7 @@ const TagInput = React.forwardRef<HTMLInputElement, TagInputProps>((props, ref)
(allowDuplicates || !tags.some((tag) => tag.text === newTagText)) &&
(maxTags === undefined || tags.length < maxTags)
) {
const newTagId = crypto.getRandomValues(new Uint32Array(1))[0].toString();
const newTagId = generateTagId();
setTags([...tags, { id: newTagId, text: newTagText }]);
onTagAdd?.(newTagText);
setTagCount((prevTagCount) => prevTagCount + 1);
Expand Down Expand Up @@ -280,7 +282,7 @@ const TagInput = React.forwardRef<HTMLInputElement, TagInputProps>((props, ref)
return;
}

const newTagId = uuid();
const newTagId = generateTagId();

if (
newTagText &&
Expand Down
4 changes: 3 additions & 1 deletion website/components/tag/tag-input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export interface TagInputProps extends OmittedInputProps, VariantProps<typeof ta
onClearAll?: () => void;
inputProps?: React.InputHTMLAttributes<HTMLInputElement>;
restrictTagsToAutocompleteOptions?: boolean;
generateTagId?: () => string;
}

const TagInput = React.forwardRef<HTMLInputElement, TagInputProps>((props, ref) => {
Expand Down Expand Up @@ -106,6 +107,7 @@ const TagInput = React.forwardRef<HTMLInputElement, TagInputProps>((props, ref)
usePopoverForTags = false,
inputProps = {},
restrictTagsToAutocompleteOptions,
generateTagId = crypto.getRandomValues(new Uint32Array(1))[0].toString,
} = props;

const [inputValue, setInputValue] = React.useState('');
Expand Down Expand Up @@ -168,7 +170,7 @@ const TagInput = React.forwardRef<HTMLInputElement, TagInputProps>((props, ref)
return;
}

const newTagId = crypto.getRandomValues(new Uint32Array(1))[0].toString();
const newTagId = generateTagId();

if (
newTagText &&
Expand Down
7 changes: 7 additions & 0 deletions website/content/docs/api-reference.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,13 @@ return (
<TableCell className="border-r">Customizes the rendering of tags.</TableCell>
<TableCell>No</TableCell>
</TableRow>
<TableRow>
<TableCell className="border-r">generateTagId</TableCell>
<TableCell className="border-r">null</TableCell>
<TableCell className="border-r">Function</TableCell>
<TableCell className="border-r">Generates an id for a newly created tag.</TableCell>
<TableCell>No</TableCell>
</TableRow>
</TableBody>
</Table>
import {custom} from "zod"
4 changes: 3 additions & 1 deletion website/legacy/content/snippets/tag-input.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export interface TagInputProps extends OmittedInputProps, VariantProps<typeof ta
onClearAll?: () => void;
inputProps?: React.InputHTMLAttributes<HTMLInputElement>;
restrictTagsToAutocompleteOptions?: boolean;
generateTagId?: () => string;
}

const TagInput = React.forwardRef<HTMLInputElement, TagInputProps>((props, ref) => {
Expand Down Expand Up @@ -112,6 +113,7 @@ const TagInput = React.forwardRef<HTMLInputElement, TagInputProps>((props, ref)
usePopoverForTags = false,
inputProps = {},
restrictTagsToAutocompleteOptions,
generateTagId = crypto.getRandomValues(new Uint32Array(1))[0].toString;
} = props;

const [inputValue, setInputValue] = React.useState('');
Expand Down Expand Up @@ -174,7 +176,7 @@ const TagInput = React.forwardRef<HTMLInputElement, TagInputProps>((props, ref)
return;
}

const newTagId = crypto.getRandomValues(new Uint32Array(1))[0].toString();
const newTagId = generateTagId();

if (
newTagText &&
Expand Down
Loading