Skip to content

feat: ✨ onKeyUp event callback and custom classNames for tag and input #24

New issue

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

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

Already on GitHub? # to your account

Merged
merged 2 commits into from
Sep 26, 2022
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ export default Example;
| `placeholder` | placeholder for text input | `string` | |
| `value` | initial tags | `string[]` | `[]` |
| `onChange` | onChange callback (added/removed) | `string[]` | |
| `classNames` | className for styling input and tags (i.e {tag:'tag-cls', input: 'input-cls'}) | `object[tag, input]` | |
| `onKeyUp` | input `onKeyUp` callback | `event` | |
| `onBlur` | input `onBlur` callback | `event` | |
| `seprators` | when to add tag (i.e. `Space`,`Enter`) | `string[]` | `["Enter"]` |
| `onExisting` | if tag is already added then callback | `(tag: string) => void` | |
Expand Down
6 changes: 3 additions & 3 deletions src/classnames.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
* A minimal utility to combine classes
*
* @export
* @param {(string[] | string)} obj
* @param {(string[] | string | undefined)} obj
* @returns {string}
*/
export default function cc(...obj: (string | number)[]): string {
return obj.join(" ");
export default function cc(...obj: (string | number | undefined)[]): string {
return obj.filter(c => c).join(" ");
}
12 changes: 10 additions & 2 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ export interface TagsInputProps {
disabled?: boolean;
isEditOnRemove?: boolean;
beforeAddValidate?: (tag: string, existingTags: string[]) => boolean;
onKeyUp?: (e: React.KeyboardEvent<HTMLInputElement>) => void;
classNames?: {
input?: string;
tag?: string;
};
}

// initialize goober once
Expand Down Expand Up @@ -74,6 +79,8 @@ export const TagsInput = ({
disabled,
isEditOnRemove,
beforeAddValidate,
onKeyUp,
classNames,
}: TagsInputProps) => {
const [tags, setTags] = useState<any>(value || []);

Expand Down Expand Up @@ -112,17 +119,18 @@ export const TagsInput = ({
return (
<div aria-labelledby={name} className={cc("rti--container", RTIContainer)}>
{tags.map(tag => (
<Tag key={tag} text={tag} remove={onTagRemove} disabled={disabled} />
<Tag key={tag} className={classNames?.tag} text={tag} remove={onTagRemove} disabled={disabled} />
))}

<input
className={cc("rti--input", RTIInput)}
className={cc("rti--input", RTIInput, classNames?.input)}
type="text"
name={name}
placeholder={placeHolder}
onKeyDown={handleOnKeyUp}
onBlur={onBlur}
disabled={disabled}
onKeyUp={onKeyUp}
/>
</div>
);
Expand Down
5 changes: 3 additions & 2 deletions src/tag.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ interface TagProps {
text: string;
remove: any;
disabled?: boolean;
className?: string;
}

const tagStyles = css({
Expand All @@ -30,14 +31,14 @@ const tagStyles = css({
},
});

export default function Tag({ text, remove, disabled }: TagProps) {
export default function Tag({ text, remove, disabled, className }: TagProps) {
const handleOnRemove = e => {
e.stopPropagation();
remove(text);
};

return (
<span className={cc("rti--tag", tagStyles)}>
<span className={cc("rti--tag", tagStyles, className)}>
<span>{text}</span>
{!disabled && (
<button
Expand Down