Skip to content
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

feat: datetime添加date-hour类型 #887

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

luoqiz
Copy link

@luoqiz luoqiz commented Feb 11, 2025

🤔 这个 PR 的性质是?(至少选择一个)

  • 日常 bug 修复
  • 新特性提交
  • 站点、文档改进
  • 演示代码改进
  • 组件样式/交互改进
  • TypeScript 定义更新
  • CI/CD 改进
  • 包体积优化
  • 性能优化
  • 功能增强
  • 国际化改进
  • 代码重构
  • 代码风格优化
  • 测试用例
  • 分支合并
  • 其他改动(是关于什么的改动?)

🔗 相关 Issue

💡 需求背景和解决方案

☑️ 请求合并前的自查清单

⚠️ 请自检并全部勾选全部选项⚠️

  • 文档已补充或无须补充
  • 代码演示已提供或无须提供
  • TypeScript 定义已补充或无须补充

Summary by CodeRabbit

  • 新功能
    • 添加了带“年月日时”标签的日期与小时选择器,方便用户快捷选择具体日期和小时。
    • 引入全新日期时间视图,支持日期和小时组合选择,并在确认时展示所选时间。
    • 优化了显示格式和选项验证,进一步提升了选择体验。

Copy link

vercel bot commented Feb 11, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
wot-design-uni ✅ Ready (Inspect) Visit Preview 💬 Add feedback Feb 11, 2025 0:57am

Copy link

coderabbitai bot commented Feb 11, 2025

Walkthrough

这次改动在两个页面中新增了“年月日时”日期时间选择器,分别使用 wd-datetime-pickerwd-datetime-picker-view 组件,添加了相应的响应式变量和事件处理函数。同时,对日期时间类型进行了扩展,新增了 'date-hour' 选项,并更新了相关组件内部的数值格式化和范围逻辑。所有改动均为增量更新,未变更既有功能。

Changes

文件(s) 改动总结
src/pages/datetimePicker/Index.vue
src/pages/datetimePickerView/Index.vue
在模板中新增 “年月日时” 选择器组件,绑定新响应式变量 (value20),并添加事件处理函数 (handleConfirm20, onChange20)
src/uni_modules/.../types.ts
src/uni_modules/.../wd-datetime-picker-view.vue
扩展 DateTimeType 类型,新增 'date-hour' 选项,并调整组件内部以支持该类型(如范围获取与列变更处理)
src/uni_modules/.../wd-datetime-picker.vue 更新默认显示格式和列禁用规则,支持 date-hour 类型的格式化和有效性判断

Sequence Diagram(s)

sequenceDiagram
    participant U as 用户
    participant DP as wd-datetime-picker 组件
    participant HC as handleConfirm20 方法
    U->>DP: 选择日期和小时
    DP->>HC: 触发确认事件
    HC->>U: 日志输出选中值
Loading
sequenceDiagram
    participant U as 用户
    participant DTV as wd-datetime-picker-view 组件
    participant OC as onChange20 方法
    U->>DTV: 选择日期和小时
    DTV->>OC: 触发变更事件
    OC->>U: 弹出 Toast 显示选中值
Loading

Suggested reviewers

  • Moonofweisheng

Poem

我是一只小兔子,
跳跃在代码的原野里,
“年月日时”新奇多变,
更新之风吹满枝,
让我们欢庆这改动,
宝贝代码笑嘻嘻。
🐰✨ Happy coding!

✨ Finishing Touches
  • 📝 Generate Docstrings (Beta)

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

netlify bot commented Feb 11, 2025

Deploy Preview for wot-design-uni ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit a63b271
🔍 Latest deploy log https://app.netlify.com/sites/wot-design-uni/deploys/67ab48e7da2cb4000836190f
😎 Deploy Preview https://deploy-preview-887--wot-design-uni.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (7)
src/pages/datetimePickerView/Index.vue (1)

49-49: 建议改进类型安全性

value20onChange20 的类型定义可以更严格:

-const value20 = ref<string>('')
+const value20 = ref<number | string>('')

