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
61 changes: 61 additions & 0 deletions src/packages/calendar/__tests__/calendar.scss.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import fs from 'fs'
import path from 'path'
import sass from 'sass'

const compileCalendarScss = () => {
const file = path.resolve(__dirname, '../calendar.scss')
// 与 vite.config.mts 保持一致:相对 @import 由构建移除,变量在文件头注入
const source = fs
.readFileSync(file, 'utf8')
.replace(/@import\s+['"](\.{2}?\/)[^'".]+(.s?css)['"];/g, '')
return sass
.compileString(`@import "styles/variables.scss";\n${source}`, {
loadPaths: [path.resolve(__dirname, '../../../')],
quietDeps: true,
logger: { warn() {}, debug() {} },
})
.css.replace(/\r\n/g, '\n')
}

const declarations = (css: string, selector: string) => {
const match = css.match(new RegExp(`(?:^|\\n)${selector} \\{([^}]*)\\}`))
if (!match) {
throw new Error(`未找到选择器 ${selector}`)
}
return match[1]
}

describe('calendar.scss', () => {
const css = compileCalendarScss()
const skyline = '.nut-calendar.nut-calendar-skyline'

test('H5 / WebView 内容区保留块级滚动布局,可跨月滚动', () => {
const content = declarations(css, '.nut-calendar-content')

expect(content).toContain('display: block')
expect(content).toContain('overflow-y: auto')
expect(content).not.toContain('flex-direction: column')
})

test('H5 / WebView 日期保持 float 布局', () => {
const days = declarations(css, '.nut-calendar-days')
const day = declarations(css, '.nut-calendar-day')

expect(days).not.toContain('display: flex')
expect(day).toContain('float: left')
expect(day).not.toContain('flex: 0 0')
})

test('Skyline 的 flex 覆盖限定在 nut-calendar-skyline 之下', () => {
expect(declarations(css, `${skyline} .nut-calendar-content`)).toContain(
'overflow-y: visible'
)
expect(declarations(css, `${skyline} .nut-calendar-days`)).toContain(
'flex-wrap: wrap'
)
expect(declarations(css, `${skyline} .nut-calendar-day`)).toContain(
'float: none'
)
expect(css).not.toMatch(/\n\.nut-calendar-skyline/)
})
})
48 changes: 48 additions & 0 deletions src/packages/calendar/calendar.scss
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,10 @@
flex-direction: column;
position: relative;
float: left;
// 先写字面量再写 var():Skyline 不支持 CSS 变量时回退到字面量
width: 14.28%;
width: $calendar-day-width;
height: 60px;
height: $calendar-day-height;
font-weight: $calendar-day-font-weight;
margin-bottom: 4px;
Expand Down Expand Up @@ -249,6 +252,51 @@
}
}

// Skyline 对 float / overflow 滚动支持不足:仅在 Skyline 环境下(Taro 端由 calendaritem.taro.tsx
// 判定环境后给根节点追加 .nut-calendar-skyline)切换为 flex 布局,
// H5 / WebView 保持上面原有的块级滚动布局。
.nut-calendar.nut-calendar-skyline {
.nut-calendar-weeks {
flex-direction: row;
}

.nut-calendar-week-item {
width: $calendar-day-width;
text-align: center;
}

.nut-calendar-content {
display: flex;
flex-direction: column;
overflow-y: visible;
}

.nut-calendar-pannel {
.viewArea {
width: 100%;
}
}

.nut-calendar-month {
width: 100%;
}

.nut-calendar-days {
display: flex;
flex-direction: row;
flex-wrap: wrap;
align-items: center;
width: 100%;
}

.nut-calendar-day {
flex: 0 0 14.28%;
flex: 0 0 $calendar-day-width;
float: none;
box-sizing: border-box;
}
}

.nut-calendar-popup .nut-popup-title-right {
top: 7px !important;
}
Expand Down
88 changes: 88 additions & 0 deletions src/packages/calendaritem/__tests__/calendaritem.taro.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/* eslint-disable global-require */
import * as React from 'react'
import { render } from '@testing-library/react'
import '@testing-library/jest-dom'
import { isSkyline } from '@/utils/taro/is-skyline'
import { CalendarItem } from '../calendaritem.taro'

