Skip to content

Commit b0674cd

Browse files
feat: ✨ onKeyUp event callback and custom classNames for tag and input (#24)
* add onkeyup event and className for style the tags and input field (#22) * add onkeyup event and className for style the tags and input field * Refactors Co-authored-by: Moin Khan <moin26944@gmail.com> * chore: 🧹 housekeeping Co-authored-by: Moin Khan <moin26944@gmail.com>
1 parent b6e3740 commit b0674cd

File tree

4 files changed

+18
-7
lines changed

4 files changed

+18
-7
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ export default Example;
6161
| `placeholder` | placeholder for text input | `string` | |
6262
| `value` | initial tags | `string[]` | `[]` |
6363
| `onChange` | onChange callback (added/removed) | `string[]` | |
64+
| `classNames` | className for styling input and tags (i.e {tag:'tag-cls', input: 'input-cls'}) | `object[tag, input]` | |
65+
| `onKeyUp` | input `onKeyUp` callback | `event` | |
6466
| `onBlur` | input `onBlur` callback | `event` | |
6567
| `seprators` | when to add tag (i.e. `Space`,`Enter`) | `string[]` | `["Enter"]` |
6668
| `onExisting` | if tag is already added then callback | `(tag: string) => void` | |

src/classnames.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
* A minimal utility to combine classes
33
*
44
* @export
5-
* @param {(string[] | string)} obj
5+
* @param {(string[] | string | undefined)} obj
66
* @returns {string}
77
*/
8-
export default function cc(...obj: (string | number)[]): string {
9-
return obj.join(" ");
8+
export default function cc(...obj: (string | number | undefined)[]): string {
9+
return obj.filter(c => c).join(" ");
1010
}

src/index.tsx

+10-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ export interface TagsInputProps {
1616
disabled?: boolean;
1717
isEditOnRemove?: boolean;
1818
beforeAddValidate?: (tag: string, existingTags: string[]) => boolean;
19+
onKeyUp?: (e: React.KeyboardEvent<HTMLInputElement>) => void;
20+
classNames?: {
21+
input?: string;
22+
tag?: string;
23+
};
1924
}
2025

2126
// initialize goober once
@@ -74,6 +79,8 @@ export const TagsInput = ({
7479
disabled,
7580
isEditOnRemove,
7681
beforeAddValidate,
82+
onKeyUp,
83+
classNames,
7784
}: TagsInputProps) => {
7885
const [tags, setTags] = useState<any>(value || []);
7986

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

118125
<input
119-
className={cc("rti--input", RTIInput)}
126+
className={cc("rti--input", RTIInput, classNames?.input)}
120127
type="text"
121128
name={name}
122129
placeholder={placeHolder}
123130
onKeyDown={handleOnKeyUp}
124131
onBlur={onBlur}
125132
disabled={disabled}
133+
onKeyUp={onKeyUp}
126134
/>
127135
</div>
128136
);

src/tag.tsx

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ interface TagProps {
66
text: string;
77
remove: any;
88
disabled?: boolean;
9+
className?: string;
910
}
1011

1112
const tagStyles = css({
@@ -30,14 +31,14 @@ const tagStyles = css({
3031
},
3132
});
3233

33-
export default function Tag({ text, remove, disabled }: TagProps) {
34+
export default function Tag({ text, remove, disabled, className }: TagProps) {
3435
const handleOnRemove = e => {
3536
e.stopPropagation();
3637
remove(text);
3738
};
3839

3940
return (
40-
<span className={cc("rti--tag", tagStyles)}>
41+
<span className={cc("rti--tag", tagStyles, className)}>
4142
<span>{text}</span>
4243
{!disabled && (
4344
<button

0 commit comments

Comments
 (0)