Skip to content

Commit

Permalink
style: remove some non-null assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
SevenOutman committed Jan 22, 2022
1 parent 8978a49 commit 4c0a328
Show file tree
Hide file tree
Showing 20 changed files with 125 additions and 87 deletions.
1 change: 1 addition & 0 deletions src/Affix/Affix.tsx
Expand Up @@ -22,6 +22,7 @@ function useOffset(mountRef: React.RefObject<HTMLDivElement>) {
const [offset, setOffset] = useState<Offset | null>(null);

const updateOffset = useCallback(() => {
// FIXME upgrade dom-lib
setOffset(getOffset(mountRef.current!));
}, [mountRef]);

Expand Down
11 changes: 8 additions & 3 deletions src/Calendar/TimeDropdown.tsx
Expand Up @@ -82,8 +82,11 @@ const scrollTo = (time: Time, row: HTMLDivElement) => {
const node = container?.querySelector(`[data-key="${type}-${value}"]`);

if (node && container) {
const { top } = getPosition(node, container)!;
scrollTopAnimation(container, top, scrollTop(container) !== 0);
const position = getPosition(node, container);

if (position) {
scrollTopAnimation(container, position.top, scrollTop(container) !== 0);
}
}
});
};
Expand All @@ -104,7 +107,9 @@ const TimeDropdown: RsRefForwardingComponent<'div', TimeDropdownProps> = React.f
useEffect(() => {
const time = getTime({ format, date, showMeridian });
// The currently selected time scrolls to the visible range.
show && scrollTo(time, rowRef.current!);
if (show && rowRef.current) {
scrollTo(time, rowRef.current);
}
}, [date, format, show, showMeridian]);

const handleClick = (type: TimeType, d: number, event: React.MouseEvent) => {
Expand Down
4 changes: 2 additions & 2 deletions src/Cascader/DropdownMenu.tsx
Expand Up @@ -78,9 +78,9 @@ const DropdownMenu: RsRefForwardingComponent<'div', DropdownMenuProps> = React.f
}

