ui: autocomplete: fix value is not a function

This commit is contained in:
无限UCW committed 2021-08-25 12:55:32 +08:00
1 parent fe36a3f17e
commit c78fc82d5b
2 files changed
+15 -2

No files matched your search

@@ -28,8 +28,8 @@ const AutoComplete = forwardRef(function AutoComplete(props, ref) {
const freeSoloConverter = freeSolo ? props.freeSoloConverter ?? ((i) => i) : ((i) => i); const freeSoloConverter = freeSolo ? props.freeSoloConverter ?? ((i) => i) : ((i) => i);
const [focused, setFocused] = useState(false); // is focused const [focused, setFocused] = useState(false); // is focused
const [selected, setSelected] = useState(defaultItems); // selected items const [selected, setSelected] = useState(multi ? defaultItems : []); // selected items
const [selectedKeys, setSelectedKeys] = useState(defaultItems.map((i) => itemKey(i))); // keys of selected items const [selectedKeys, setSelectedKeys] = useState(multi ? defaultItems.map((i) => itemKey(i)) : []); // keys of selected items
const [itemList, setItemList] = useState([]); // items list const [itemList, setItemList] = useState([]); // items list
const [currentItem, setCurrentItem] = useState(null); // index of current item (in item list) const [currentItem, setCurrentItem] = useState(null); // index of current item (in item list)
@@ -99,6 +99,7 @@ const AutoComplete = forwardRef(function AutoComplete(props, ref) {
inputRef.current.focus(); inputRef.current.focus();
} else { } else {
inputRef.current.value = itemKey(item); inputRef.current.value = itemKey(item);
dispatchChange();
} }
setItemList([]); setItemList([]);
setCurrentItem(null); setCurrentItem(null);
@@ -214,6 +215,7 @@ const AutoComplete = forwardRef(function AutoComplete(props, ref) {
}} }}
onBlur={() => setFocused(false)} onBlur={() => setFocused(false)}
onKeyDown={handleInputKeyDown} onKeyDown={handleInputKeyDown}
defaultValue={multi ? '' : defaultItems.join(',')}
/> />
</div> </div>
{focused && itemList.length > 0 && ( {focused && itemList.length > 0 && (
@@ -17,7 +17,14 @@ export default class AutoComplete extends DOMAttachedObject {
...options, ...options,
}; };
this.ref = null; this.ref = null;
this.clear = this.clear.bind(this);
this.onChange = this.onChange.bind(this); this.onChange = this.onChange.bind(this);
this.attach = this.attach.bind(this);
this.open = this.open.bind(this);
this.close = this.close.bind(this);
this.value = this.value.bind(this);
this.detach = this.detach.bind(this);
this.focus = this.focus.bind(this);
this.container = document.createElement('div'); this.container = document.createElement('div');
this.$dom.addClass('autocomplete-dummy').after(this.container); this.$dom.addClass('autocomplete-dummy').after(this.container);
// Note: use `setTimeout(fn, 0)` to ensure that code is executed after browser autofill // Note: use `setTimeout(fn, 0)` to ensure that code is executed after browser autofill
@@ -63,6 +70,10 @@ export default class AutoComplete extends DOMAttachedObject {
this.ref.closeList(); this.ref.closeList();
} }
value() {
return this.$dom.val();
}
detach() { detach() {
if (this.detached) return; if (this.detached) return;
super.detach(); super.detach();