Skip to content

Commit

Permalink
Remove optional-chaining transform (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
szhsin authored Jul 20, 2024
1 parent 2d9b7c0 commit 41c2302
Show file tree
Hide file tree
Showing 15 changed files with 48 additions and 70 deletions.
6 changes: 1 addition & 5 deletions babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,7 @@ module.exports = {
'@babel/preset-env',
{
bugfixes: true,
include: [
'@babel/plugin-proposal-optional-chaining',
'@babel/plugin-proposal-nullish-coalescing-operator',
'@babel/plugin-proposal-optional-catch-binding'
],
include: ['@babel/plugin-transform-optional-catch-binding'],
exclude: ['@babel/plugin-transform-typeof-symbol']
}
],
Expand Down
47 changes: 19 additions & 28 deletions dist/cjs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const useAutocomplete = ({
id: useId(),
tmpValue,
setTmpValue,
onChange: newValue => passthrough.value != newValue && (onChange == null ? void 0 : onChange(newValue)),
onChange: newValue => passthrough.value != newValue && onChange?.(newValue),
...passthrough,
...state
});
Expand All @@ -68,9 +68,9 @@ const useCombobox = ({
getSelectedValue: () => getItemValue(selected),
onSelectChange: newItem => {
if (!isEqual(newItem, selected)) {
onSelectChange == null || onSelectChange(newItem);
onSelectChange?.(newItem);
} else if (flipOnSelect) {
onSelectChange == null || onSelectChange();
onSelectChange?.();
}
}
});
Expand All @@ -80,16 +80,16 @@ const useMultiSelect = ({
isEqual = defaultEqual,
getItemValue,
selected,
onSelectChange: _onSelectChange = () => {},
onSelectChange,
flipOnSelect,
...passthrough
}) => {
const removeItem = itemToRemove => _onSelectChange(selected.filter(item => !isEqual(itemToRemove, item)));
const removeItem = itemToRemove => onSelectChange?.(selected.filter(item => !isEqual(itemToRemove, item)));
const removeSelect = item => {
if (item) {
removeItem(item);
} else {
selected.length && _onSelectChange(selected.slice(0, selected.length - 1));
selected.length && onSelectChange?.(selected.slice(0, selected.length - 1));
}
};
return {
Expand All @@ -102,7 +102,7 @@ const useMultiSelect = ({
onSelectChange: newItem => {
if (!newItem) return;
if (selected.findIndex(item => isEqual(item, newItem)) < 0) {
_onSelectChange([...selected, newItem]);
onSelectChange?.([...selected, newItem]);
} else if (flipOnSelect) {
removeItem(newItem);
}
Expand Down Expand Up @@ -154,18 +154,14 @@ const useFocusCapture = focusRef => {
if (document.activeElement === focusRef.current) mutable.a = 1;
}, () => {
if (mutable.a) {
var _focusRef$current;
mutable.a = 0;
(_focusRef$current = focusRef.current) == null || _focusRef$current.focus();
focusRef.current?.focus();
return true;
}
}, () => {
var _focusRef$current2;
return (_focusRef$current2 = focusRef.current) == null ? void 0 : _focusRef$current2.focus();
}];
}, () => focusRef.current?.focus()];
};

const scrollIntoView = element => element == null ? void 0 : element.scrollIntoView({
const scrollIntoView = element => element?.scrollIntoView({
block: 'nearest'
});
const autocompleteLite = ({
Expand Down Expand Up @@ -196,18 +192,17 @@ const autocompleteLite = ({
items,
id
}) => {
var _ref;
const [startCapture, inCapture, stopCapture] = useFocusCapture(inputRef);
const inputValue = (_ref = tmpValue || value) != null ? _ref : getSelectedValue();
const inputValue = (tmpValue || value) ?? getSelectedValue();
const selectItemOrAction = (item, noAction) => {
if (isItemAction != null && isItemAction(item)) {
!noAction && (onAction == null ? void 0 : onAction(item));
if (isItemAction?.(item)) {
!noAction && onAction?.(item);
return true;
}
const itemValue = getItemValue(item);
if (!select) onChange(itemValue);
const endIndex = itemValue.length;
inputRef.current.setSelectionRange(endIndex, endIndex);
inputRef.current?.setSelectionRange(endIndex, endIndex);
onSelectChange(item);
};
const resetState = shouldClose => {
Expand All @@ -231,7 +226,7 @@ const autocompleteLite = ({
if (--nextIndex < baseIndex) nextIndex = itemLength - 1;
}
newItem = items[nextIndex];
if (!newItem || !(isItemDisabled != null && isItemDisabled(newItem))) break;
if (!newItem || !isItemDisabled?.(newItem)) break;
if (++itemCounter >= itemLength) return;
}
setFocusItem(newItem);
Expand Down Expand Up @@ -272,7 +267,7 @@ const autocompleteLite = ({
'aria-selected': select ? isItemSelected(item) : isEqual(item, focusItem),
ref: isEqual(item, focusItem) ? scrollIntoView : null,
onClick: () => {
if (!(isItemDisabled != null && isItemDisabled(item))) {
if (!isItemDisabled?.(item)) {
resetState(selectItemOrAction(item));
}
}
Expand Down Expand Up @@ -454,13 +449,9 @@ const dropdownToggle = ({
const toggleRef = React.useRef(null);
const inputValue = tmpValue || value || '';
React.useEffect(() => {
var _inputRef$current;
if (open) (_inputRef$current = inputRef.current) == null || _inputRef$current.focus();
if (open) inputRef.current?.focus();
}, [open, inputRef]);
const focusToggle = () => setTimeout(() => {
var _toggleRef$current;
return (_toggleRef$current = toggleRef.current) == null ? void 0 : _toggleRef$current.focus();
}, 0);
const focusToggle = () => setTimeout(() => toggleRef.current?.focus(), 0);
return {
isInputEmpty: !inputValue,
getToggleProps: () => ({
Expand Down Expand Up @@ -523,7 +514,7 @@ const multiInput = () => ({
}),
getInputProps: () => ({
onBlur: inCapture,
onKeyDown: e => !e.target.value && e.key === 'Backspace' && (removeSelect == null ? void 0 : removeSelect())
onKeyDown: e => !e.target.value && e.key === 'Backspace' && removeSelect?.()
})
};
};
Expand Down
15 changes: 7 additions & 8 deletions dist/esm/features/atom/autocompleteLite.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ButtonProps, getId } from '../../common.js';
import { useFocusCapture } from '../../hooks/useFocusCapture.js';

const scrollIntoView = element => element == null ? void 0 : element.scrollIntoView({
const scrollIntoView = element => element?.scrollIntoView({
block: 'nearest'
});
const autocompleteLite = ({
Expand Down Expand Up @@ -32,18 +32,17 @@ const autocompleteLite = ({
items,
id
}) => {
var _ref;
const [startCapture, inCapture, stopCapture] = useFocusCapture(inputRef);
const inputValue = (_ref = tmpValue || value) != null ? _ref : getSelectedValue();
const inputValue = (tmpValue || value) ?? getSelectedValue();
const selectItemOrAction = (item, noAction) => {
if (isItemAction != null && isItemAction(item)) {
!noAction && (onAction == null ? void 0 : onAction(item));
if (isItemAction?.(item)) {
!noAction && onAction?.(item);
return true;
}
const itemValue = getItemValue(item);
if (!select) onChange(itemValue);
const endIndex = itemValue.length;
inputRef.current.setSelectionRange(endIndex, endIndex);
inputRef.current?.setSelectionRange(endIndex, endIndex);
onSelectChange(item);
};
const resetState = shouldClose => {
Expand All @@ -67,7 +66,7 @@ const autocompleteLite = ({
if (--nextIndex < baseIndex) nextIndex = itemLength - 1;
}
newItem = items[nextIndex];
if (!newItem || !(isItemDisabled != null && isItemDisabled(newItem))) break;
if (!newItem || !isItemDisabled?.(newItem)) break;
if (++itemCounter >= itemLength) return;
}
setFocusItem(newItem);
Expand Down Expand Up @@ -108,7 +107,7 @@ const autocompleteLite = ({
'aria-selected': select ? isItemSelected(item) : isEqual(item, focusItem),
ref: isEqual(item, focusItem) ? scrollIntoView : null,
onClick: () => {
if (!(isItemDisabled != null && isItemDisabled(item))) {
if (!isItemDisabled?.(item)) {
resetState(selectItemOrAction(item));
}
}
Expand Down
8 changes: 2 additions & 6 deletions dist/esm/features/atom/dropdownToggle.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,9 @@ const dropdownToggle = ({
const toggleRef = useRef(null);
const inputValue = tmpValue || value || '';
useEffect(() => {
var _inputRef$current;
if (open) (_inputRef$current = inputRef.current) == null || _inputRef$current.focus();
if (open) inputRef.current?.focus();
}, [open, inputRef]);
const focusToggle = () => setTimeout(() => {
var _toggleRef$current;
return (_toggleRef$current = toggleRef.current) == null ? void 0 : _toggleRef$current.focus();
}, 0);
const focusToggle = () => setTimeout(() => toggleRef.current?.focus(), 0);
return {
isInputEmpty: !inputValue,
getToggleProps: () => ({
Expand Down
2 changes: 1 addition & 1 deletion dist/esm/features/atom/multiInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const multiInput = () => ({
}),
getInputProps: () => ({
onBlur: inCapture,
onKeyDown: e => !e.target.value && e.key === 'Backspace' && (removeSelect == null ? void 0 : removeSelect())
onKeyDown: e => !e.target.value && e.key === 'Backspace' && removeSelect?.()
})
};
};
Expand Down
2 changes: 1 addition & 1 deletion dist/esm/hooks/useAutocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const useAutocomplete = ({
id: useId(),
tmpValue,
setTmpValue,
onChange: newValue => passthrough.value != newValue && (onChange == null ? void 0 : onChange(newValue)),
onChange: newValue => passthrough.value != newValue && onChange?.(newValue),
...passthrough,
...state
});
Expand Down
4 changes: 2 additions & 2 deletions dist/esm/hooks/useCombobox.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ const useCombobox = ({
getSelectedValue: () => getItemValue(selected),
onSelectChange: newItem => {
if (!isEqual(newItem, selected)) {
onSelectChange == null || onSelectChange(newItem);
onSelectChange?.(newItem);
} else if (flipOnSelect) {
onSelectChange == null || onSelectChange();
onSelectChange?.();
}
}
});
Expand Down
8 changes: 2 additions & 6 deletions dist/esm/hooks/useFocusCapture.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,11 @@ const useFocusCapture = focusRef => {
if (document.activeElement === focusRef.current) mutable.a = 1;
}, () => {
if (mutable.a) {
var _focusRef$current;
mutable.a = 0;
(_focusRef$current = focusRef.current) == null || _focusRef$current.focus();
focusRef.current?.focus();
return true;
}
}, () => {
var _focusRef$current2;
return (_focusRef$current2 = focusRef.current) == null ? void 0 : _focusRef$current2.focus();
}];
}, () => focusRef.current?.focus()];
};

export { useFocusCapture };
8 changes: 4 additions & 4 deletions dist/esm/hooks/useMultiSelect.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ const useMultiSelect = ({
isEqual = defaultEqual,
getItemValue,
selected,
onSelectChange: _onSelectChange = () => {},
onSelectChange,
flipOnSelect,
...passthrough
}) => {
const removeItem = itemToRemove => _onSelectChange(selected.filter(item => !isEqual(itemToRemove, item)));
const removeItem = itemToRemove => onSelectChange?.(selected.filter(item => !isEqual(itemToRemove, item)));
const removeSelect = item => {
if (item) {
removeItem(item);
} else {
selected.length && _onSelectChange(selected.slice(0, selected.length - 1));
selected.length && onSelectChange?.(selected.slice(0, selected.length - 1));
}
};
return {
Expand All @@ -28,7 +28,7 @@ const useMultiSelect = ({
onSelectChange: newItem => {
if (!newItem) return;
if (selected.findIndex(item => isEqual(item, newItem)) < 0) {
_onSelectChange([...selected, newItem]);
onSelectChange?.([...selected, newItem]);
} else if (flipOnSelect) {
removeItem(newItem);
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@szhsin/react-autocomplete",
"version": "0.9.5",
"version": "0.9.6",
"description": "",
"author": "Zheng Song",
"license": "MIT",
Expand Down
2 changes: 1 addition & 1 deletion src/features/atom/autocompleteLite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ const autocompleteLite =
const itemValue = getItemValue(item);
if (!select) onChange(itemValue);
const endIndex = itemValue.length;
inputRef.current!.setSelectionRange(endIndex, endIndex);
inputRef.current?.setSelectionRange(endIndex, endIndex);
// We place onSelectChange after onChange to give user an opportunity
// to manipulate the `value` state
onSelectChange(item);
Expand Down
8 changes: 4 additions & 4 deletions src/hooks/useMultiSelect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ const useMultiSelect = <T, FeatureYield extends object>({
isEqual = defaultEqual,
getItemValue,
selected,
onSelectChange: _onSelectChange = () => {},
onSelectChange,
flipOnSelect,
...passthrough
}: MultiSelectProps<T, FeatureYield>) => {
const removeItem = (itemToRemove: T) =>
_onSelectChange(selected.filter((item) => !isEqual(itemToRemove, item)));
onSelectChange?.(selected.filter((item) => !isEqual(itemToRemove, item)));

const removeSelect: AdapterProps<T>['removeSelect'] = (item) => {
if (item) {
removeItem(item);
} else {
selected.length && _onSelectChange(selected.slice(0, selected.length - 1));
selected.length && onSelectChange?.(selected.slice(0, selected.length - 1));
}
};

Expand All @@ -33,7 +33,7 @@ const useMultiSelect = <T, FeatureYield extends object>({
if (!newItem) return;

if (selected.findIndex((item) => isEqual(item, newItem)) < 0) {
_onSelectChange([...selected, newItem]);
onSelectChange?.([...selected, newItem]);
} else if (flipOnSelect) {
removeItem(newItem);
}
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export interface PassthroughProps<T> {
isItemDisabled?: (item: T) => boolean;
isItemAction?: (item: T) => boolean;
onAction?: (item: T) => void;
value?: string | undefined;
value: string | undefined;
onChange: (value?: string | undefined) => void;
items: T[];
}
Expand Down
2 changes: 1 addition & 1 deletion types/hooks/useMultiSelect.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/// <reference types="react" />
import type { MultiSelectProps } from '../types';
declare const useMultiSelect: <T, FeatureYield extends object>({ isEqual, getItemValue, selected, onSelectChange: _onSelectChange, flipOnSelect, ...passthrough }: MultiSelectProps<T, FeatureYield>) => {
declare const useMultiSelect: <T, FeatureYield extends object>({ isEqual, getItemValue, selected, onSelectChange, flipOnSelect, ...passthrough }: MultiSelectProps<T, FeatureYield>) => {
inputRef: import("react").RefObject<HTMLInputElement>;
focusItem: T | undefined;
setFocusItem: (item?: T | undefined) => void;
Expand Down
2 changes: 1 addition & 1 deletion types/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export interface PassthroughProps<T> {
isItemDisabled?: (item: T) => boolean;
isItemAction?: (item: T) => boolean;
onAction?: (item: T) => void;
value?: string | undefined;
value: string | undefined;
onChange: (value?: string | undefined) => void;
items: T[];
}
Expand Down

0 comments on commit 41c2302

Please sign in to comment.