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(DatePicker): add onFormatLabel #660

Merged
merged 1 commit into from
Mar 24, 2023
Merged

Conversation

wyj580231
Copy link
Collaborator

@github-actions
Copy link
Contributor

github-actions bot commented Mar 24, 2023

🎊 PR Preview d3c1be0 has been successfully built and deployed to https://ant-design-ant-design-mini-preview-pr-660.surge.sh

🕐 Build time: 767.878s

🤖 By surge-preview

onOk="handleRangeOk"
onFormatLabel="handleFormatLabel">
</range-picker>
</list-item>
</list>
Copy link
Contributor

Choose a reason for hiding this comment

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

这段代码实现了一个列表,其中包含两个日期选择器和一个时间范围选择器。下面是一些建议:

  • 看起来这些选择器都是使用某些相同的事件处理程序和功能函数。如果您有能力将它们提取到单独的模块或类中,则可能更好。
  • 在自定义渲染内容的日期选择器中,不显示单位。但没有提供任何有关所省略单位的信息,这可能会使用户感到困惑。最好在文本或工具提示中解释所涉及的单位。
  • 在时间范围选择器后添加了另一个大小相似,但却没有给出任何标准范围的时间范围选择器。为了帮助用户了解如何选择正确的值,最好在列表项旁边的头部中添加一些简要的文字说明。

@@ -27,4 +27,7 @@ Page({
handleDismiss(e) {
console.log('e', e);
},
handleFormatLabel(precision, value) {
return value;
},
});
Copy link
Contributor

Choose a reason for hiding this comment

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

这段代码是个小程序(微信小程序或者支付宝小程序)的一个页面逻辑,主要包括一个handleDismiss函数和一个handleFormatLabel函数。这里面有一个问题:这两个函数都没有对参数进行使用,可能需要检查一下是否有必须传入的参数被漏掉了。

另外,这里的handleFormatLabel函数看起来不太完成,因为直接返回了传入的value,建议检查一遍功能实现是否符合预期。

最后,这段代码较短,看起来还比较简单,目前并没有发现潜在的错误风险。

