-
Notifications
You must be signed in to change notification settings - Fork 451
feat: add search to dropdown and multi-select fields #753
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
Open
gosiexon-zen
wants to merge
1
commit into
master
Choose a base branch
from
mbien/add-search-to-dropdowns
base: master
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.
+1,163
−57
Open
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 hidden or 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 hidden or 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,37 @@ | ||
| interface HighlightMatchProps { | ||
| text: string; | ||
| searchValue: string; | ||
| } | ||
|
|
||
| /** | ||
| * Highlights the matching portion of text by making it bold. | ||
| * Case-insensitive matching. | ||
| */ | ||
| export function HighlightMatch({ | ||
| text, | ||
| searchValue, | ||
| }: HighlightMatchProps): JSX.Element { | ||
| if (!searchValue) { | ||
| return <span>{text}</span>; | ||
| } | ||
|
|
||
| const lowerText = text.toLowerCase(); | ||
| const lowerSearch = searchValue.toLowerCase(); | ||
| const index = lowerText.indexOf(lowerSearch); | ||
|
|
||
| if (index === -1) { | ||
| return <span>{text}</span>; | ||
| } | ||
|
|
||
| const before = text.slice(0, index); | ||
| const match = text.slice(index, index + searchValue.length); | ||
| const after = text.slice(index + searchValue.length); | ||
|
|
||
| return ( | ||
| <span> | ||
| {before} | ||
| <strong>{match}</strong> | ||
| {after} | ||
| </span> | ||
| ); | ||
| } |
This file contains hidden or 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,188 @@ | ||
| import { render } from "../../test/render"; | ||
| import { screen } from "@testing-library/react"; | ||
| import { MultiSelect } from "./MultiSelect"; | ||
| import type { TicketFieldObject } from "../data-types/TicketFieldObject"; | ||
|
|
||
| describe("MultiSelect", () => { | ||
| const mockOnChange = jest.fn(); | ||
|
|
||
| const baseField: TicketFieldObject = { | ||
| id: 1, | ||
| name: "test_multiselect", | ||
| label: "Test MultiSelect", | ||
| type: "multiselect", | ||
| value: [], | ||
| error: null, | ||
| required: false, | ||
| description: "", | ||
| options: [ | ||
| { name: "Option 1", value: "option_1" }, | ||
| { name: "Option 2", value: "option_2" }, | ||
| { name: "Option 3", value: "option_3" }, | ||
| { name: "Option 4", value: "option_4" }, | ||
| ], | ||
| }; | ||
|
|
||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
| }); | ||
|
|
||
| it("renders field with label", () => { | ||
| render(<MultiSelect field={baseField} onChange={mockOnChange} />); | ||
|
|
||
| expect(screen.getByText("Test MultiSelect")).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it("shows required indicator when field is required", () => { | ||
| const requiredField = { ...baseField, required: true }; | ||
| render(<MultiSelect field={requiredField} onChange={mockOnChange} />); | ||
|
|
||
| expect(screen.getByText("*")).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it("displays description when provided", () => { | ||
| const fieldWithDescription = { | ||
| ...baseField, | ||
| description: "<p>Select multiple options</p>", | ||
| }; | ||
| render( | ||
| <MultiSelect field={fieldWithDescription} onChange={mockOnChange} /> | ||
| ); | ||
|
|
||
| expect(screen.getByText("Select multiple options")).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it("displays error message when error is present", () => { | ||
| const fieldWithError = { | ||
| ...baseField, | ||
| error: "At least one option is required", | ||
| }; | ||
| render(<MultiSelect field={fieldWithError} onChange={mockOnChange} />); | ||
|
|
||
| expect( | ||
| screen.getByText("At least one option is required") | ||
| ).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it("renders hidden inputs for each selected value for form submission", () => { | ||
| const fieldWithValues = { | ||
| ...baseField, | ||
| value: ["option_1", "option_2"], | ||
| }; | ||
| const { container } = render( | ||
| <MultiSelect field={fieldWithValues} onChange={mockOnChange} /> | ||
| ); | ||
|
|
||
| const hiddenInputs = container.querySelectorAll( | ||
| 'input[type="hidden"][name="test_multiselect[]"]' | ||
| ); | ||
| expect(hiddenInputs).toHaveLength(2); | ||
| expect(hiddenInputs[0]).toHaveValue("option_1"); | ||
| expect(hiddenInputs[1]).toHaveValue("option_2"); | ||
| }); | ||
|
|
||
| it("renders with no hidden inputs when no values selected", () => { | ||
| const { container } = render( | ||
| <MultiSelect field={baseField} onChange={mockOnChange} /> | ||
| ); | ||
|
|
||
| const hiddenInputs = container.querySelectorAll( | ||
| 'input[type="hidden"][name="test_multiselect[]"]' | ||
| ); | ||
| expect(hiddenInputs).toHaveLength(0); | ||
| }); | ||
|
|
||
| it("handles nested options correctly", () => { | ||
| const fieldWithNestedOptions: TicketFieldObject = { | ||
| ...baseField, | ||
| options: [ | ||
| { name: "Category::SubCategory::Item", value: "cat_subcat_item" }, | ||
| { name: "Another Option", value: "another" }, | ||
| ], | ||
| }; | ||
|
|
||
| const { container } = render( | ||
| <MultiSelect field={fieldWithNestedOptions} onChange={mockOnChange} /> | ||
| ); | ||
|
|
||
| // Component should render without errors | ||
| expect(container).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it("renders with deeply nested selected options", () => { | ||
| const fieldWithNestedOptions: TicketFieldObject = { | ||
| ...baseField, | ||
| value: ["cameras_dslr_consumer", "cameras_dslr_pro"], | ||
| options: [ | ||
| { | ||
| name: "Cameras::Digital SLR::Consumer", | ||
| value: "cameras_dslr_consumer", | ||
| }, | ||
| { | ||
| name: "Cameras::Digital SLR::Professional", | ||
| value: "cameras_dslr_pro", | ||
| }, | ||
| { name: "Lenses", value: "lenses" }, | ||
| ], | ||
| }; | ||
|
|
||
| const { container } = render( | ||
| <MultiSelect field={fieldWithNestedOptions} onChange={mockOnChange} /> | ||
| ); | ||
|
|
||
| const hiddenInputs = container.querySelectorAll( | ||
| 'input[type="hidden"][name="test_multiselect[]"]' | ||
| ); | ||
| expect(hiddenInputs).toHaveLength(2); | ||
| expect(hiddenInputs[0]).toHaveValue("cameras_dslr_consumer"); | ||
| expect(hiddenInputs[1]).toHaveValue("cameras_dslr_pro"); | ||
| }); | ||
|
|
||
| it("initializes with correct values from props", () => { | ||
| const fieldWithValues = { | ||
| ...baseField, | ||
| value: ["option_1", "option_4"], | ||
| }; | ||
| const { container } = render( | ||
| <MultiSelect field={fieldWithValues} onChange={mockOnChange} /> | ||
| ); | ||
|
|
||
| const hiddenInputs = container.querySelectorAll( | ||
| 'input[type="hidden"][name="test_multiselect[]"]' | ||
| ); | ||
| expect(hiddenInputs).toHaveLength(2); | ||
| expect(hiddenInputs[0]).toHaveValue("option_1"); | ||
| expect(hiddenInputs[1]).toHaveValue("option_4"); | ||
| }); | ||
|
|
||
| it("handles empty array value", () => { | ||
| const fieldWithEmptyArray = { | ||
| ...baseField, | ||
| value: [], | ||
| }; | ||
| const { container } = render( | ||
| <MultiSelect field={fieldWithEmptyArray} onChange={mockOnChange} /> | ||
| ); | ||
|
|
||
| const hiddenInputs = container.querySelectorAll( | ||
| 'input[type="hidden"][name="test_multiselect[]"]' | ||
| ); | ||
| expect(hiddenInputs).toHaveLength(0); | ||
| }); | ||
|
|
||
| it("renders with single selected value", () => { | ||
| const fieldWithSingleValue = { | ||
| ...baseField, | ||
| value: ["option_1"], | ||
| }; | ||
| const { container } = render( | ||
| <MultiSelect field={fieldWithSingleValue} onChange={mockOnChange} /> | ||
| ); | ||
|
|
||
| const hiddenInputs = container.querySelectorAll( | ||
| 'input[type="hidden"][name="test_multiselect[]"]' | ||
| ); | ||
| expect(hiddenInputs).toHaveLength(1); | ||
| expect(hiddenInputs[0]).toHaveValue("option_1"); | ||
| }); | ||
| }); |
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.
It was to fix a Rollup build error. The
@zendeskgarden/react-dropdowns.legacypackage imports KEY_CODES from@zendeskgarden/container-utilities, but that export only exists in v1.x. Our package.json has@zendeskgarden/container-utilitiesat ^2.0.2 in dependencies, and v2.x removed the KEY_CODES export — so the legacy dropdowns package couldn't resolve it and the build failed.