|
1 | 1 | // LICENSE : MIT |
2 | 2 | "use strict"; |
3 | 3 | import React, { useRef, useEffect, useImperativeHandle } from "react"; |
4 | | -import Select from "react-select"; |
| 4 | +import CreatableSelect from "react-select/creatable"; |
5 | 5 |
|
6 | 6 | function TagSelect({ tags, selectedTags, selectTags, ref }) { |
7 | 7 | const selectRef = useRef(null); |
@@ -35,6 +35,14 @@ function TagSelect({ tags, selectedTags, selectTags, ref }) { |
35 | 35 | selectTags(tags); |
36 | 36 | }; |
37 | 37 |
|
| 38 | + const handleCreate = (inputValue) => { |
| 39 | + const newTag = inputValue.trim(); |
| 40 | + if (newTag) { |
| 41 | + const updatedTags = [...selectedTags, newTag]; |
| 42 | + selectTags(updatedTags); |
| 43 | + } |
| 44 | + }; |
| 45 | + |
38 | 46 | // react-selectのスタイルをTitleInputやURLInputと統一 |
39 | 47 | const customStyles = { |
40 | 48 | control: (provided) => ({ |
@@ -95,19 +103,21 @@ function TagSelect({ tags, selectedTags, selectTags, ref }) { |
95 | 103 | return ( |
96 | 104 | <div className="EditorToolbar"> |
97 | 105 | <h2 className="l-header">Tags</h2> |
98 | | - <Select |
| 106 | + <CreatableSelect |
99 | 107 | ref={selectRef} |
100 | 108 | name="form-field-name" |
101 | 109 | value={selectedOptions} |
102 | 110 | options={options} |
103 | 111 | isMulti |
104 | 112 | isClearable |
105 | | - placeholder="Select Tag(s)" |
| 113 | + placeholder="Select or create tag(s)" |
106 | 114 | onChange={handleChange} |
| 115 | + onCreateOption={handleCreate} |
107 | 116 | styles={customStyles} |
108 | 117 | menuPosition="fixed" |
109 | 118 | menuPortalTarget={document.body} |
110 | 119 | menuShouldBlockScroll={false} |
| 120 | + formatCreateLabel={(inputValue) => `Create "${inputValue}"`} |
111 | 121 | /> |
112 | 122 | </div> |
113 | 123 | ); |
|
0 commit comments