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

replace unmaintained chip-input lib #61

Merged
merged 4 commits into from
Mar 18, 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
23 changes: 11 additions & 12 deletions packages/gui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,22 @@
"private": true,
"devDependencies": {},
"dependencies": {
"@types/emoji-mart": "^3.0.9",
"@types/react-portal": "^4.0.4",
"@types/vscode": "^1.64.0",
"@types/react-grid-layout": "^1.3.1",
"@emotion/core": "^11.0.0",
"@emotion/styled": "^11.6.0",
"@fortawesome/fontawesome-svg-core": "^1.3.0",
"@fortawesome/free-solid-svg-icons": "^6.0.0",
"@fortawesome/react-fontawesome": "^0.1.17",
"@material-ui/core": "^4.12.3",
"@material-ui/icons": "^4.11.2",
"@material-ui/lab": "^4.0.0-alpha.60",
"@sentry/integrations": "^6.17.6",
"@sentry/react": "^6.17.6",
"@sentry/tracing": "^6.17.6",
"@types/emoji-mart": "^3.0.9",
"@types/react-dom": "^17.0.11",
"@types/react-grid-layout": "^1.3.1",
"@types/react-portal": "^4.0.4",
"@types/vscode": "^1.64.0",
"@vscode-marquee/dialog": "^0.1.0",
"@vscode-marquee/utils": "^0.1.0",
"@vscode-marquee/widget": "^0.1.0",
Expand All @@ -25,18 +32,10 @@
"@vscode-marquee/widget-todo": "^0.1.0",
"@vscode-marquee/widget-weather": "^0.1.0",
"@vscode-marquee/widget-welcome": "^0.1.0",
"@material-ui/core": "^4.12.3",
"@material-ui/icons": "^4.11.2",
"@material-ui/lab": "^4.0.0-alpha.60",
"@sentry/integrations": "^6.17.6",
"@sentry/react": "^6.17.6",
"@sentry/tracing": "^6.17.6",
"@types/react-dom": "^17.0.11",
"copy-to-clipboard": "^3.3.1",
"email-validator": "^2.0.4",
"emoji-mart": "^3.0.1",
"html-escaper": "^3.0.3",
"material-ui-chip-input": "^1.1.0",
"react": "^17.0.2",
"react-animated-slider": "^2.0.0",
"react-debounce-input": "^3.2.5",
Expand Down
89 changes: 89 additions & 0 deletions packages/widget-todo/src/components/ChipInput.tsx
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}
/>
);
}
14 changes: 2 additions & 12 deletions packages/widget-todo/src/dialogs/AddDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import {
Button,
TextField,
} from "@material-ui/core";
import ChipInput from "material-ui-chip-input";

import { theme, MarqueeWindow } from "@vscode-marquee/utils";
import { SplitButton } from "@vscode-marquee/widget";
import { DialogTitle, DialogContainer } from "@vscode-marquee/dialog";

import TodoContext from "../Context";
import ChipInput from "../components/ChipInput";

declare const window: MarqueeWindow;
const options = ['Add to Workspace', 'Add as Global Todo'];
Expand Down Expand Up @@ -61,21 +61,11 @@ const TodoAddDialog = React.memo(({ close }: { close: () => void }) => {
/>
<div style={{ height: "8px", minWidth: "100%" }} />
<ChipInput
newChipKeyCodes={[13, 9]}
blurBehavior="add"
fullWidth
label="Add some tags!"
// helperText="Can be used in filtering."
variant="filled"
value={tags}
onAdd={(tag) => {
setTags([...tags, tag]);
}}
onDelete={(chip, index) => {
const newTags = [...tags];
newTags.splice(index, 1);
setTags(newTags);
}}
onChange={tags => setTags(tags)}
/>
</DialogContent>
<DialogActions style={{ paddingRight: theme.spacing(3) }}>
Expand Down
14 changes: 2 additions & 12 deletions packages/widget-todo/src/dialogs/EditDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import {
import { DialogContainer, DialogTitle } from "@vscode-marquee/dialog";
import { jumpTo, theme } from '@vscode-marquee/utils';

import ChipInput from "material-ui-chip-input";
import LinkIcon from "@material-ui/icons/Link";

import TodoContext from "../Context";
import type { Todo } from '../types';
import ChipInput from "../components/ChipInput";

interface TodoEditDialogParams {
close: () => void
Expand Down Expand Up @@ -77,21 +77,11 @@ const TodoEditDialog = React.memo(({ close, todo }: TodoEditDialogParams) => {
/>
<div style={{ height: "8px", minWidth: "100%" }} />
<ChipInput
newChipKeyCodes={[13, 9]}
blurBehavior="add"
fullWidth
label="Add some tags!"
// helperText="Can be used in filtering."
variant="filled"
value={tags}
onAdd={(tag) => {
setTags([...tags, tag]);
}}
onDelete={(chip, index) => {
const newTags = [...tags];
newTags.splice(index, 1);
setTags(newTags);
}}
onChange={tags => setTags(tags)}
/>
</DialogContent>
<DialogActions style={{ paddingRight: theme.spacing(3) }}>
Expand Down
98 changes: 98 additions & 0 deletions packages/widget-todo/tests/ChipInput.test.tsx
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")!);
Copy link
Contributor Author

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.


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);
});
10 changes: 1 addition & 9 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6003,14 +6003,6 @@ markdown-it@^12.3.2:
mdurl "^1.0.1"
uc.micro "^1.0.5"

material-ui-chip-input@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/material-ui-chip-input/-/material-ui-chip-input-1.1.0.tgz#46550074da358fcfe075350181c315c9a405dc7d"
integrity sha512-95JsxYtzBFUyvzLC0Ae8qo1h8heu9wcSgnGjF/Sy9QQ9pL/ufLVUyjS8uFULW4kEyeNZbZurux3KZKC3FLnoqg==
dependencies:
classnames "^2.2.5"
prop-types "^15.6.1"

md5.js@^1.3.4:
version "1.3.5"
resolved "https://registry.yarnpkg.com/md5.js/-/md5.js-1.3.5.tgz#b5d07b8e3216e3e27cd728d72f70d1e6a342005f"
Expand Down Expand Up @@ -6787,7 +6779,7 @@ prompts@^2.0.1:
kleur "^3.0.3"
sisteransi "^1.0.5"

prop-types@15.x, prop-types@^15.0.0, prop-types@^15.5.10, prop-types@^15.5.4, prop-types@^15.5.8, prop-types@^15.6.0, prop-types@^15.6.1, prop-types@^15.6.2, prop-types@^15.7.2, prop-types@^15.8.1:
prop-types@15.x, prop-types@^15.0.0, prop-types@^15.5.10, prop-types@^15.5.4, prop-types@^15.5.8, prop-types@^15.6.0, prop-types@^15.6.2, prop-types@^15.7.2, prop-types@^15.8.1:
version "15.8.1"
resolved "https://registry.yarnpkg.com/prop-types/-/prop-types-15.8.1.tgz#67d87bf1a694f48435cf332c24af10214a3140b5"
integrity sha512-oj87CgZICdulUohogVAR7AjlC0327U4el4L6eAvOqCeudMDVU0NThNaV+b9Df4dXgSP1gXMTnPdhfe/2qDH5cg==
Expand Down