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

fix(select): Create a generic Id type for Option interface #214

Closed
wants to merge 9 commits into from
3 changes: 2 additions & 1 deletion src/select/typeahead/util/typeaheadSelectUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ function filterOptionsByKeyword<T extends TypeaheadSelectOption = TypeaheadSelec
if (keyword) {
filteredOptions = options.filter(
(option) =>
!option.title || option.title.toLowerCase().includes(keyword.toLowerCase())
typeof option.title === "string" &&
option.title.toLowerCase().includes(keyword.toLowerCase())
);
}

Expand Down
16 changes: 11 additions & 5 deletions src/select/util/selectTypes.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import React from "react";

interface Option {
id: string;
interface Option<Id = string> {
id: Id;
title: React.ReactNode;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did we add title property here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it can be useful for some cases, but I can set it as optional or remove it completely.

isDisabled?: boolean;
}

interface TypeaheadSelectOption extends Option {
title: string;
}
// TypeaheadSelectOption is intentionally empty. It happens not
// to have more properties than Option, but this may
// change in the future, and it helps to have a TypeaheadSelectOption
// interface that people can use. Therefore the no-empty-interface is disabled
// rule for this declaration:

// eslint-disable-next-line @typescript-eslint/no-empty-interface
interface TypeaheadSelectOption extends Option {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can use type instead of an interface to avoid disabling this rule.

Suggested change
// eslint-disable-next-line @typescript-eslint/no-empty-interface
interface TypeaheadSelectOption extends Option {}
type TypeaheadSelectOption = Option;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried the way you said, but it didn't let me set a generic type to TypeaheadSelectOption 🤔


type SelectItemElement = HTMLLIElement | HTMLDivElement;

Expand Down
9 changes: 5 additions & 4 deletions stories/11-Typeahead.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import StoryFragment from "./utils/StoryFragment";
import FormField from "../src/form/field/FormField";
import TypeaheadSelect from "../src/select/typeahead/TypeaheadSelect";
import {TypeaheadSelectOption} from "../src/select/util/selectTypes";
import {Language} from "./utils/constants/select/selectStoryConstants";

const simulateAPICall = (timeout = 1000) =>
new Promise((resolve) => setTimeout(resolve, timeout));
Expand All @@ -26,11 +27,11 @@ storiesOf("Typeahead", module).add("Typeahead", () => {
id: "spanish",
title: "Spanish"
}
],
] as TypeaheadSelectOption<Language>[],
thirdOptions: [],
selectedOptions: [] as TypeaheadSelectOption[],
secondSelectedOptions: [] as TypeaheadSelectOption[],
thirdSelectedOptions: [] as TypeaheadSelectOption[],
selectedOptions: [] as TypeaheadSelectOption<Language>[],
secondSelectedOptions: [] as TypeaheadSelectOption<Language>[],
thirdSelectedOptions: [] as TypeaheadSelectOption<Language>[],
areOptionsFetching: false,
keyword: ""
};
Expand Down
22 changes: 13 additions & 9 deletions stories/utils/constants/select/selectStoryConstants.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React from "react";
import {Option} from "../../../../src/select/util/selectTypes";

function CoinDropdownOptionCustomContent({
id,
Expand All @@ -21,6 +22,13 @@ function CoinDropdownOptionCustomContent({
);
}

export enum Language {
TURKISH = "turkish",
ENGLISH = "english",
SPANISH = "spanish",
FRENCH = "french"
}

const initialState = {
basic: {
options: [
Expand All @@ -42,7 +50,7 @@ const initialState = {
isDisabled: true
}
],
selectedOption: null as {id: string; isDisabled?: boolean; title: string} | null
selectedOption: null as Option<Language> | null
},
multiSelect: {
options: [
Expand All @@ -63,8 +71,8 @@ const initialState = {
title: "French - Disabled",
isDisabled: true
}
] as {id: string; isDisabled?: boolean; title: string}[],
value: [] as {id: string; isDisabled?: boolean; title: string}[]
] as Option<Language>[],
value: [] as Option<Language>[]
},
withSubtitle: {
options: [
Expand All @@ -84,7 +92,7 @@ const initialState = {
subtitle: "JavaScript"
}
],
selectedOption: null as {id: string; isDisabled?: boolean; subtitle: string} | null
selectedOption: null as Option | null
},
withCustomContent: {
options: [
Expand Down Expand Up @@ -127,11 +135,7 @@ const initialState = {
)
}
],
selectedOption: null as {
id: string;
title?: string;
CustomContent: JSX.Element;
} | null
selectedOption: null as Option | null
}
};

Expand Down
5 changes: 3 additions & 2 deletions stories/utils/selectStoryUtils.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import {initialState} from "./constants/select/selectStoryConstants";
import {Option} from "../../src/select/util/selectTypes";
import {initialState, Language} from "./constants/select/selectStoryConstants";

function handleMultiSelect(
state: typeof initialState.multiSelect,
setState: React.Dispatch<React.SetStateAction<typeof initialState.multiSelect>>,
option: {id: string; isDisabled?: boolean; title: string}
option: Option<Language>
) {
const isSelected = state.value.findIndex((opt) => opt.id === option.id) > -1;

Expand Down