-
Notifications
You must be signed in to change notification settings - Fork 478
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
Fleet UI: Clean up TabNav and TargetChipSelector components #26256
Draft
RachelElysia
wants to merge
5
commits into
main
Choose a base branch
from
26231-tabs-and-target-chips
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
934d91b
Fleet UI: Clean up TabNav and TargetChipSelector components
RachelElysia 695aed5
Add unit tests for both components
RachelElysia 99d8a76
Remove active state and change keyboard state
RachelElysia 0d0b8bf
Add TabText component
RachelElysia 00ed59f
Add count to TabText
RachelElysia 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
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
110 changes: 110 additions & 0 deletions
110
frontend/components/LiveQuery/TargetChipSelector/TargetChipSelector.stories.tsx
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,110 @@ | ||
import React from "react"; | ||
import { Meta, StoryObj } from "@storybook/react"; | ||
import { ISelectLabel, ISelectTeam } from "interfaces/target"; | ||
import TargetChipSelector from "./TargetChipSelector"; // Adjust the path if necessary | ||
|
||
const meta: Meta<typeof TargetChipSelector> = { | ||
component: TargetChipSelector, | ||
title: "Components/TargetChipSelector", | ||
argTypes: { | ||
entity: { | ||
description: "The label or team entity to display.", | ||
control: { type: "object" }, | ||
}, | ||
isSelected: { | ||
description: | ||
"Whether the chip is currently selected, updated by parent onClick handler.", | ||
control: { type: "boolean" }, | ||
}, | ||
onClick: { | ||
description: "The handler to call when the chip is clicked.", | ||
action: "clicked", // Use Storybook's action to track clicks | ||
}, | ||
}, | ||
parameters: { | ||
backgrounds: { | ||
default: "light", | ||
values: [ | ||
{ name: "light", value: "#ffffff" }, | ||
{ name: "dark", value: "#333333" }, | ||
], | ||
}, | ||
}, | ||
}; | ||
|
||
export default meta; | ||
|
||
type Story = StoryObj<typeof TargetChipSelector>; | ||
|
||
// Example data for labels and teams | ||
const mockLabel: ISelectLabel = { | ||
id: 1, | ||
name: "Example Label", | ||
label_type: "regular", | ||
description: "A test label", | ||
}; | ||
|
||
const mockTeam: ISelectTeam = { | ||
id: 2, | ||
name: "Example Team", | ||
description: "A test team", | ||
}; | ||
|
||
export const LabelExample: Story = { | ||
args: { | ||
entity: mockLabel, | ||
isSelected: false, | ||
onClick: (value) => (event) => { | ||
event.preventDefault(); | ||
console.log("Clicked label:", value); | ||
}, | ||
}, | ||
render: (args) => ( | ||
<TargetChipSelector | ||
entity={args.entity} | ||
isSelected={args.isSelected} | ||
onClick={args.onClick} | ||
/> | ||
), | ||
}; | ||
|
||
export const TeamExample: Story = { | ||
args: { | ||
entity: mockTeam, | ||
isSelected: true, | ||
onClick: (value) => (event) => { | ||
event.preventDefault(); | ||
console.log("Clicked team:", value); | ||
}, | ||
}, | ||
render: (args) => ( | ||
<TargetChipSelector | ||
entity={args.entity} | ||
isSelected={args.isSelected} | ||
onClick={args.onClick} | ||
/> | ||
), | ||
}; | ||
|
||
export const BuiltInLabelExample: Story = { | ||
args: { | ||
entity: { | ||
id: 3, | ||
name: "MS Windows", | ||
label_type: "builtin", | ||
description: "Microsoft Windows hosts", | ||
}, | ||
isSelected: false, | ||
onClick: (value) => (event) => { | ||
event.preventDefault(); | ||
console.log("Clicked label:", value); | ||
}, | ||
}, | ||
render: (args) => ( | ||
<TargetChipSelector | ||
entity={args.entity} | ||
isSelected={args.isSelected} | ||
onClick={args.onClick} | ||
/> | ||
), | ||
}; |
102 changes: 102 additions & 0 deletions
102
frontend/components/LiveQuery/TargetChipSelector/TargetChipSelector.tests.tsx
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,102 @@ | ||
import React from "react"; | ||
import { render, screen, fireEvent } from "@testing-library/react"; | ||
|
||
import { ISelectLabel, ISelectTeam } from "interfaces/target"; | ||
import TargetChipSelector from "./TargetChipSelector"; | ||
|
||
describe("TargetChipSelector", () => { | ||
const mockOnClick = jest.fn(); | ||
|
||
const mockLabel: ISelectLabel = { | ||
id: 1, | ||
name: "Example Label", | ||
label_type: "regular", | ||
description: "A test label", | ||
}; | ||
|
||
const mockTeam: ISelectTeam = { | ||
id: 2, | ||
name: "Example Team", | ||
description: "A test team", | ||
}; | ||
|
||
it("renders the correct display text for a label", () => { | ||
render( | ||
<TargetChipSelector | ||
entity={mockLabel} | ||
isSelected={false} | ||
onClick={mockOnClick} | ||
/> | ||
); | ||
|
||
expect(screen.getByText("Example Label")).toBeInTheDocument(); | ||
}); | ||
|
||
it("renders the correct display text for a team", () => { | ||
render( | ||
<TargetChipSelector | ||
entity={mockTeam} | ||
isSelected={false} | ||
onClick={mockOnClick} | ||
/> | ||
); | ||
|
||
expect(screen.getByText("Example Team")).toBeInTheDocument(); | ||
}); | ||
|
||
it("renders the correct icon when selected", () => { | ||
render( | ||
<TargetChipSelector entity={mockLabel} isSelected onClick={mockOnClick} /> | ||
); | ||
|
||
expect(screen.getByLabelText("check")).toBeInTheDocument(); | ||
}); | ||
|
||
it("renders the correct icon when not selected", () => { | ||
render( | ||
<TargetChipSelector | ||
entity={mockLabel} | ||
isSelected={false} | ||
onClick={mockOnClick} | ||
/> | ||
); | ||
|
||
expect(screen.getByLabelText("plus")).toBeInTheDocument(); | ||
}); | ||
|
||
it("calls the onClick handler with the correct entity when clicked", () => { | ||
render( | ||
<TargetChipSelector | ||
entity={mockLabel} | ||
isSelected={false} | ||
onClick={(value) => (event) => mockOnClick(value, event)} | ||
/> | ||
); | ||
|
||
fireEvent.click(screen.getByRole("button")); | ||
|
||
expect(mockOnClick).toHaveBeenCalledWith(mockLabel, expect.any(Object)); | ||
}); | ||
|
||
it("applies the correct data-selected attribute when selected", () => { | ||
render( | ||
<TargetChipSelector entity={mockLabel} isSelected onClick={mockOnClick} /> | ||
); | ||
|
||
const button = screen.getByRole("button"); | ||
expect(button).toHaveAttribute("data-selected", "true"); | ||
}); | ||
|
||
it("applies the correct data-selected attribute when not selected", () => { | ||
render( | ||
<TargetChipSelector | ||
entity={mockLabel} | ||
isSelected={false} | ||
onClick={mockOnClick} | ||
/> | ||
); | ||
|
||
const button = screen.getByRole("button"); | ||
expect(button).toHaveAttribute("data-selected", "false"); | ||
}); | ||
}); |
55 changes: 55 additions & 0 deletions
55
frontend/components/LiveQuery/TargetChipSelector/TargetChipSelector.tsx
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,55 @@ | ||
import React from "react"; | ||
import { | ||
ISelectLabel, | ||
ISelectTeam, | ||
ISelectTargetsEntity, | ||
} from "interfaces/target"; | ||
import Icon from "components/Icon"; | ||
import { | ||
PlatformLabelNameFromAPI, | ||
LABEL_DISPLAY_MAP, | ||
} from "utilities/constants"; | ||
|
||
interface ITargetChipSelectorProps { | ||
entity: ISelectLabel | ISelectTeam; | ||
isSelected: boolean; | ||
onClick: ( | ||
value: ISelectLabel | ISelectTeam | ||
) => React.MouseEventHandler<HTMLButtonElement>; | ||
} | ||
|
||
const isBuiltInLabel = ( | ||
entity: ISelectTargetsEntity | ||
): entity is ISelectLabel & { label_type: "builtin" } => { | ||
return "label_type" in entity && entity.label_type === "builtin"; | ||
}; | ||
|
||
const TargetChipSelector = ({ | ||
entity, | ||
isSelected, | ||
onClick, | ||
}: ITargetChipSelectorProps): JSX.Element => { | ||
const displayText = (): string => { | ||
if (isBuiltInLabel(entity)) { | ||
const labelName = entity.name as PlatformLabelNameFromAPI; | ||
if (labelName in LABEL_DISPLAY_MAP) { | ||
return LABEL_DISPLAY_MAP[labelName] || labelName; | ||
} | ||
} | ||
|
||
return entity.name || "Missing display name"; | ||
}; | ||
|
||
return ( | ||
<button | ||
className="target-chip-selector" | ||
data-selected={isSelected} | ||
onClick={(e) => onClick(entity)(e)} | ||
> | ||
<Icon name={isSelected ? "check" : "plus"} /> | ||
<span className="selector-name">{displayText()}</span> | ||
</button> | ||
); | ||
}; | ||
|
||
export default TargetChipSelector; |
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
.target-pill-selector { | ||
.target-chip-selector { | ||
padding: $pad-small; | ||
background-color: $core-white; | ||
border: none; | ||
|
@@ -34,7 +34,7 @@ | |
} | ||
|
||
&:hover { | ||
box-shadow: inset 0 0 0 1px $core-vibrant-blue-over; | ||
background-color: $ui-vibrant-blue-10; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. changed hover state per figma design |
||
|
||
&:active { | ||
|
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 @@ | ||
export { default } from "./TargetChipSelector"; |
Oops, something went wrong.
Oops, something went wrong.
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.
Moved to separate directory/file to add unit tests, storybook, organize styling