Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/@react-aria/utils/src/useResizeObserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ function hasResizeObserver() {
}

type useResizeObserverOptionsType<T> = {
ref: RefObject<T | undefined>,
ref: RefObject<T | undefined> | undefined,
onResize: () => void
}

Expand Down
14 changes: 8 additions & 6 deletions packages/@react-spectrum/actionbar/src/ActionBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,16 @@ function ActionBar<T extends object>(props: SpectrumActionBarProps<T>, ref: DOMR
in={isOpen}
mountOnEnter
unmountOnExit>
<ActionBarInner {...props} ref={ref} />
<ActionBarInnerWithRef {...props} ref={ref} />
</OpenTransition>
);
}

interface ActionBarInnerProps extends SpectrumActionBarProps<unknown> {
interface ActionBarInnerProps<T> extends SpectrumActionBarProps<T> {
isOpen?: boolean
}

const ActionBarInner = React.forwardRef((props: ActionBarInnerProps, ref: DOMRef<HTMLDivElement>) => {
function ActionBarInner<T>(props: ActionBarInnerProps<T>, ref: DOMRef<HTMLDivElement>) {
props = useProviderProps(props);

let {
Expand Down Expand Up @@ -101,7 +101,7 @@ const ActionBarInner = React.forwardRef((props: ActionBarInnerProps, ref: DOMRef
<ActionGroup
aria-label={stringFormatter.format('actions')}
isQuiet
staticColor={isEmphasized ? 'white' : null}
staticColor={isEmphasized ? 'white' : undefined}
overflowMode="collapse"
buttonLabelBehavior="collapse"
onAction={onAction}
Expand All @@ -113,7 +113,7 @@ const ActionBarInner = React.forwardRef((props: ActionBarInnerProps, ref: DOMRef
aria-label={stringFormatter.format('clearSelection')}
onPress={() => onClearSelection()}
isQuiet
staticColor={isEmphasized ? 'white' : null}>
staticColor={isEmphasized ? 'white' : undefined}>
<CrossLarge />
</ActionButton>
<Text UNSAFE_className={classNames(styles, 'react-spectrum-ActionBar-selectedCount')}>
Expand All @@ -125,7 +125,9 @@ const ActionBarInner = React.forwardRef((props: ActionBarInnerProps, ref: DOMRef
</div>
</FocusScope>
);
});
}

const ActionBarInnerWithRef = React.forwardRef(ActionBarInner) as <T>(props: SpectrumActionBarProps<T> & {ref?: DOMRef<HTMLDivElement>}) => ReturnType<typeof ActionBarInner>;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whyyyyy?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because of typescript :( this was the best interpretation of this discussion I could come up with https://stackoverflow.com/questions/58469229/react-with-typescript-generics-while-using-react-forwardref


/**
* TODO: Add description of component here.
Expand Down
84 changes: 45 additions & 39 deletions packages/@react-spectrum/actiongroup/src/ActionGroup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,36 +84,42 @@ function ActionGroup<T extends object>(props: SpectrumActionGroupProps<T>, ref:
}

let computeVisibleItems = (visibleItems: number) => {
let listItems = Array.from(domRef.current.children) as HTMLLIElement[];
let containerSize = orientation === 'horizontal' ? wrapperRef.current.offsetWidth : wrapperRef.current.offsetHeight;
let isShowingMenu = visibleItems < state.collection.size;
let calculatedSize = 0;
let newVisibleItems = 0;

if (isShowingMenu) {
calculatedSize += orientation === 'horizontal'
? outerWidth(listItems.pop(), false, true)
: outerHeight(listItems.pop(), false, true);
}
if (domRef.current && wrapperRef.current) {
let listItems = Array.from(domRef.current.children) as HTMLLIElement[];
let containerSize = orientation === 'horizontal' ? wrapperRef.current.offsetWidth : wrapperRef.current.offsetHeight;
let isShowingMenu = visibleItems < state.collection.size;
let calculatedSize = 0;
let newVisibleItems = 0;

if (isShowingMenu) {
let item = listItems.pop();
if (item) {
calculatedSize += orientation === 'horizontal'
? outerWidth(item, false, true)
: outerHeight(item, false, true);
}
}

for (let [i, item] of listItems.entries()) {
calculatedSize += orientation === 'horizontal'
? outerWidth(item, i === 0, i === listItems.length - 1)
: outerHeight(item, i === 0, i === listItems.length - 1);
if (calculatedSize <= containerSize) {
newVisibleItems++;
} else {
break;
for (let [i, item] of listItems.entries()) {
calculatedSize += orientation === 'horizontal'
? outerWidth(item, i === 0, i === listItems.length - 1)
: outerHeight(item, i === 0, i === listItems.length - 1);
if (calculatedSize <= containerSize) {
newVisibleItems++;
} else {
break;
}
}
}

// If selection is enabled, and not all of the items fit, collapse all of them into a dropdown
// immediately rather than having some visible and some not.
if (selectionMode !== 'none' && newVisibleItems < state.collection.size) {
return 0;
}
// If selection is enabled, and not all of the items fit, collapse all of them into a dropdown
// immediately rather than having some visible and some not.
if (selectionMode !== 'none' && newVisibleItems < state.collection.size) {
return 0;
}

return newVisibleItems;
return newVisibleItems;
}
return visibleItems;
};

setVisibleItems(function *() {
Expand Down Expand Up @@ -166,14 +172,14 @@ function ActionGroup<T extends object>(props: SpectrumActionGroupProps<T>, ref:
// in all scenarios because it may not shrink when available space is reduced.
let parentRef = useMemo(() => ({
get current() {
return wrapperRef.current.parentElement;
return wrapperRef.current?.parentElement;
}
}), [wrapperRef]);
useResizeObserver({ref: overflowMode !== 'wrap' ? parentRef : null, onResize: updateOverflow});
useResizeObserver({ref: overflowMode !== 'wrap' ? parentRef : undefined, onResize: updateOverflow});
useLayoutEffect(updateOverflow, [updateOverflow, state.collection]);

let children = [...state.collection];
let menuItem = null;
let menuItem: ReactElement | null = null;
let menuProps = {};

// If there are no visible items, don't apply any props to the action group container
Expand Down Expand Up @@ -257,16 +263,16 @@ export {_ActionGroup as ActionGroup};
interface ActionGroupItemProps<T> extends DOMProps, StyleProps {
item: Node<T>,
state: ListState<T>,
isDisabled: boolean,
isEmphasized: boolean,
isDisabled?: boolean,
isEmphasized?: boolean,
staticColor?: 'white' | 'black',
hideButtonText?: boolean,
orientation?: 'horizontal' | 'vertical',
onAction: (key: Key) => void
onAction?: (key: Key) => void
}

function ActionGroupItem<T>({item, state, isDisabled, isEmphasized, staticColor, onAction, hideButtonText, orientation}: ActionGroupItemProps<T>) {
let ref = useRef();
let ref = useRef(null);
let {buttonProps} = useActionGroupItem({key: item.key}, state);
isDisabled = isDisabled || state.disabledKeys.has(item.key);
let isSelected = state.selectionManager.isSelected(item.key);
Expand All @@ -282,7 +288,7 @@ function ActionGroupItem<T>({item, state, isDisabled, isEmphasized, staticColor,
// If button text is hidden, we need to show it as a tooltip instead, so
// go find the text element in the DOM after rendering.
let textId = useId();
let [textContent, setTextContent] = useState('');
let [textContent, setTextContent] = useState<string | null | undefined>('');
useLayoutEffect(() => {
if (hideButtonText) {
setTextContent(document.getElementById(textId)?.textContent);
Expand Down Expand Up @@ -359,7 +365,7 @@ interface ActionGroupMenuProps<T> extends AriaLabelingProps {
summaryIcon?: ReactNode,
isOnlyItem?: boolean,
orientation?: 'horizontal' | 'vertical',
onAction: (key: Key) => void
onAction?: (key: Key) => void
}

function ActionGroupMenu<T>({state, isDisabled, isEmphasized, staticColor, items, onAction, summaryIcon, hideButtonText, isOnlyItem, orientation, ...otherProps}: ActionGroupMenuProps<T>) {
Expand All @@ -376,7 +382,7 @@ function ActionGroupMenu<T>({state, isDisabled, isEmphasized, staticColor, items
let {hoverProps, isHovered} = useHover({isDisabled});

// If no aria-label or aria-labelledby is given, provide a default one.
let ariaLabel = otherProps['aria-label'] || (otherProps['aria-labelledby'] ? null : '…');
let ariaLabel = otherProps['aria-label'] || (otherProps['aria-labelledby'] ? undefined : '…');
let ariaLabelledby = otherProps['aria-labelledby'];
let textId = useId();
let id = useId();
Expand All @@ -392,14 +398,14 @@ function ActionGroupMenu<T>({state, isDisabled, isEmphasized, staticColor, items
let isSelected = state.selectionManager.selectionMode !== 'none' && !state.selectionManager.isEmpty;

// If single selection and empty selection is not allowed, swap the contents of the button to the selected item (like a Picker).
if (!summaryIcon && state.selectionManager.selectionMode === 'single' && state.selectionManager.disallowEmptySelection) {
if (!summaryIcon && state.selectionManager.selectionMode === 'single' && state.selectionManager.disallowEmptySelection && state.selectionManager.firstSelectedKey != null) {
let selectedItem = state.collection.getItem(state.selectionManager.firstSelectedKey);
if (selectedItem) {
summaryIcon = selectedItem.rendered;
if (typeof summaryIcon === 'string') {
summaryIcon = <Text>{summaryIcon}</Text>;
}
iconOnly = hideButtonText;
iconOnly = !!hideButtonText;
ariaLabelledby = `${ariaLabelledby ?? id} ${textId}`;
}
}
Expand Down Expand Up @@ -453,7 +459,7 @@ function ActionGroupMenu<T>({state, isDisabled, isEmphasized, staticColor, items
}
isDisabled={isDisabled}
staticColor={staticColor}>
{summaryIcon || <More />}
{summaryIcon ? <More /> : null}
</ActionButton>
</PressResponder>
</SlotProvider>
Expand Down
2 changes: 2 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
{
"name": "typescript-strict-plugin",
"paths": [
"./packages/@react-spectrum/action",
"./packages/@react-spectrum/button",
"./packages/@react-spectrum/checkbox",
"./packages/@react-spectrum/divider",
Expand All @@ -44,6 +45,7 @@
"./packages/@react-spectrum/text",
"./packages/@react-spectrum/view",
"./packages/@react-spectrum/well",
"./packages/@react-types/action",
"./packages/@react-types/button",
"./packages/@react-types/checkbox",
"./packages/@react-types/divider",
Expand Down