Skip to content

Commit

Permalink
feat: Allow creating options on dropdown (#188)
Browse files Browse the repository at this point in the history
  • Loading branch information
d-beezee authored Apr 26, 2024
1 parent 3f2bd77 commit 5396f5d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
25 changes: 25 additions & 0 deletions src/stories/Dropdown/Dropdown.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,28 @@ MultiDropdown.args = {
name: "colors",
options: colourOptions,
};

export const CreatableDropdown = Template.bind({});
CreatableDropdown.args = {
className: "creatable-single",
defaultValue: colourOptions[0],
name: "color",
onCreateOption: async (inputValue: string) => {
await new Promise((resolve) => setTimeout(resolve, 1000));
alert(`Creating ${inputValue}`);
},
options: colourOptions,
};

export const CreatableMultiDropdown = Template.bind({});
CreatableMultiDropdown.args = {
isMulti: true,
className: "creatable-multi",
defaultValue: colourOptions[0],
name: "color",
onCreateOption: async (inputValue: string) => {
await new Promise((resolve) => setTimeout(resolve, 1000));
alert(`Creating ${inputValue}`);
},
options: colourOptions,
};
14 changes: 10 additions & 4 deletions src/stories/Dropdown/index.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import ReactSelect, { Props, GroupBase, StylesConfig } from "react-select5";
import { aqTheme, customComponents } from "./styles";
import ReactSelect, { GroupBase, Props, StylesConfig } from "react-select5";
import Creatable from "react-select5/creatable";
import { aqBootstrapTheme } from "../theme/defaultTheme";
import { aqTheme, customComponents } from "./styles";

export function Dropdown<
Option,
IsMulti extends boolean = false,
Group extends GroupBase<Option> = GroupBase<Option>,
>(props: Props<Option, IsMulti, Group>) {
>(
props: Props<Option, IsMulti, Group> & {
onCreateOption?: (inputValue: string) => Promise<void>;
},
) {
const customStyle: StylesConfig<Option, IsMulti, Group> = {
control: (provided, state) => {
let borderColor = aqBootstrapTheme.colors.elementGeneric;
Expand Down Expand Up @@ -201,8 +206,9 @@ export function Dropdown<
},
};

const Component = props.onCreateOption ? Creatable : ReactSelect;
return (
<ReactSelect
<Component
theme={(theme) => ({
...theme,
...aqTheme,
Expand Down

0 comments on commit 5396f5d

Please sign in to comment.