Skip to content

fix: Last one also take in count #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 4, 2021
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
61 changes: 30 additions & 31 deletions src/Overflow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,23 @@ import Item from './Item';
import { useBatchFrameState } from './hooks/useBatchFrameState';
import RawItem from './RawItem';

export const OverflowContext = React.createContext<{
prefixCls: string;
responsive: boolean;
order: number;
registerSize: (key: React.Key, width: number | null) => void;
display: boolean;
export const OverflowContext =
React.createContext<{
prefixCls: string;
responsive: boolean;
order: number;
registerSize: (key: React.Key, width: number | null) => void;
display: boolean;

invalidate: boolean;
invalidate: boolean;

// Item Usage
item?: any;
itemKey?: React.Key;
// Item Usage
item?: any;
itemKey?: React.Key;

// Rest Usage
className?: string;
}>(null);
// Rest Usage
className?: string;
}>(null);

const RESPONSIVE = 'responsive' as const;
const INVALIDATE = 'invalidate' as const;
Expand Down Expand Up @@ -90,7 +91,7 @@ function Overflow<ItemType = any>(

const createUseState = useBatchFrameState();

const fullySSR = ssr === 'full'
const fullySSR = ssr === 'full';

const [containerWidth, setContainerWidth] = createUseState<number>(null);
const mergedContainerWidth = containerWidth || 0;
Expand All @@ -106,13 +107,13 @@ function Overflow<ItemType = any>(
const [suffixFixedStart, setSuffixFixedStart] = useState<number>(null);

const [displayCount, setDisplayCount] = useState(null);
const mergedDisplayCount = React.useMemo(() => {
if (displayCount === null && fullySSR) {
return Number.MAX_SAFE_INTEGER;
}
const mergedDisplayCount = React.useMemo(() => {
if (displayCount === null && fullySSR) {
return Number.MAX_SAFE_INTEGER;
}

return displayCount || 0;
}, [displayCount, containerWidth]);
return displayCount || 0;
}, [displayCount, containerWidth]);

const [restReady, setRestReady] = useState(false);

Expand Down Expand Up @@ -241,8 +242,11 @@ function Overflow<ItemType = any>(
totalWidth += currentItemWidth;

if (
i === lastIndex - 1 &&
totalWidth + getItemWidth(lastIndex)! <= mergedContainerWidth
// Only one means `totalWidth` is the final width
(lastIndex === 0 && totalWidth <= mergedContainerWidth) ||
// Last two width will be the final width
(i === lastIndex - 1 &&
totalWidth + getItemWidth(lastIndex)! <= mergedContainerWidth)
) {
// Additional check if match the end
updateDisplayCount(lastIndex);
Expand All @@ -255,11 +259,6 @@ function Overflow<ItemType = any>(
totalWidth - currentItemWidth - suffixWidth + restWidth,
);
break;
} else if (i === lastIndex) {
// Reach the end
updateDisplayCount(lastIndex);
setSuffixFixedStart(totalWidth - suffixWidth);
break;
}
}

Expand Down Expand Up @@ -425,9 +424,9 @@ type FilledOverflowType = ForwardOverflowType & {

ForwardOverflow.displayName = 'Overflow';

((ForwardOverflow as unknown) as FilledOverflowType).Item = RawItem;
((ForwardOverflow as unknown) as FilledOverflowType).RESPONSIVE = RESPONSIVE;
((ForwardOverflow as unknown) as FilledOverflowType).INVALIDATE = INVALIDATE;
(ForwardOverflow as unknown as FilledOverflowType).Item = RawItem;
(ForwardOverflow as unknown as FilledOverflowType).RESPONSIVE = RESPONSIVE;
(ForwardOverflow as unknown as FilledOverflowType).INVALIDATE = INVALIDATE;

// Convert to generic type
export default (ForwardOverflow as unknown) as FilledOverflowType;
export default ForwardOverflow as unknown as FilledOverflowType;
50 changes: 39 additions & 11 deletions tests/responsive.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,12 @@ describe('Overflow.Responsive', () => {
wrapper.initSize(100, 20); // [0][1][2][3][4][+2](5)(6)
expect(wrapper.findItems()).toHaveLength(6);
[true, true, true, true, false, false].forEach((display, i) => {
expect(
wrapper
.findItems()
.at(i)
.props().display,
).toBe(display);
expect(wrapper.findItems().at(i).props().display).toBe(display);
});
expect(wrapper.findRest()).toHaveLength(1);
expect(wrapper.findRest().text()).toEqual('+ 2 ...');
expect(
wrapper
.findItems()
.find('div')
.last()
.prop('aria-hidden'),
wrapper.findItems().find('div').last().prop('aria-hidden'),
).toBeTruthy();
});

Expand All @@ -73,6 +64,21 @@ describe('Overflow.Responsive', () => {
expect(wrapper.findRest().props().display).toBeFalsy();
});

it('just fit', () => {
const wrapper = mount(
<Overflow<ItemType>
data={getData(1)}
itemKey="key"
renderItem={renderItem}
maxCount="responsive"
/>,
);
wrapper.initSize(20, 20);

expect(wrapper.findItems()).toHaveLength(1);
expect(wrapper.findRest().props().display).toBeFalsy();
});

it('remove to clean up', () => {
const data = getData(6);

Expand Down Expand Up @@ -219,4 +225,26 @@ describe('Overflow.Responsive', () => {
expect(wrapper.findSuffix().props().style.position).toBeFalsy();
});
});

it('render rest directly', () => {
const wrapper = mount(
<Overflow<ItemType>
data={getData(10)}
itemKey="key"
renderItem={renderItem}
maxCount="responsive"
renderRawRest={omitItems => {
return (
<Overflow.Item component="span" className="custom-rest">
{omitItems.length}
</Overflow.Item>
);
}}
/>,
);

wrapper.initSize(100, 20);

expect(wrapper.find('span.custom-rest').text()).toEqual('6');
});
});