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

Handle errors in the take action forms #1025

Merged
merged 4 commits into from
Apr 24, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
84 changes: 46 additions & 38 deletions ui/src/app/base/components/TagSelector/TagSelector.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Button, Input, Label } from "@canonical/react-components";
import Field from "@canonical/react-components/dist/components/Field";
import classNames from "classnames";
import React, { useEffect, useRef, useState } from "react";

Expand Down Expand Up @@ -116,6 +117,8 @@ const generateSelectedItems = ({ selectedTags, updateTags }) =>
export const TagSelector = ({
allowNewTags = false,
disabled,
error,
help,
initialSelected = [],
label,
onTagsUpdate,
Expand Down Expand Up @@ -157,45 +160,50 @@ export const TagSelector = ({

return (
<div className="tag-selector" ref={wrapperRef}>
<Label onClick={() => setDropdownOpen(true)}>{label}</Label>
{selectedTags.length > 0 && (
<ul className="tag-selector__selected-list">
{generateSelectedItems({ selectedTags, updateTags })}
</ul>
)}
<Input
className={classNames("tag-selector__input", {
"tags-selected": selectedTags.length > 0,
})}
disabled={disabled}
onChange={(e) => setFilter(e.target.value)}
onFocus={() => setDropdownOpen(true)}
onKeyPress={(e) => {
if (e.key === "Enter") {
e.preventDefault();
if (allowNewTags) {
updateTags([...selectedTags, sanitiseFilter(filter)]);
}
}
}}
placeholder={placeholder}
required={required}
type="text"
value={filter}
/>
{dropdownOpen && (
<div className="tag-selector__dropdown">
<ul className="tag-selector__dropdown-list">
{generateDropdownItems({
allowNewTags,
filter,
selectedTags,
tags,
updateTags,
})}
<Field
error={error}
help={help}
label={<Label onClick={() => setDropdownOpen(true)}>{label}</Label>}
>
{selectedTags.length > 0 && (
<ul className="tag-selector__selected-list">
{generateSelectedItems({ selectedTags, updateTags })}
</ul>
</div>
)}
)}
<Input
className={classNames("tag-selector__input", {
"tags-selected": selectedTags.length > 0,
})}
disabled={disabled}
onChange={(e) => setFilter(e.target.value)}
onFocus={() => setDropdownOpen(true)}
onKeyPress={(e) => {
if (e.key === "Enter") {
e.preventDefault();
if (allowNewTags) {
updateTags([...selectedTags, sanitiseFilter(filter)]);
}
}
}}
placeholder={placeholder}
required={required}
type="text"
value={filter}
/>
{dropdownOpen && (
<div className="tag-selector__dropdown">
<ul className="tag-selector__dropdown-list">
{generateDropdownItems({
allowNewTags,
filter,
selectedTags,
tags,
updateTags,
})}
</ul>
</div>
)}
</Field>
</div>
);
};
Expand Down
10 changes: 5 additions & 5 deletions ui/src/app/base/components/TagSelector/TagSelector.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { shallow } from "enzyme";
import { mount, shallow } from "enzyme";
import React from "react";

import TagSelector from "./TagSelector";
Expand Down Expand Up @@ -26,20 +26,20 @@ describe("TagSelector", () => {
});

it("renders and matches the snapshot when opened", () => {
const component = shallow(
const component = mount(
<TagSelector
label="Tags"
placeholder="Select or create tags"
onTagsUpdate={jest.fn()}
tags={tags}
/>
);
component.find("Label").simulate("click");
component.find("Label").first().simulate("click");
expect(component).toMatchSnapshot();
});

it("renders and matches the snapshot with tag descriptions", () => {
const component = shallow(
const component = mount(
<TagSelector
label="Tags"
placeholder="Select or create tags"
Expand All @@ -50,7 +50,7 @@ describe("TagSelector", () => {
]}
/>
);
component.find("Label").simulate("click");
component.find("Label").first().simulate("click");
expect(component).toMatchSnapshot();
});

Expand Down
Loading