second: '秒',
};
return `${value}${suffixMap[precision]}`;
},
defaultFormat(date, valueStrs) {
const { format, splitCharacter } = this.props;
if (format && valueStrs && valueStrs[0] && valueStrs[1]) {
Copy link
Contributor

Choose a reason for hiding this comment

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

这段代码包含了一个组件的实现。在调用 getRangeData 函数时,增加了一个参数和回调函数,来接收和处理一个新值。该函数也包括了一个新的方法 onFormatLabel,它将默认的日期 label 转换为一个可配置的字符串。建议做以下改进:

  • 变量声明应该使用 const 和 let 关键字,以便更好地管理变量生命周期。
  • 使用解构器可以减少对 this.props 的引用,从而使代码更简洁。
  • 无需检查 undefined 或 null 值的条件语句会增加可读性。

如果你还没有正确的定义组件的 props 验证规则,那么建议您添加验证规则,以确保 props 的类型正确,避免潜在的 bug 隐患。

onFormatLabel?(
precision: 'year' | 'month' | 'day' | 'hour' | 'minute' | 'second',
value: number
);
}

export const DateRangePickerDefaultProps: Partial<IDateRangePickerProps> = {
Copy link
Contributor

Choose a reason for hiding this comment

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

这段代码是一个日期范围选择器组件的接口定义。以下是我的代码审查意见:

建议:

  • popStyle字段缺乏说明文档,应该提供更详细的描述。
  • onFormatLabel函数需要更详细的说明,例如返回类型和使用方式等。

风险:

  • 没有发现明显的致命错误或潜在风险。

改进方向:

  • 可以添加更多的参数的配置选项,例如初始日期值、日期格式化选项等。
  • 可以考虑支持国际化和本地化。

请注意,以上只是基于代码片段和语境的一般性意见,具体的优化策略还需要深入研究整个项目的需求和架构。

| onFormat | 选中值的文本显示格式 | (date: [Date,Date], dateStr: [string,string]) => string | - |
| onFormatLabel | 自定义每列展示的内容,默认添加年、月、日、时、分、秒单位 | (precision: `year` \| `month` \| `day` \| `hour` \| `minute`, value: number) => string | - |
| onVisibleChange | 弹出框显示/隐藏状态变化触发 | (visible: boolean, (event: [Event](https://opendocs.alipay.com/mini/framework/event-object)) => void | - | No newline at end of file
Copy link
Contributor

Choose a reason for hiding this comment

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

这段代码是一个组件的属性列表。我的理解是,这些属性是用于定义DatePicker和RangePicker组件的行为的。

在DatePicker属性列表中,我注意到onFormatLabel属性已经被删除了。其他属性似乎都没有问题。在RangePicker属性列表中,除了删除了onFormatLabel之外,其他属性也看起来都没有问题。

关于改进建议,我认为可以通过添加一些额外的配置项或参数来增加组件的可定制性。例如,可以添加一个用于设置选取日期范围的参数,或者允许用户自定义日期格式的参数,以及其他类似的选项。此外,应该对组件进行更全面的测试,以确保其功能正确性和稳定性。

second: '秒',
};
return `${value}${suffixMap[precision]}`;
},
onChange(selectedIndex) {
selectedIndex = getValidValue(selectedIndex);
const { onPickerChange, format, precision } = this.props;
Copy link
Contributor

Choose a reason for hiding this comment

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

这段代码是一个React组件的部分,作用是渲染一个日期选择器。下面是我的建议:

  1. 在getRangeData函数中,增加了onFormatLabel函数作为新参数。onFormatLabel旨在自定义标签格式化。这种更改很好,因为它增加了程序的可扩展性。

  2. 建议传递precision和value参数到onFormatLabel函数,在函数内设置格式时使用它们。此更改从技术上讲不必要,但它使得代码更健壮并且对未来的修改更具抵御力。

  3. defaultFormatLabel函数的目的是为每个标签添加后缀(例如“年”,“月”等)。建议将这些后缀作为常量放入代码的顶部。还可以考虑从props获取后缀而不是硬编码名称。

  4. 没有看到完整的代码,但是应确保传递给onChange的参数经过验证,以防止错误发生。

onFormatLabel?(
precision: 'year' | 'month' | 'day' | 'hour' | 'minute' | 'second',
value: number
);
}

export const DatePickerDefaultProps: IDatePickerProps = {
Copy link
Contributor

Choose a reason for hiding this comment

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

这段代码是一个日期选择器组件,主要包括以下几个方面的内容:

  1. props 接口定义了一些必需的和可选的属性,其中包括日期、时间、默认值等;
  2. 默认 props 定义了一些默认值;
  3. popStyle 属性用于指定弹出框样式;
  4. onFormatLabel 属性是一个回调函数,用于自定义每列展示的内容;
  5. 这里没有看到太大的 bug 风险,但可以考虑通过添加 TypeScript 类型来提高代码的类型安全性,在注释中对每个属性作更详细的说明。

可能需要改进的地方是:

  1. 更好的命名方式,有助于更好地理解代码。比如 onFormatLabel 可以改成 formatLabelCallback
  2. 在注释中对于 value 参数也进行更具体的类型描述,并且如果这个方法最好顺便返回一个字符串或节点,让自定义的内容更灵活;
  3. 考虑是否需要对每个属性的默认值做出更多的修改。

以上只是一些简单的意见,具体实现取决于具体情况。

@codecov
Copy link

codecov bot commented Mar 24, 2023

Codecov Report

Patch coverage has no change and project coverage change: -0.02 ⚠️

Comparison is base (ac05d0f) 15.16% compared to head (d3c1be0) 15.14%.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #660      +/-   ##
==========================================
- Coverage   15.16%   15.14%   -0.02%     
==========================================
  Files         158      158              
  Lines        3251     3281      +30     
  Branches      850      861      +11     
==========================================
+ Hits          493      497       +4     
- Misses       2408     2429      +21     
- Partials      350      355       +5     
Impacted Files Coverage Δ
src/DatePicker/RangePicker/index.ts 0.00% <0.00%> (ø)
src/DatePicker/RangePicker/props.ts 0.00% <ø> (ø)
src/DatePicker/index.ts 0.00% <0.00%> (ø)
src/DatePicker/props.ts 0.00% <ø> (ø)
src/DatePicker/util.ts 0.00% <0.00%> (ø)
src/Form/FormDatePicker/index.ts 0.00% <0.00%> (ø)
src/Form/FormRangePicker/index.ts 0.00% <0.00%> (ø)

... and 1 file with indirect coverage changes

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

@wyj580231 wyj580231 force-pushed the feat/DatePicker_0324 branch from dcbf26d to 34d4c99 Compare March 24, 2023 04:51
onOk="handleRangeOk"
onFormatLabel="handleFormatLabel">
</range-picker>
</list-item>
</list>
Copy link
Contributor

Choose a reason for hiding this comment

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

这段代码看起来是一个时间选择器的实现,其中新增了一个“自定义每列的渲染内容”的选项,可以让用户在日期选择器中选择不显示单位。另外,还有一个增加了一个“每列不显示单位”的选项到时间范围选择器中。

从安全性和稳定性的角度来看,这段代码看起来没有显著的问题或风险。但是如果需要优化代码,可能需要对UI设计进行重新考虑,以提高用户交互的可用性。

@@ -27,4 +27,7 @@ Page({
handleDismiss(e) {
console.log('e', e);
},
handleFormatLabel(type, value) {
return String(value);
},
});
Copy link
Contributor

Choose a reason for hiding this comment

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

这段代码看起来非常短小,所以没有很大的潜在风险。下面是一些改进或建议的方式:

  1. 在handleFormatLabel()函数中,我们只是简单地将输入值转换为字符串并返回它。如果这是您想要实现的唯一目的,那么这段代码已经足够好了。但如果需要更多的格式化逻辑,您可能需要重命名handleFormatLabel()为更准确反映其用途的名称。

  2. 另外,需要注意变量和参数的命名方式。type和value可能不太能说明它们的具体含义,更好的实践是使用更明确的名称,使代码更易于读取和理解。

总而言之,这段代码非常简短,看起来没有明显的错误或潜在风险。如果您需要更详细的代码审查,应该查看调用这些函数的上下文,并根据运行时环境和应用程序的目标考虑最佳做法。

second: '秒',
};
return `${value}${suffixMap[type]}`;
},
defaultFormat(date, valueStrs) {
const { format, splitCharacter } = this.props;
if (format && valueStrs && valueStrs[0] && valueStrs[1]) {
Copy link
Contributor

Choose a reason for hiding this comment

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

这段代码针对一个组件实现了getRangeData方法和onFormatLabel方法的修改以及添加。getRangeData方法接收precision、min、max、currentPickerDay四个参数,并调用this.onFormatLabel方法,这个方法会在pick event之后被调用,具体返回值会被传回到picker view data中。

onFormatLabel方法由于需要自定义生成label格式,所以通过设置props上的onFormatLabel函数来实现,如果存在该回调,则先调用一次该回调来允许props层面上的用户自定义label格式,否则使用defaultFormatLabel中预设的格式输出。

此外,还有一个defaultFormat(date, valueStrs)方法也被实现,它需要format、splitCharacter两个props。但是目前这个组件并没有包含defaultFormat方法的调用。

优化建议:

  • 调用getRangeData时,如果得到的结果没有变化,应该只更新视图而不重新生成数据;
  • 考虑将defaultFormat方法与组件配置相关的一些逻辑封装在组件内部,使组件更加可复用,可以移植性更强。

onFormatLabel?(
type: 'year' | 'month' | 'day' | 'hour' | 'minute' | 'second',
value: number
): string;
}

export const DateRangePickerDefaultProps: Partial<IDateRangePickerProps> = {
Copy link
Contributor

Choose a reason for hiding this comment

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

代码补丁中增加了 onFormatLabel 函数的定义,用于自定义每列展示的内容。建议在注释中添加函数的用途和返回值的格式注释。同时在代码实现上要确保对参数进行类型检查并处理可能的边界问题,如 value 参数超出有效范围等。另外,建议在代码中保证命名一致性,如使用 popStyle 时应当与其他属性采用相同的命名方式。

| onFormat | 选中值的文本显示格式 | (date: [Date,Date], dateStr: [string,string]) => string | - |
| onFormatLabel | 自定义每列展示的内容,默认添加年、月、日、时、分、秒单位 | (type: `year` \| `month` \| `day` \| `hour` \| `minute`, value: number) => string | - |
| onVisibleChange | 弹出框显示/隐藏状态变化触发 | (visible: boolean, (event: [Event](https://opendocs.alipay.com/mini/framework/event-object)) => void | - | No newline at end of file
Copy link
Contributor

Choose a reason for hiding this comment

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

这段代码是对RangePicker和DatePicker组件的属性进行了修改和扩展。

在RangePicker组件中新增了onFormatLabel属性,用于自定义每列展示的内容,并且默认添加了年、月、日、时、分单位。这一改动可以提供更加灵活的选择器展示模式。

另外,所有回调函数(onOk、onCancel、onPickerChange、onFormat、onVisibleChange)都添加了默认值“-”,这样做有助于代码的可读性和维护性。

总体来说,这段代码没有明显的风险和缺陷,并且扩展了组件的功能,提高了其灵活性和易用性。建议保留这些改动。

second: '秒',
};
return `${value}${suffixMap[type]}`;
},
onChange(selectedIndex) {
selectedIndex = getValidValue(selectedIndex);
const { onPickerChange, format, precision } = this.props;
Copy link
Contributor

Choose a reason for hiding this comment

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

这段代码是 React 组件中的一部分,其中包含了一个函数 getRangeData 和两个回调函数 onFormatLabel 和 defaultFormatLabel。

  1. 在 getRangeData 函数的调用中增加了一个新的参数,这个参数是 onFormatLabel 回调函数的参照对象。
  2. onFormatLabel 函数用于对选择器的值进行格式化,可以通过 props 属性传入自定义的格式化函数。如果没有传入自定义函数,则默认使用 defaultFormatLabel 函数进行格式化。
  3. defaultFormatLabel 函数是 onFormatLabel 函数的备选方案,用于在没有传入自定义格式化函数时提供一个默认的格式化方法。
  4. 在 onChange 回调函数中,通过调用 getValidValue 函数对 selectedIndex 进行了处理。

改进建议:

  1. 建议增加注释,以便后期维护和修改。
  2. 如果组件更复杂,建议使用 propTypes 或者 TypeScript 等方式进行类型检查,减少类型错误风险。
  3. 可以考虑使用函数式组件或 hooks API 进行重构,使组件更简洁易读、易维护。
  4. 建议在组件内部增加容错能力,例如对于缺失或错误的 props 值进行判断和提示,减少潜在的错误风险。

onFormatLabel?(
type: 'year' | 'month' | 'day' | 'hour' | 'minute' | 'second',
value: number
): string;
}

export const DatePickerDefaultProps: IDatePickerProps = {
Copy link
Contributor

Choose a reason for hiding this comment

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

这段代码是一个React组件的接口定义和默认props。根据代码来看,没有明显的bug风险。以下是一些可能的改进建议:

  1. 添加类型检查:目前这段代码没有类型检查。添加类型检查可以帮助在编译阶段发现潜在的问题,提高代码质量。

  2. 更具描述性的变量名:例如popStyle、IDatePickerProps等变量命名不够清晰或明确。更具描述性的变量名可以使代码易于理解。

  3. 子组件化:如果日期选择器很复杂,则可以考虑将其拆分为几个子组件以提高可复用性并降低代码复杂度。

对于新添加的onFormatLabel方法,需要对调用它的地方进行修改以实现其功能。另外,建议给该方法添加一些详细的注释,以增加代码的可读性。

@wyj580231 wyj580231 force-pushed the feat/DatePicker_0324 branch from 34d4c99 to d3c1be0 Compare March 24, 2023 08:28
onOk="handleRangeOk"
onFormatLabel="handleFormatLabel">
</range-picker>
</list-item>
</list>
Copy link
Contributor

Choose a reason for hiding this comment

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

这是一个简短的前端代码片段。以下是我对这段代码的一些评论和建议:

  • 在第 2 个列表项中,使用了一个新组件 <date-picker> 。请确保该组件已经完全测试,包括在不同浏览器、设备和网络条件下。
  • 对于第 5 个列表项中新增的 <range-picker>组件,请也进行充分的测试。
  • 使用onFormatLabel方法调整范围选择器中显示的日期格式会很好,以便使其更符合用户需求。
  • 如果可以的话,建议将选择器中的文案标准化为语言资源的形式,以便在从多个应用程序中共享和重用。

希望这些建议对您有所帮助!

@@ -27,4 +27,7 @@ Page({
handleDismiss(e) {
console.log('e', e);
},
handleFormatLabel(type, value) {
return String(value);
},
});
Copy link
Contributor

Choose a reason for hiding this comment

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

该代码看上去非常简单,仅有一个函数 handleFormatLabel。此函数接受两个参数,一个类型和一个值,并返回该值的字符串表示形式。

修改似乎对代码并没有太大影响,因为只是新增了一种处理方式但没有从原来的函数移除任何逻辑或方法。但是无法确保是否会对其他依赖当前代码的部分产生潜在风险,需要更多的上下文才能确定。

将来可能需要添加更多的注释来解释代码功能、用途、输入输出等等。

另外,需要注意使用良好的命名规范来表明变量用途以及函数语义清晰易读性强。

second: '秒',
};
return `${value}${suffixMap[type]}`;
},
defaultFormat(date, valueStrs) {
const { format, splitCharacter } = this.props;
if (format && valueStrs && valueStrs[0] && valueStrs[1]) {
Copy link
Contributor

Choose a reason for hiding this comment

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

本代码补丁主要实现了日期选择器的一些功能,其中包括用户更改日期、格式化时间标签等。以下是对该代码片段的简短评估:

  • 通过getRangeData()函数获取了需要呈现在日期选择器中的日期范围。
  • 添加了onFormatLabel()函数,用于自定义时间标签的格式化,但是如果未使用该函数,则默认使用defaultFormatLabel()函数。
  • defaultFormat()函数用于将所选日期和时间格式化为指定文本格式,因此该方法几乎不需要任何修改。

由于无法得知组件的完整实现细节,因此我们难以确定是否存在任何潜在风险或改进建议。

onFormatLabel?(
type: 'year' | 'month' | 'day' | 'hour' | 'minute' | 'second',
value: number
): string;
}

export const DateRangePickerDefaultProps: Partial<IDateRangePickerProps> = {
Copy link
Contributor

Choose a reason for hiding this comment

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

这段代码是对一个名为IDateRangePickerProps的接口进行了修改和添加属性。 新增加的属性onFormatLabel可以自定义每个日期选择器的展示内容,原有的popStyle属性可以设置弹出框的样式。

建议优化:需要进一步检查代码的实际使用情况和具体环境,针对实际需求做出调整,例如:是否需要封装成组件等等。此外,还需要确保在调用过程中类型和值传递正确,否则可能会导致程序错误。

@jc9702507 jc9702507 merged commit 24f6fe2 into master Mar 24, 2023
@github-actions github-actions bot mentioned this pull request Mar 24, 2023
@DiamondYuan DiamondYuan deleted the feat/DatePicker_0324 branch October 27, 2023 06:39
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.

3 participants