Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions src/hooks/use-multi-select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const defaultStrings = {
search: "Search",
selectAll: "Select All",
selectSomeItems: "Select...",
create: "Create",
};

const defaultProps: Partial<ISelectProps> = {
Expand Down
2 changes: 2 additions & 0 deletions src/lib/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,6 @@ export interface ISelectProps {
ClearSelectedIcon?: ReactNode;
defaultIsOpen?: boolean;
isOpen?: boolean;
isCreatable?: boolean;
onCreate?: Function;
}
13 changes: 12 additions & 1 deletion src/select-panel/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ const SelectPanel = () => {
hasSelectAll,
ClearIcon,
debounceDuration,
isCreatable,
onCreate=()=>{}
} = useMultiSelect();

const listRef = useRef<any>();
Expand Down Expand Up @@ -126,6 +128,11 @@ const SelectPanel = () => {
setFocusIndex(FocusType.SEARCH);
};

const handleNewField = () => {
onCreate(searchText);
onChange([...value, {label: searchText, value: searchText.toLowerCase()}]);
};

const getFilteredOptions = async () =>
customFilterOptions
? await customFilterOptions(options, searchTextForFilter)
Expand Down Expand Up @@ -203,7 +210,11 @@ const SelectPanel = () => {
onClick={(_e, index) => handleItemClicked(index)}
/>
) : (
<li className="no-options">{t("noOptions")}</li>
isCreatable ? (
<li onClick={handleNewField} className="add-option">{t("create")} "{searchText}"</li>
) : (
<li className="no-options">{t("noOptions")}</li>
)
)}
</ul>
</div>
Expand Down
9 changes: 9 additions & 0 deletions src/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,15 @@
display: none;
}

.rmsc .add-option {
padding: var(--rmsc-p);
text-align: left;
}

.rmsc .add-option:hover {
background: var(--rmsc-hover);
}

.rmsc .item-renderer {
display: flex;
align-items: baseline;
Expand Down
43 changes: 43 additions & 0 deletions stories/enabled-new-field.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { text, withKnobs } from "@storybook/addon-knobs";
import React, { useState } from "react";

import MultiSelect from "../src/multi-select";

export default {
title: "Multiselect",
decorators: [withKnobs],
};

const INITIAL_OPTIONS = [
{ label: "Grapes 🍇", value: "grapes" },
{ label: "Mango 🥭", value: "mango", disabled: true },
{ label: "Strawberry 🍓", value: "strawberry" },
];

export const ExampleNewField = () => {

function handleNewField(newVal) {
setOptions([...options, {label: newVal, value: newVal.toLowerCase()}]);
}

const [options, setOptions] = useState(INITIAL_OPTIONS);
const [selected, setSelected] = useState([]);

return (
<div>
<pre>{JSON.stringify(selected)}</pre>
<MultiSelect
options={options}
value={selected}
onChange={setSelected}
labelledBy={text("labelledBy", "Select Fruits")}
isCreatable
onCreate={handleNewField}
/>
</div>
);
};

ExampleNewField.story = {
name: "New field",
};