Skip to content

Commit ce7517f

Browse files
committed
fix(noticebar): first item is not visible when scrolling vertically
1 parent 2be2459 commit ce7517f

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

src/packages/noticebar/__test__/noticebar.spec.tsx

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,3 +220,43 @@ test('vertical event test', async () => {
220220
fireEvent.click(box)
221221
await waitFor(() => expect(handleClick).toBeCalled())
222222
})
223+
224+
test('vertical container height calculation with children', async () => {
225+
const horseLamp1 = [
226+
'NoticeBar 公告栏',
227+
'Cascader 级联选择',
228+
'DatePicker 日期选择器',
229+
'CheckBox 复选按钮',
230+
]
231+
const height = 50
232+
const { container } = render(
233+
<NoticeBar direction="vertical" height={height} speed={10} duration={1000}>
234+
{horseLamp1.map((item, index) => {
235+
return (
236+
<div
237+
className="custom-item"
238+
style={{ height: `${height}px`, lineHeight: `${height}px` }}
239+
key={index}
240+
>
241+
{item}
242+
</div>
243+
)
244+
})}
245+
</NoticeBar>
246+
)
247+
248+
await waitFor(
249+
() => {
250+
const wrapElement = container.querySelector('.nut-noticebar-box-wrap')
251+
if (wrapElement) {
252+
// 验证容器高度应该是 (childCount + 1) * height
253+
// childCount = 4, height = 50, 所以期望高度是 (4 + 1) * 50 = 250px
254+
const expectedHeight = `${(horseLamp1.length + 1) * height}px`
255+
console.log(wrapElement, 'wrapElement')
256+
expect(wrapElement).toHaveStyle(`height: ${expectedHeight}`)
257+
}
258+
},
259+
// 由于init中并不会立刻设置样式,所以需要等待一段时间
260+
{ timeout: 3000 }
261+
)
262+
})

src/packages/noticebar/noticebar.taro.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ export const NoticeBar: FunctionComponent<
358358
target.style.transitionDuration = `${
359359
swiperRef.current.moving ? 0 : duration
360360
}ms`
361-
target.style.height = `${Number(height) * childCount}px`
361+
target.style.height = `${Number(height) * (childCount + 1)}px`
362362
target.style.transform = `translate3D(0,${_offset}px,0)`
363363
}
364364
// 无缝滚动第一个元素位移控制

src/packages/noticebar/noticebar.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ export const NoticeBar: FunctionComponent<
353353
target.style.transitionDuration = `${
354354
swiperRef.current.moving ? 0 : duration
355355
}ms`
356-
target.style.height = `${Number(height) * childCount}px`
356+
target.style.height = `${Number(height) * (childCount + 1)}px`
357357
target.style.transform = `translate3D(0,${_offset}px,0)`
358358
}
359359
// 无缝滚动第一个元素位移控制

0 commit comments

Comments
 (0)