-function onChange20({ value }: any) {
+function onChange20({ value }: { value: number | string }) {
   toast.show('选择了' + new Date(value))
 }

Also applies to: 96-98

src/uni_modules/wot-design-uni/components/wd-datetime-picker-view/types.ts (1)

4-4: 建议完善类型文档

为新增的 'date-hour' 类型添加 JSDoc 注释,说明其用途和格式。例如:

+/**
+ * 日期时间类型
+ * @type {string}
+ * - 'date' - 年月日
+ * - 'year-month' - 年月
+ * - 'time' - 时分
+ * - 'datetime' - 年月日时分
+ * - 'year' - 年
+ * - 'date-hour' - 年月日时
+ */
export type DateTimeType = 'date' | 'year-month' | 'time' | 'datetime' | 'year' | 'date-hour'
src/pages/datetimePicker/Index.vue (1)

7-7: 建议增加错误处理

date-hour 类型的日期时间选择器中,建议添加错误处理和验证:

-<wd-datetime-picker label="年月日时" v-model="value20" type="date-hour" @confirm="handleConfirm20" :formatter="formatter" />
+<wd-datetime-picker
+  label="年月日时"
+  v-model="value20"
+  type="date-hour"
+  @confirm="handleConfirm20"
+  :formatter="formatter"
+  :before-confirm="validateDateHour"
+/>

并添加验证函数:

const validateDateHour = (value: number, resolve: (isPass: boolean) => void) => {
  const date = new Date(value)
  if (date.getTime() < Date.now()) {
    resolve(true)
  } else {
    toast.error('不能选择未来的时间')
    resolve(false)
  }
}
src/uni_modules/wot-design-uni/components/wd-datetime-picker-view/wd-datetime-picker-view.vue (3)

124-125: 建议改进类型验证的实现方式

建议将类型列表提取为常量,避免重复定义:

+const DATETIME_TYPES = ['date', 'year-month', 'time', 'datetime', 'year', 'date-hour'] as const
+type DateTimeType = typeof DATETIME_TYPES[number]

watch(
  () => props.type,
  (target) => {
-    const type = ['date', 'year-month', 'time', 'datetime', 'year', 'date-hour']
-    if (type.indexOf(target) === -1) {
+    if (!DATETIME_TYPES.includes(target)) {
      console.error(`type must be one of ${type}`)
    }
    updateValue()
  }
)

437-439: 建议提取日期时间处理逻辑

建议将日期时间处理逻辑提取到单独的函数中,提高代码可维护性:

+function processDateHourType(values: string[]) {
+  return {
+    hour: Number(values[3]) && parseInt(values[3]),
+    minute: 0
+  }
+}

if (type === 'date-hour') {
-  hour = Number(values[3]) && parseInt(values[3])
+  const { hour: dateHour } = processDateHourType(values)
+  hour = dateHour
}

466-468: 建议统一时间处理逻辑

建议将所有时间类型的处理逻辑统一到一个地方:

+const TIME_PROCESSORS = {
+  datetime: (values: string[]) => ({
+    hour: Number(values[3]) && parseInt(values[3]),
+    minute: Number(values[4]) && parseInt(values[4])
+  }),
+  'date-hour': (values: string[]) => ({
+    hour: Number(values[3]) && parseInt(values[3]),
+    minute: 0
+  })
+}

-if (props.type === 'date-hour') {
-  hour = Number(values[3]) && parseInt(values[3])
-}
+if (TIME_PROCESSORS[props.type]) {
+  const { hour: processedHour, minute: processedMinute } = TIME_PROCESSORS[props.type](values)
+  hour = processedHour
+  minute = processedMinute
+}
src/uni_modules/wot-design-uni/components/wd-datetime-picker/wd-datetime-picker.vue (1)

748-765: 建议优化列禁用规则的实现。

'date-hour' 类型的列禁用规则实现逻辑正确,但与 'datetime' 类型有重复代码。建议提取共同逻辑以提高代码复用性。

建议重构如下:

 } else if (type === 'date-hour') {
-    const year = boundary[0]
-    const month = boundary[1]
-    const date = boundary[2]
-    const hour = boundary[3]
-
-    if (column.type === 'year') {
-      return isStart ? value > year : value < year
-    }
-    if (column.type === 'month' && currentValue[0] === year) {
-      return isStart ? value > month : value < month
-    }
-    if (column.type === 'date' && currentValue[0] === year && currentValue[1] === month) {
-      return isStart ? value > date : value < date
-    }
-    if (column.type === 'hour' && currentValue[0] === year && currentValue[1] === month && currentValue[2] === date) {
-      return isStart ? value > hour : value < hour
-    }
+    // 复用 datetime 类型的前4个时间组件的禁用规则
+    return columnDisabledRulesForDateComponents(isStart, column, currentValue, boundary, 4)
 }

同时添加辅助函数:

function columnDisabledRulesForDateComponents(
  isStart: boolean,
  column: { type: DatetimePickerViewColumnType },
  currentValue: number[],
  boundary: number[],
  componentCount: number
) {
  const components = ['year', 'month', 'date', 'hour', 'minute']
  const types = components.slice(0, componentCount)
  const index = types.indexOf(column.type)
  
  if (index === -1) return false
  
  // 检查之前的所有组件是否匹配
  const previousMatch = types
    .slice(0, index)
    .every((_, i) => currentValue[i] === boundary[i])
    
  if (index === 0 || previousMatch) {
    return isStart ? value > boundary[index] : value < boundary[index]
  }
  
  return false
}
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between ed60852 and a63b271.

📒 Files selected for processing (5)
  • src/pages/datetimePicker/Index.vue (3 hunks)
  • src/pages/datetimePickerView/Index.vue (3 hunks)
  • src/uni_modules/wot-design-uni/components/wd-datetime-picker-view/types.ts (2 hunks)
  • src/uni_modules/wot-design-uni/components/wd-datetime-picker-view/wd-datetime-picker-view.vue (4 hunks)
  • src/uni_modules/wot-design-uni/components/wd-datetime-picker/wd-datetime-picker.vue (3 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: Redirect rules - wot-design-uni
  • GitHub Check: Header rules - wot-design-uni
  • GitHub Check: Pages changed - wot-design-uni
🔇 Additional comments (3)
src/pages/datetimePickerView/Index.vue (1)

8-10: 新增的年月日时选择器演示代码实现正确!

组件的使用方式和现有的其他时间选择器保持一致,便于用户理解和使用。

src/uni_modules/wot-design-uni/components/wd-datetime-picker/wd-datetime-picker.vue (2)

633-634: LGTM! 类型映射添加正确。

'date-hour' 类型的映射数组包含了正确的时间组件:年、月、日、时。


654-655: LGTM! 日期时间格式化实现正确。

'date-hour' 类型的格式化字符串正确连接了年、月、日和小时。

@luoqiz
Copy link
Author

luoqiz commented Feb 14, 2025

@Moonofweisheng 辛苦尽快把这个分支合并,尽早发布新版本啊

@luoqiz luoqiz closed this Feb 14, 2025
@luoqiz luoqiz reopened this Feb 14, 2025
@Moonofweisheng
Copy link
Owner

Moonofweisheng commented Feb 14, 2025 via email

@luoqiz
Copy link
Author

luoqiz commented Feb 17, 2025

@Moonofweisheng 这个怎么说?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants