diff --git a/src/hooks/use-multi-select.tsx b/src/hooks/use-multi-select.tsx index de359ee..d1dceaf 100644 --- a/src/hooks/use-multi-select.tsx +++ b/src/hooks/use-multi-select.tsx @@ -10,6 +10,7 @@ const defaultStrings = { search: "Search", selectAll: "Select All", selectSomeItems: "Select...", + create: "Create", }; const defaultProps: Partial = { diff --git a/src/lib/interfaces.ts b/src/lib/interfaces.ts index ea68ace..df3e910 100644 --- a/src/lib/interfaces.ts +++ b/src/lib/interfaces.ts @@ -33,4 +33,6 @@ export interface ISelectProps { ClearSelectedIcon?: ReactNode; defaultIsOpen?: boolean; isOpen?: boolean; + isCreatable?: boolean; + onCreate?: Function; } diff --git a/src/select-panel/index.tsx b/src/select-panel/index.tsx index cf05056..c5a2738 100644 --- a/src/select-panel/index.tsx +++ b/src/select-panel/index.tsx @@ -39,6 +39,8 @@ const SelectPanel = () => { hasSelectAll, ClearIcon, debounceDuration, + isCreatable, + onCreate=()=>{} } = useMultiSelect(); const listRef = useRef(); @@ -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) @@ -203,7 +210,11 @@ const SelectPanel = () => { onClick={(_e, index) => handleItemClicked(index)} /> ) : ( -
  • {t("noOptions")}
  • + isCreatable ? ( +
  • {t("create")} "{searchText}"
  • + ) : ( +
  • {t("noOptions")}
  • + ) )} diff --git a/src/style.css b/src/style.css index eccf4d3..7ff152c 100644 --- a/src/style.css +++ b/src/style.css @@ -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; diff --git a/stories/enabled-new-field.stories.tsx b/stories/enabled-new-field.stories.tsx new file mode 100644 index 0000000..ba0c28f --- /dev/null +++ b/stories/enabled-new-field.stories.tsx @@ -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 ( +
    +
    {JSON.stringify(selected)}
    + +
    + ); +}; + +ExampleNewField.story = { + name: "New field", +};