Skip to content

Commit a9b1ab4

Browse files
authored
fix(Splitter): respect size limits when container resizes (#59084)
1 parent 9c0758a commit a9b1ab4

3 files changed

Lines changed: 141 additions & 1 deletion

File tree

‎components/splitter/__tests__/index.test.tsx‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,24 @@ describe('Splitter', () => {
375375
expect(onResizeEnd).toHaveBeenCalledWith([10, 90]);
376376
});
377377

378+
it('should respect min when container shrinks after drag', async () => {
379+
containerSize = 1000;
380+
381+
const { container } = render(<SplitterDemo items={[{ min: 200 }, {}]} />);
382+
383+
await resizeSplitter();
384+
385+
mockDrag(container.querySelector('.ant-splitter-bar-dragger')!, -300);
386+
387+
containerSize = 600;
388+
await resizeSplitter();
389+
390+
const panels = container.querySelectorAll<HTMLElement>('.ant-splitter-panel');
391+
392+
expect(Number.parseFloat(panels[0].style.flexBasis)).toBeCloseTo(200);
393+
expect(Number.parseFloat(panels[1].style.flexBasis)).toBeCloseTo(400);
394+
});
395+
378396
it('with max', async () => {
379397
const onResize = jest.fn();
380398
const onResizeEnd = jest.fn();

‎components/splitter/__tests__/size.test.tsx‎

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,94 @@ describe('useSizes', () => {
9292
expect(sizes).toEqual([400, 600]);
9393
});
9494

95+
it('should respect min when container shrinks', () => {
96+
const items = [
97+
{
98+
size: 200,
99+
min: 200,
100+
},
101+
{
102+
size: 800,
103+
},
104+
];
105+
106+
const { result } = renderHook(() => useSizes(items, 600));
107+
const [, postPxSizes] = result.current;
108+
109+
expect(postPxSizes[0]).toBeCloseTo(200);
110+
expect(postPxSizes[1]).toBeCloseTo(400);
111+
});
112+
113+
it('should respect percentage min when container shrinks', () => {
114+
const items = [
115+
{
116+
size: 200,
117+
min: '40%',
118+
},
119+
{
120+
size: 800,
121+
},
122+
];
123+
124+
const { result } = renderHook(() => useSizes(items, 600));
125+
const [, postPxSizes] = result.current;
126+
127+
expect(postPxSizes[0]).toBeCloseTo(240);
128+
expect(postPxSizes[1]).toBeCloseTo(360);
129+
});
130+
131+
it('should respect max when container grows', () => {
132+
const items = [
133+
{
134+
size: 100,
135+
max: 200,
136+
},
137+
{
138+
size: 100,
139+
},
140+
];
141+
142+
const { result } = renderHook(() => useSizes(items, containerSize));
143+
const [, postPxSizes] = result.current;
144+
145+
expect(postPxSizes).toEqual([200, 800]);
146+
});
147+
148+
it('should keep collapsed panel at zero when container changes', () => {
149+
const items = [
150+
{
151+
size: 0,
152+
min: 200,
153+
},
154+
{
155+
size: 1000,
156+
},
157+
];
158+
159+
const { result } = renderHook(() => useSizes(items, 600));
160+
const [, postPxSizes] = result.current;
161+
162+
expect(postPxSizes).toEqual([0, 600]);
163+
});
164+
165+
it('should preserve proportions when limits cannot fit the container', () => {
166+
const items = [
167+
{
168+
size: 200,
169+
min: 400,
170+
},
171+
{
172+
size: 800,
173+
min: 400,
174+
},
175+
];
176+
177+
const { result } = renderHook(() => useSizes(items, 600));
178+
const [, postPxSizes] = result.current;
179+
180+
expect(postPxSizes).toEqual([120, 480]);
181+
});
182+
95183
it('should correct when all size is 0', () => {
96184
const items = [
97185
{

‎components/splitter/hooks/sizeUtil.ts‎

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,38 @@
11
type SizeUnit = number | undefined;
22

3+
function fitPtgSizes(sizes: number[], minSizes: SizeUnit[], maxSizes: SizeUnit[]): number[] {
4+
// Collapsed panels should stay at 0 even when they have a min size.
5+
const mergedMinSizes = sizes.map((size, index) => (size === 0 ? 0 : (minSizes[index] ?? 0)));
6+
const mergedMaxSizes = sizes.map((size, index) => (size === 0 ? 0 : (maxSizes[index] ?? 1)));
7+
8+
const invalidLimits = mergedMinSizes.some((min, index) => min > mergedMaxSizes[index]);
9+
const totalMin = mergedMinSizes.reduce((sum, size) => sum + size, 0);
10+
const totalMax = mergedMaxSizes.reduce((sum, size) => sum + size, 0);
11+
12+
// Keep the normalized proportions when the limits cannot fit the container.
13+
if (invalidLimits || totalMin > 1 || totalMax < 1) {
14+
return sizes;
15+
}
16+
17+
const result = sizes.map((size, index) =>
18+
Math.min(mergedMaxSizes[index], Math.max(mergedMinSizes[index], size)),
19+
);
20+
const total = result.reduce((sum, size) => sum + size, 0);
21+
22+
if (total === 1) {
23+
return result;
24+
}
25+
26+
const grow = total < 1;
27+
const spaces = result.map((size, index) =>
28+
grow ? mergedMaxSizes[index] - size : size - mergedMinSizes[index],
29+
);
30+
const totalSpace = spaces.reduce((sum, space) => sum + space, 0);
31+
const rest = 1 - total;
32+
33+
return result.map((size, index) => size + rest * (spaces[index] / totalSpace));
34+
}
35+
336
export function autoPtgSizes(
437
ptgSizes: SizeUnit[],
538
minPtgSizes: SizeUnit[],
@@ -28,7 +61,8 @@ export function autoPtgSizes(
2861
}
2962
const scale = 1 / currentTotalPtg;
3063
// We know `size` is a number here because undefinedIndexes is empty.
31-
return ptgSizes.map((size) => (size as number) * scale);
64+
const scaledSizes = ptgSizes.map((size) => (size as number) * scale);
65+
return fitPtgSizes(scaledSizes, minPtgSizes, maxPtgSizes);
3266
}
3367

3468
// Fill if exceed

0 commit comments

Comments
 (0)