Skip to content

Commit

Permalink
Merge pull request #35 from szhsin/feat/selectable
Browse files Browse the repository at this point in the history
feat: selectable
  • Loading branch information
szhsin authored Jun 22, 2024
2 parents 8798dcf + f94dd6c commit a6d7bd9
Show file tree
Hide file tree
Showing 23 changed files with 246 additions and 238 deletions.
86 changes: 40 additions & 46 deletions dist/cjs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
var react = require('react');

const useAutocomplete = ({
value = '',
onChange = () => {},
value,
onChange,
selectedItem,
onSelectedItemChange,
isItemDisabled = () => false,
feature: useFeature,
traversal: useTraversal,
Expand All @@ -14,13 +16,10 @@ const useAutocomplete = ({
const [tmpValue, setTmpValue] = react.useState();
const [open, setOpen] = react.useState(false);
const [focusItem, setFocusItem] = react.useState();
const [selectedItem, setSelectedItem] = react.useState();
const getItemValue = react.useCallback(item => item == null ? '' : _getItemValue ? _getItemValue(item) : item.toString(), [_getItemValue]);
const getItemValue = item => item == null ? '' : _getItemValue ? _getItemValue(item) : item.toString();
const state = {
focusItem,
setFocusItem,
selectedItem,
setSelectedItem,
open,
setOpen
};
Expand All @@ -30,7 +29,9 @@ const useAutocomplete = ({
getItemValue,
isItemDisabled,
value,
onChange,
onChange: newValue => value != newValue && (onChange == null ? void 0 : onChange(newValue)),
selectedItem,
onSelectedItemChange: newItem => newItem !== selectedItem && (onSelectedItemChange == null ? void 0 : onSelectedItemChange(newItem)),
inputRef,
...state
};
Expand Down Expand Up @@ -84,9 +85,10 @@ const scrollIntoView = element => element == null ? void 0 : element.scrollIntoV
});
const autocompleteLite = ({
rovingText,
constricted,
selectOnBlur = true,
deselectOnBlur = true
select,
selectOnBlur = rovingText,
deselectOnClear = true,
deselectOnChange = true
} = {}) => ({
getItemValue,
isItemDisabled,
Expand All @@ -96,29 +98,30 @@ const autocompleteLite = ({
tmpValue,
setTmpValue,
selectedItem,
setSelectedItem,
onSelectedItemChange,
focusItem,
setFocusItem,
open,
setOpen,
inputRef
}) => {
var _ref;
const mutable = useMutableState({});
const inputValue = tmpValue || value;
const updateValue = (newValue, moveCaretToEnd = true) => {
setTmpValue();
const inputValue = (_ref = tmpValue || value) != null ? _ref : getItemValue(selectedItem);
const updateValue = newValue => {
const endIndex = newValue.length;
moveCaretToEnd && inputRef.current.setSelectionRange(endIndex, endIndex);
if (value != newValue) onChange(newValue);
inputRef.current.setSelectionRange(endIndex, endIndex);
if (!select) onChange(newValue);
};
const updateItem = item => item !== selectedItem && setSelectedItem(item);
const updateAll = item => {
updateItem(item);
onSelectedItemChange(item);
updateValue(getItemValue(item));
};
const closeList = () => {
setOpen(false);
setFocusItem();
setTmpValue();
if (select) onChange();
};
return {
clearable: !!inputValue,
Expand All @@ -130,9 +133,11 @@ const autocompleteLite = ({
onClick: () => {
var _inputRef$current;
(_inputRef$current = inputRef.current) == null || _inputRef$current.focus();
updateValue('');
setFocusItem();
setOpen(true);
onChange('');
setTmpValue();
setFocusItem();
if (deselectOnClear) onSelectedItemChange();
}
}),
getListProps: () => ({
Expand All @@ -155,9 +160,12 @@ const autocompleteLite = ({
ref: inputRef,
value: inputValue,
onChange: e => {
setFocusItem();
setOpen(true);
updateValue(e.target.value, false);
setFocusItem();
setTmpValue();
const newValue = e.target.value;
onChange(newValue);
if (!select && deselectOnChange || deselectOnClear && !newValue) onSelectedItemChange();
},
onBlur: ({
target
Expand All @@ -170,12 +178,7 @@ const autocompleteLite = ({
if (!open) return;
if (selectOnBlur && focusItem) {
updateAll(focusItem);
} else if (constricted) {
if (value || !deselectOnBlur) updateAll(selectedItem);else updateItem();
} else if (getItemValue(selectedItem) != value) {
updateItem();
}
setTmpValue();
closeList();
},
onKeyDown: e => {
Expand All @@ -197,15 +200,7 @@ const autocompleteLite = ({
}
break;
case 'Escape':
if (open) {
if (constricted) {
updateAll(selectedItem);
} else if (!value || getItemValue(selectedItem) != value) {
updateItem();
updateValue(value);
}
closeList();
}
if (open) closeList();
break;
}
},
Expand Down Expand Up @@ -280,23 +275,22 @@ const dropdownToggle = () => ({
open,
setOpen,
focusItem,
onChange
value,
tmpValue
}) => {
const mutable = useMutableState({});
const toggleRef = react.useRef(null);
const inputValue = tmpValue || value || '';
react.useEffect(() => {
var _inputRef$current;
if (open) (_inputRef$current = inputRef.current) == null || _inputRef$current.focus();
}, [open, inputRef]);
const openList = () => {
onChange('');
setOpen(true);
};
const focusToggle = () => setTimeout(() => {
var _toggleRef$current;
return (_toggleRef$current = toggleRef.current) == null ? void 0 : _toggleRef$current.focus();
}, 0);
return {
clearable: !!inputValue,
getToggleProps: () => ({
ref: toggleRef,
onMouseDown: () => {
Expand All @@ -306,7 +300,7 @@ const dropdownToggle = () => ({
if (mutable.a) {
mutable.a = 0;
} else {
openList();
setOpen(true);
}
},
onKeyDown: e => {
Expand All @@ -315,11 +309,12 @@ const dropdownToggle = () => ({
} = e;
if (key === 'ArrowDown') {
e.preventDefault();
openList();
setOpen(true);
}
}
}),
getInputProps: () => ({
value: inputValue,
onKeyDown: e => {
const {
key
Expand All @@ -336,9 +331,8 @@ const dropdownToggle = () => ({

const dropdown = props => mergeFeatures(autocompleteLite({
...props,
constricted: true,
selectOnBlur: false,
deselectOnBlur: false
select: true,
deselectOnClear: false
}), dropdownToggle());

const inline = ({
Expand Down
52 changes: 23 additions & 29 deletions dist/esm/features/atom/autocompleteLite.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ const scrollIntoView = element => element == null ? void 0 : element.scrollIntoV
});
const autocompleteLite = ({
rovingText,
constricted,
selectOnBlur = true,
deselectOnBlur = true
select,
selectOnBlur = rovingText,
deselectOnClear = true,
deselectOnChange = true
} = {}) => ({
getItemValue,
isItemDisabled,
Expand All @@ -17,29 +18,30 @@ const autocompleteLite = ({
tmpValue,
setTmpValue,
selectedItem,
setSelectedItem,
onSelectedItemChange,
focusItem,
setFocusItem,
open,
setOpen,
inputRef
}) => {
var _ref;
const mutable = useMutableState({});
const inputValue = tmpValue || value;
const updateValue = (newValue, moveCaretToEnd = true) => {
setTmpValue();
const inputValue = (_ref = tmpValue || value) != null ? _ref : getItemValue(selectedItem);
const updateValue = newValue => {
const endIndex = newValue.length;
moveCaretToEnd && inputRef.current.setSelectionRange(endIndex, endIndex);
if (value != newValue) onChange(newValue);
inputRef.current.setSelectionRange(endIndex, endIndex);
if (!select) onChange(newValue);
};
const updateItem = item => item !== selectedItem && setSelectedItem(item);
const updateAll = item => {
updateItem(item);
onSelectedItemChange(item);
updateValue(getItemValue(item));
};
const closeList = () => {
setOpen(false);
setFocusItem();
setTmpValue();
if (select) onChange();
};
return {
clearable: !!inputValue,
Expand All @@ -51,9 +53,11 @@ const autocompleteLite = ({
onClick: () => {
var _inputRef$current;
(_inputRef$current = inputRef.current) == null || _inputRef$current.focus();
updateValue('');
setFocusItem();
setOpen(true);
onChange('');
setTmpValue();
setFocusItem();
if (deselectOnClear) onSelectedItemChange();
}
}),
getListProps: () => ({
Expand All @@ -76,9 +80,12 @@ const autocompleteLite = ({
ref: inputRef,
value: inputValue,
onChange: e => {
setFocusItem();
setOpen(true);
updateValue(e.target.value, false);
setFocusItem();
setTmpValue();
const newValue = e.target.value;
onChange(newValue);
if (!select && deselectOnChange || deselectOnClear && !newValue) onSelectedItemChange();
},
onBlur: ({
target
Expand All @@ -91,12 +98,7 @@ const autocompleteLite = ({
if (!open) return;
if (selectOnBlur && focusItem) {
updateAll(focusItem);
} else if (constricted) {
if (value || !deselectOnBlur) updateAll(selectedItem);else updateItem();
} else if (getItemValue(selectedItem) != value) {
updateItem();
}
setTmpValue();
closeList();
},
onKeyDown: e => {
Expand All @@ -118,15 +120,7 @@ const autocompleteLite = ({
}
break;
case 'Escape':
if (open) {
if (constricted) {
updateAll(selectedItem);
} else if (!value || getItemValue(selectedItem) != value) {
updateItem();
updateValue(value);
}
closeList();
}
if (open) closeList();
break;
}
},
Expand Down
14 changes: 7 additions & 7 deletions dist/esm/features/atom/dropdownToggle.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,22 @@ const dropdownToggle = () => ({
open,
setOpen,
focusItem,
onChange
value,
tmpValue
}) => {
const mutable = useMutableState({});
const toggleRef = useRef(null);
const inputValue = tmpValue || value || '';
useEffect(() => {
var _inputRef$current;
if (open) (_inputRef$current = inputRef.current) == null || _inputRef$current.focus();
}, [open, inputRef]);
const openList = () => {
onChange('');
setOpen(true);
};
const focusToggle = () => setTimeout(() => {
var _toggleRef$current;
return (_toggleRef$current = toggleRef.current) == null ? void 0 : _toggleRef$current.focus();
}, 0);
return {
clearable: !!inputValue,
getToggleProps: () => ({
ref: toggleRef,
onMouseDown: () => {
Expand All @@ -32,7 +31,7 @@ const dropdownToggle = () => ({
if (mutable.a) {
mutable.a = 0;
} else {
openList();
setOpen(true);
}
},
onKeyDown: e => {
Expand All @@ -41,11 +40,12 @@ const dropdownToggle = () => ({
} = e;
if (key === 'ArrowDown') {
e.preventDefault();
openList();
setOpen(true);
}
}
}),
getInputProps: () => ({
value: inputValue,
onKeyDown: e => {
const {
key
Expand Down
5 changes: 2 additions & 3 deletions dist/esm/features/molecule/dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ import { dropdownToggle } from '../atom/dropdownToggle.js';

const dropdown = props => mergeFeatures(autocompleteLite({
...props,
constricted: true,
selectOnBlur: false,
deselectOnBlur: false
select: true,
deselectOnClear: false
}), dropdownToggle());

export { dropdown };
Loading

0 comments on commit a6d7bd9

Please sign in to comment.