const { MockView, MockScrollView } = vi.hoisted(() => {
const React = require('react')
const MockView = React.forwardRef(
({ children, catchMove, ...props }: any, ref: any) =>
React.createElement('div', { ...props, ref }, children)
)
MockView.displayName = 'MockView'
const MockScrollView = React.forwardRef(
(
{
children,
scrollY,
scrollTop,
scrollWithAnimation,
type,
...props
}: any,
ref: any
) =>
React.createElement(
'div',
{ ...props, ref, 'data-scroll-view': '' },
children
)
)
MockScrollView.displayName = 'MockScrollView'
return { MockView, MockScrollView }
})

vi.mock('@tarojs/components', () => ({
View: MockView,
ScrollView: MockScrollView,
}))

vi.mock('@tarojs/taro', () => ({
default: { getEnv: () => 'WEAPP' },
nextTick: (cb: () => void) => cb(),
}))

vi.mock('@/utils/taro/is-skyline', () => ({ isSkyline: vi.fn() }))

const mockedIsSkyline = vi.mocked(isSkyline)

describe('CalendarItem (Taro)', () => {
beforeEach(() => {
mockedIsSkyline.mockReset()
})

test('内容区使用 ScrollView 渲染', () => {
mockedIsSkyline.mockReturnValue(false)
const { container } = render(
<CalendarItem startDate="2022-01-01" endDate="2022-03-31" />
)

const content = container.querySelector('.nut-calendar-content')
expect(content).toHaveAttribute('data-scroll-view')
expect(container.querySelectorAll('.nut-calendar-month').length).toBe(3)
})

test('非 Skyline 环境不追加 Skyline 修饰类,保持原有布局', () => {
mockedIsSkyline.mockReturnValue(false)
const { container } = render(
<CalendarItem startDate="2022-01-01" endDate="2022-03-31" />
)

const root = container.querySelector('.nut-calendar') as HTMLElement
expect(root).not.toHaveClass('nut-calendar-skyline')
})

test('Skyline 环境给根节点追加 nut-calendar-skyline,启用 flex 覆盖样式', () => {
mockedIsSkyline.mockReturnValue(true)
const { container } = render(
<CalendarItem startDate="2022-01-01" endDate="2022-03-31" />
)

const root = container.querySelector('.nut-calendar') as HTMLElement
expect(root).toHaveClass('nut-calendar-skyline')
// 覆盖样式只在根节点的修饰类下生效,内部节点不应散落修饰类
expect(container.querySelectorAll('.nut-calendar-skyline').length).toBe(1)
})
})
6 changes: 5 additions & 1 deletion src/packages/calendaritem/calendaritem.taro.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
import requestAniFrame from '@/utils/raf'
import { useConfig } from '@/packages/configprovider/index.taro'
import { usePropsValue } from '@/hooks/use-props-value'
import { isSkyline } from '@/utils/taro/is-skyline'
import {
splitDate,
isMultiple,
Expand Down Expand Up @@ -533,7 +534,8 @@ export const CalendarItem = React.forwardRef<
if (monthsData.length <= 1) {
return
}
const scrollTop = (e.target as HTMLElement).scrollTop
const scrollTop =
e.detail?.scrollTop ?? (e.target as HTMLElement)?.scrollTop ?? 0
Taro.getEnv() === 'WEB' && setScrollTop(scrollTop)
let current = Math.floor(scrollTop / avgHeight)
if (current < 0) return
Expand Down Expand Up @@ -730,6 +732,8 @@ export const CalendarItem = React.forwardRef<
{
[`${classPrefix}-title`]: !popup,
[`${classPrefix}-nofooter`]: !!autoBackfill,
// Skyline 不支持 float / overflow 滚动布局,追加修饰类启用 calendar.scss 中的 flex 覆盖样式
[`${classPrefix}-skyline`]: isSkyline(),
},
classPrefix,
className
Expand Down
Loading