if (activeItem) {
const position = getPosition(activeItem, column)!;
const position = getPosition(activeItem, column);
// Let the active option scroll into view.
scrollTop(column, position.top);
scrollTop(column, position?.top);
}
});
}, [prefix]);
Expand Down
26 changes: 14 additions & 12 deletions src/CheckTreePicker/CheckTreePicker.tsx
Expand Up @@ -122,14 +122,14 @@ const CheckTreePicker: PickerComponent<CheckTreePickerProps> = React.forwardRef(
placeholder,
value: controlledValue,
defaultValue = emptyArray,
defaultExpandAll,
defaultExpandAll = false,
disabledItemValues = emptyArray,
expandItemValues: controlledExpandItemValues,
defaultExpandItemValues,
defaultExpandItemValues = emptyArray,
height = 360,
menuStyle,
searchable = true,
virtualized,
virtualized = false,
className,
classPrefix = 'picker',
menuClassName,
Expand Down Expand Up @@ -286,15 +286,17 @@ const CheckTreePicker: PickerComponent<CheckTreePickerProps> = React.forwardRef(
};

const focusActiveNode = useCallback(() => {
focusToActiveTreeNode({
list: listRef.current!,
valueKey,
selector: `.${checkTreePrefix('node-active')}`,
activeNode,
virtualized: virtualized!,
container: treeViewRef.current!,
formattedNodes: getFormattedNodes()
});
if (listRef.current) {
focusToActiveTreeNode({
list: listRef.current,
valueKey,
selector: `.${checkTreePrefix('node-active')}`,
activeNode,
virtualized: virtualized,
container: treeViewRef.current,
formattedNodes: getFormattedNodes()
});
}
}, [checkTreePrefix, activeNode, getFormattedNodes, valueKey, virtualized]);

useEffect(() => {
Expand Down
17 changes: 10 additions & 7 deletions src/CheckTreePicker/utils.ts
Expand Up @@ -104,15 +104,18 @@ export function isEveryFirstLevelNodeUncheckable(
* get node uncheckable state
* @param {*} node
*/
export function isNodeUncheckable(node: any, props: Partial<CheckTreePickerProps>) {
export function isNodeUncheckable(
node: any,
props: Required<Pick<CheckTreePickerProps, 'uncheckableItemValues' | 'valueKey'>>
) {
const { uncheckableItemValues = [], valueKey } = props;
return uncheckableItemValues.some((value: any) => shallowEqual(node[valueKey!], value));
return uncheckableItemValues.some((value: any) => shallowEqual(node[valueKey], value));
}

export function getFormattedTree(
data: any[],
nodes: TreeNodesType,
props: Partial<CheckTreePickerProps>
props: Required<Pick<CheckTreePickerProps, 'childrenKey' | 'cascade'>>
) {
const { childrenKey, cascade } = props;
return data.map((node: any) => {
Expand All @@ -127,8 +130,8 @@ export function getFormattedTree(
formatted.uncheckable = curNode.uncheckable;
formatted.parent = curNode.parent;
formatted.checkState = checkState;
if (node[childrenKey!]?.length > 0) {
formatted[childrenKey!] = getFormattedTree(formatted[childrenKey!], nodes, props);
if (node[childrenKey]?.length > 0) {
formatted[childrenKey] = getFormattedTree(formatted[childrenKey], nodes, props);
}
}

Expand All @@ -139,14 +142,14 @@ export function getFormattedTree(
export function getDisabledState(
nodes: TreeNodesType,
node: TreeNodeType,
props: Partial<CheckTreePickerProps>
props: Required<Pick<CheckTreePickerProps, 'disabledItemValues' | 'valueKey'>>
) {
const { disabledItemValues = [], valueKey } = props;
if (!isNil(node.refKey) && isNil(nodes[node.refKey])) {
return false;
}
return disabledItemValues.some((value: any) =>
shallowEqual(nodes[node.refKey!][valueKey!], value)
shallowEqual(nodes[node.refKey!][valueKey], value)
);
}

Expand Down
12 changes: 7 additions & 5 deletions src/DateRangePicker/DateRangePicker.tsx
Expand Up @@ -341,14 +341,14 @@ const DateRangePicker: DateRangePicker = React.forwardRef((props: DateRangePicke
// and waiting for user to select the second date to complete the selection.
if (!hasDoneSelect.current) {
// If `hoverRange` is set, you need to change the value of hoverDateRange according to the rules
if (!isNil(nextHoverDateRange)) {
if (!isNil(nextHoverDateRange) && !isNil(selectRangeValueRef.current)) {
let nextSelectedDates: DateRange = [
selectRangeValueRef.current![0],
selectRangeValueRef.current[0],
nextHoverDateRange[1]
];

if (DateUtils.isBefore(nextHoverDateRange[0], selectRangeValueRef.current![0])) {
nextSelectedDates = [nextHoverDateRange[0], selectRangeValueRef.current![1]];
if (DateUtils.isBefore(nextHoverDateRange[0], selectRangeValueRef.current[0])) {
nextSelectedDates = [nextHoverDateRange[0], selectRangeValueRef.current[1]];
}
setSelectedDates(nextSelectedDates);
} else {
Expand Down Expand Up @@ -414,7 +414,9 @@ const DateRangePicker: DateRangePicker = React.forwardRef((props: DateRangePicke
}

setHoverDateRange(
nextSelectDates.length === 2 ? nextSelectDates : [nextSelectDates[0]!, nextSelectDates[0]!]
nextSelectDates.length === 2
? nextSelectDates
: [nextSelectDates[0] as Date, nextSelectDates[0] as Date]
);
setSelectedDates(nextSelectDates);
updateCalendarDate(nextSelectDates);
Expand Down
5 changes: 4 additions & 1 deletion src/InputPicker/InputAutosize.tsx
Expand Up @@ -132,7 +132,10 @@ const InputAutosize = React.forwardRef(
return;
}

copyStyles(inputStyles, sizerRef.current!);
if (sizerRef.current) {
copyStyles(inputStyles, sizerRef.current);
}

if (placeholderRef.current) {
copyStyles(inputStyles, placeholderRef.current);
}
Expand Down
7 changes: 5 additions & 2 deletions src/InputPicker/InputPicker.tsx
Expand Up @@ -205,7 +205,7 @@ const InputPicker: PickerComponent<InputPickerProps> = React.forwardRef(
{
data: getAllDataAndCache(),
valueKey,
target: () => overlayRef.current!
target: () => overlayRef.current
}
);

Expand Down Expand Up @@ -432,13 +432,16 @@ const InputPicker: PickerComponent<InputPickerProps> = React.forwardRef(
const allData = getAllData();
let focusItem = allData.find(item => shallowEqual(item[valueKey], focusItemValue));

// FIXME Bad state flow
if (!focusItem && focusItemValue === searchKeyword) {
focusItem = createOption(searchKeyword);
}
setValue(focusItemValue);
setSearchKeyword('');

handleSelect(focusItemValue, focusItem!, event);
if (focusItem) {
handleSelect(focusItemValue, focusItem, event);
}
handleChange(focusItemValue, event);
handleClose();
},
Expand Down
14 changes: 8 additions & 6 deletions src/List/ListItem.tsx
Expand Up @@ -34,12 +34,14 @@ const ListItem = React.forwardRef((props: ListItemProps, ref: React.Ref<HTMLDivE
const listItemRef = useRef<HTMLElement>(null);

useEffect(() => {
const { unregister } = register({
node: listItemRef.current!,
edgeOffset: null,
info: { collection, disabled, index }
});
return unregister;
if (listItemRef.current) {
const { unregister } = register({
node: listItemRef.current,
edgeOffset: null,
info: { collection, disabled, index }
});
return unregister;
}
}, [collection, disabled, index, register]);

const classes = merge(className, withClassPrefix(size, { disabled, bordered }));
Expand Down
20 changes: 11 additions & 9 deletions src/Menu/MenuItem.tsx
Expand Up @@ -91,17 +91,19 @@ function MenuItem(props: MenuItemProps) {
}, [dispatch]);

useEffect(() => {
const menuitemElement = menuitemRef.current!;
const menuitemElement = menuitemRef.current;

dispatch({
type: MenuActionTypes.RegisterItem,
element: menuitemElement,
props: { disabled }
});
if (menuitemElement) {
dispatch({
type: MenuActionTypes.RegisterItem,
element: menuitemElement,
props: { disabled }
});

return () => {
dispatch({ type: MenuActionTypes.UnregisterItem, id: menuitemElement.id });
};
return () => {
dispatch({ type: MenuActionTypes.UnregisterItem, id: menuitemElement.id });
};
}
}, [menuitemRef, disabled, dispatch]);

const menuitemProps: React.LiHTMLAttributes<HTMLLIElement> & MenuitemRenderProps = {
Expand Down
4 changes: 2 additions & 2 deletions src/Pagination/PaginationGroup.tsx
Expand Up @@ -44,10 +44,10 @@ interface LimitPicker {
const LimitPicker = (props: LimitPicker) => {
const { disabled, limitOptions, locale, limit, onChangeLimit, size, prefix } = props;
const disabledPicker = typeof disabled === 'function' ? disabled('picker') : Boolean(disabled);
const formatlimitOptions = limitOptions!.map(item => {
const formatlimitOptions = limitOptions.map(item => {
return {
value: item,
label: tplTransform(locale!.limit!, item)
label: tplTransform(locale.limit!, item)
};
});

Expand Down
2 changes: 1 addition & 1 deletion src/Picker/DropdownMenu.tsx
Expand Up @@ -135,7 +135,7 @@ const DropdownMenu: DropdownMenuComponent = React.forwardRef<
return;
}

const position = getPosition(activeItem as HTMLElement, container)!;
const position = getPosition(activeItem, container)!;
const sTop = scrollTop(container);
const sHeight = getHeight(container);
if (sTop > position.top) {
Expand Down
5 changes: 4 additions & 1 deletion src/Picker/PickerOverlay.tsx
Expand Up @@ -68,7 +68,10 @@ const PickerOverlay: RsRefForwardingComponent<'div', PickerOverlayProps> = React
// Get the width value of the button,
// and then set it to the menu to make their width consistent.
const width = getWidth(getDOMNode(toggle.root));
addStyle(overlayRef.current!, 'min-width', `${width}px`);

if (overlayRef.current) {
addStyle(overlayRef.current, 'min-width', `${width}px`);
}
}
}, [autoWidth, target, overlayRef]);

Expand Down
2 changes: 1 addition & 1 deletion src/RangeSlider/RangeSlider.tsx
Expand Up @@ -125,7 +125,7 @@ const RangeSlider = React.forwardRef((props: RangeSliderProps, ref) => {

const getValueByPosition = useCallback(
(event: React.MouseEvent) => {
const barOffset = getOffset(barRef.current!)!;
const barOffset = getOffset(barRef.current as HTMLElement)!;
const offset = vertical
? barOffset.top + barOffset.height - event.pageY
: event.pageX - barOffset.left;
Expand Down
22 changes: 13 additions & 9 deletions src/Ripple/Ripple.tsx
Expand Up @@ -41,20 +41,24 @@ const Ripple = React.forwardRef((props: RippleProps, ref: React.Ref<HTMLSpanElem

const handleMouseDown = useCallback(
(event: React.MouseEvent) => {
const position = getPosition(triggerRef.current!, event);
setRippling(true);
setPosition(position);
onMouseDown?.(position, event);
if (triggerRef.current) {
const position = getPosition(triggerRef.current, event);
setRippling(true);
setPosition(position);
onMouseDown?.(position, event);
}
},
[onMouseDown]
);

useEffect(() => {
const parentNode = triggerRef.current!.parentNode as HTMLElement;
const mousedownListener = on(parentNode, 'mousedown', handleMouseDown);
return () => {
mousedownListener?.off();
};
const parentNode = triggerRef.current?.parentNode;
if (parentNode) {
const mousedownListener = on(parentNode, 'mousedown', handleMouseDown);
return () => {
mousedownListener?.off();
};
}
}, [handleMouseDown]);

return (
Expand Down
24 changes: 13 additions & 11 deletions src/TreePicker/TreePicker.tsx
Expand Up @@ -134,10 +134,10 @@ const TreePicker: PickerComponent<TreePickerProps> = React.forwardRef((props, re
valueKey = 'value',
childrenKey = 'children',
draggable,
defaultExpandAll,
defaultExpandAll = false,
disabledItemValues = emptyArray,
expandItemValues: controlledExpandItemValues,
defaultExpandItemValues,
defaultExpandItemValues = emptyArray,
id,
listProps,
getChildren,
Expand Down Expand Up @@ -257,15 +257,17 @@ const TreePicker: PickerComponent<TreePickerProps> = React.forwardRef((props, re
);

const focusActiveNode = useCallback(() => {
focusToActiveTreeNode({
list: listRef.current!,
valueKey,
selector: `.${treePrefix('node-active')}`,
activeNode,
virtualized,
container: treeViewRef.current!,
formattedNodes: getFormattedNodes()
});
if (listRef.current) {
focusToActiveTreeNode({
list: listRef.current,
valueKey,
selector: `.${treePrefix('node-active')}`,
activeNode,
virtualized,
container: treeViewRef.current,
formattedNodes: getFormattedNodes()
});
}
}, [treePrefix, activeNode, getFormattedNodes, valueKey, virtualized]);

useEffect(() => {
Expand Down
4 changes: 3 additions & 1 deletion src/Uploader/UploadTrigger.tsx
Expand Up @@ -63,7 +63,9 @@ const UploadTrigger = React.forwardRef((props: UploadTriggerProps, ref) => {
}, []);

const handleClearInput = useCallback(() => {
inputRef.current!.value = '';
if (inputRef.current) {
inputRef.current.value = '';
}
}, []);

const handleDragEnter = useCallback(
Expand Down
2 changes: 1 addition & 1 deletion src/Uploader/Uploader.tsx
Expand Up @@ -312,7 +312,7 @@ const Uploader = React.forwardRef((props: UploaderProps, ref) => {
* Clear the value in input.
*/
const cleanInputValue = useCallback(() => {
trigger.current!.clearInput();
trigger.current?.clearInput();
}, []);

/**
Expand Down

0 comments on commit 4c0a328

Please sign in to comment.