-
Notifications
You must be signed in to change notification settings - Fork 15
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
replace unmaintained chip-input lib #61
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import React, { KeyboardEvent } from "react"; | ||
import Chip from "@material-ui/core/Chip"; | ||
import TextField from "@material-ui/core/TextField"; | ||
import { InputAdornment, TextFieldProps } from "@material-ui/core"; | ||
import { makeStyles } from "@material-ui/core/styles"; | ||
|
||
const useStyles = makeStyles(() => ({ | ||
textField: { | ||
marginTop: 5, | ||
}, | ||
})); | ||
|
||
type Props = Omit<TextFieldProps, "value" | "onChange"> & { | ||
value: Array<string>; | ||
onChange: (tags: Array<string>) => void; | ||
}; | ||
|
||
export default function TagsInput({ | ||
onChange, | ||
value, | ||
onBlur, | ||
...inputProps | ||
}: Props) { | ||
const [inputValue, setInputValue] = React.useState(""); | ||
const classes = useStyles(); | ||
|
||
const addTag = () => { | ||
if (inputValue.length > 0 && !value.includes(inputValue)) { | ||
// only add new tag if it's not a duplicate | ||
onChange([...value, inputValue]); | ||
} | ||
setInputValue(""); | ||
}; | ||
|
||
const handleKeyDown = ( | ||
event: KeyboardEvent<HTMLInputElement | HTMLTextAreaElement> | ||
) => { | ||
if (event.key === "Enter") { | ||
addTag(); | ||
} | ||
if ( | ||
event.key === "Backspace" && | ||
value.length > 0 && | ||
inputValue.length === 0 | ||
) { | ||
// Removing last tag on backspace, and populating the text input with the | ||
// respective value. | ||
event.preventDefault(); | ||
|
||
const lastItem = value.slice(-1).pop()!; | ||
setInputValue(lastItem); | ||
onChange(value.slice(0, -1)); | ||
} | ||
}; | ||
|
||
const handleDelete = (item: string) => () => { | ||
onChange(value.filter((it) => it !== item)); | ||
}; | ||
|
||
const handleBlur = (event: React.FocusEvent<HTMLInputElement, Element>) => { | ||
addTag(); | ||
if (onBlur) { | ||
onBlur(event); | ||
} | ||
}; | ||
|
||
return ( | ||
<TextField | ||
value={inputValue} | ||
InputProps={{ | ||
className:classes.textField, | ||
startAdornment: value.map((item) => ( | ||
<InputAdornment key={item} position="start"> | ||
<Chip | ||
key={item} | ||
tabIndex={-1} | ||
label={item} | ||
onDelete={handleDelete(item)} | ||
/> | ||
</InputAdornment> | ||
)), | ||
onChange: (event) => setInputValue(event.target.value), | ||
onBlur: handleBlur, | ||
onKeyDown: handleKeyDown, | ||
}} | ||
{...inputProps} | ||
/> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import React from "react"; | ||
import { render, screen } from "@testing-library/react"; | ||
import ChipInput from "../src/components/ChipInput"; | ||
import userEvent from "@testing-library/user-event"; | ||
|
||
const noop = () => undefined; | ||
|
||
test("renders label", async () => { | ||
const label = "Add some tags!"; | ||
render(<ChipInput label={label} value={[]} onChange={noop} />); | ||
|
||
expect(screen.getByText(label)).toBeInTheDocument(); | ||
}); | ||
|
||
test("renders tags", async () => { | ||
const tags = ["tagOne", "tagTwo"]; | ||
render(<ChipInput value={tags} onChange={noop} />); | ||
|
||
tags.forEach((tag) => | ||
expect(screen.getByRole("button", { name: tag })).toBeInTheDocument() | ||
); | ||
}); | ||
|
||
test("triggers `onChange` with new value when enter is pressed", async () => { | ||
const onChange = jest.fn(); | ||
const oldTags = ["OldTag"]; | ||
|
||
render(<ChipInput value={oldTags} onChange={onChange} />); | ||
|
||
const userInputText = "NewTag"; | ||
userEvent.type(screen.getByRole("textbox"), userInputText + "{enter}"); | ||
|
||
expect(onChange).toHaveBeenCalledWith([...oldTags, userInputText]); | ||
}); | ||
|
||
test("triggers `onChange` with new value when input is blurred", async () => { | ||
const onChange = jest.fn(); | ||
const oldTags = ["OldTag"]; | ||
|
||
render(<ChipInput value={oldTags} onChange={onChange} />); | ||
|
||
const userInputText = "NewTag"; | ||
userEvent.type(screen.getByRole("textbox"), userInputText); | ||
|
||
// to trigger blur | ||
userEvent.tab(); | ||
|
||
expect(onChange).toHaveBeenCalledWith([...oldTags, userInputText]); | ||
}); | ||
|
||
test("doenst trigger `onChange` when new value is duplicate", async () => { | ||
const onChange = jest.fn(); | ||
const newTag = "NewTag"; | ||
const oldTags = [newTag]; | ||
|
||
render(<ChipInput value={oldTags} onChange={onChange} />); | ||
|
||
const userInputText = "NewTag"; | ||
userEvent.type(screen.getByRole("textbox"), userInputText + "{enter}"); | ||
|
||
expect(onChange).not.toBeCalled(); | ||
}); | ||
|
||
test("Clicking the X removes a tag", async () => { | ||
const onChange = jest.fn(); | ||
const tagToRemoveLabel = "tag-to-remove"; | ||
const oldTags = ["some-tag", tagToRemoveLabel, "other-tag"]; | ||
|
||
render(<ChipInput value={oldTags} onChange={onChange} />); | ||
|
||
const tagToRemove = screen.getByRole("button", { name: tagToRemoveLabel }); | ||
|
||
userEvent.click(tagToRemove.querySelector("svg")!); | ||
|
||
expect(onChange).toHaveBeenCalledWith( | ||
oldTags.filter((tag) => tag !== tagToRemoveLabel) | ||
); | ||
}); | ||
|
||
test("Typing backspace when input is empty starts editing the last tag", async () => { | ||
const onChange = jest.fn(); | ||
const tagToEdit = "tag-to-edit"; | ||
const oldTags = [tagToEdit]; | ||
|
||
render(<ChipInput value={oldTags} onChange={onChange} />); | ||
|
||
// hitting backspace when input has content just removes content | ||
userEvent.type( | ||
screen.getByRole("textbox"), | ||
"ABC{backspace}{backspace}{backspace}" | ||
); | ||
expect(onChange).not.toHaveBeenCalled(); | ||
|
||
// now that the input is empty, is starts editing the last tag | ||
userEvent.type(screen.getByRole("textbox"), "{backspace}"); | ||
expect(onChange).toHaveBeenCalledWith([]); | ||
expect(screen.getByRole("textbox")).toHaveValue(tagToEdit); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately the MUI Chip (and MUI generally) has not the greatest a11y (as they admit themselves), which makes it also harder to test at times. So I have to directly address the svg tag.
Interestingly in their own tag, they swap out the default delete icon to test it.