Skip to content

Commit

Permalink
feat(datepicker): 优化周选择器高亮判断逻辑性能问题 (#2136)
Browse files Browse the repository at this point in the history
  • Loading branch information
honkinglin authored Apr 11, 2023
1 parent 4381393 commit 87e9d85
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/_common
Submodule _common updated 75 files
+69 −0 docs/miniprogram/_design/Badge.md
+58 −0 docs/miniprogram/_design/Calendar.md
+54 −0 docs/miniprogram/_design/CountDown.md
+51 −0 docs/miniprogram/_design/DateTimePicker.md
+8 −7 docs/miniprogram/_design/Drawer.md
+50 −0 docs/miniprogram/_design/Empty.md
+56 −0 docs/miniprogram/_design/Indexes.md
+78 −0 docs/miniprogram/_design/Input.md
+65 −0 docs/miniprogram/_design/Loading.md
+55 −0 docs/miniprogram/_design/Message.md
+43 −0 docs/miniprogram/_design/Picker.md
+69 −0 docs/miniprogram/_design/Progress.md
+66 −0 docs/miniprogram/_design/Rate.md
+50 −0 docs/miniprogram/_design/Result.md
+67 −0 docs/miniprogram/_design/Search.md
+47 −0 docs/miniprogram/_design/Sidebar.md
+79 −0 docs/miniprogram/_design/Steps.md
+81 −0 docs/miniprogram/_design/Swipecell.md
+41 −0 docs/miniprogram/_design/Switch.md
+63 −0 docs/miniprogram/_design/Textarea.md
+57 −0 docs/miniprogram/_design/Toast.md
+54 −0 docs/miniprogram/_design/Upload.md
+69 −0 docs/miniprogram/_design/avatar.md
+62 −0 docs/miniprogram/_design/cascader.md
+49 −0 docs/miniprogram/_design/link.md
+52 −0 docs/miniprogram/_design/navbar.md
+45 −0 docs/miniprogram/_design/swiper.md
+67 −0 docs/miniprogram/design/avatar.md
+60 −0 docs/miniprogram/design/cascader.md
+1 −1 docs/miniprogram/design/checkbox.md
+1 −1 docs/miniprogram/design/dialog.md
+8 −7 docs/miniprogram/design/drawer.md
+54 −0 docs/miniprogram/design/indexes.md
+47 −0 docs/miniprogram/design/link.md
+50 −0 docs/miniprogram/design/navbar.md
+6 −6 docs/miniprogram/design/pull-down-refresh.md
+43 −0 docs/miniprogram/design/swiper.md
+13 −29 docs/mobile/api/image.md
+2 −28 docs/mobile/api_v2/image.md
+40 −12 docs/mobile/api_v2/input.md
+13 −12 docs/mobile/api_v2/skeleton.md
+1 −1 docs/web/api/breadcrumb.en-US.md
+103 −0 docs/web/api/cascader.en-US.md
+31 −0 docs/web/api/checkbox.en-US.md
+51 −0 docs/web/api/color-picker.en-US.md
+97 −0 docs/web/api/date-picker.en-US.md
+1 −1 docs/web/api/drawer.en-US.md
+1 −1 docs/web/api/dropdown.en-US.md
+97 −0 docs/web/api/form.en-US.md
+61 −0 docs/web/api/input-number.en-US.md
+1 −1 docs/web/api/menu.en-US.md
+2 −1 docs/web/api/message.en-US.md
+2 −1 docs/web/api/notification.en-US.md
+1 −1 docs/web/api/pagination.en-US.md
+2 −1 docs/web/api/popconfirm.en-US.md
+2 −1 docs/web/api/popup.en-US.md
+88 −0 docs/web/api/select-input.en-US.md
+128 −0 docs/web/api/select.en-US.md
+59 −0 docs/web/api/slider.en-US.md
+31 −0 docs/web/api/switch.en-US.md
+60 −0 docs/web/api/tag-input.en-US.md
+31 −0 docs/web/api/textarea.en-US.md
+61 −0 docs/web/api/time-picker.en-US.md
+42 −0 docs/web/api/transfer.en-US.md
+54 −0 docs/web/api/tree-select.en-US.md
+101 −0 docs/web/api/upload.en-US.md
+15 −2 js/date-picker/format.ts
+1 −1 js/input-number/number.ts
+1 −1 package.json
+15 −15 style/mobile/components/image/v2/_index.less
+2 −0 style/mobile/components/input/v2/_index.less
+5 −5 style/mobile/components/loading/_index.less
+50 −2 style/mobile/utilities/_index.less
+0 −4 style/web/components/input-number/_index.less
+1 −1 style/web/components/tabs/_mixin.less
38 changes: 27 additions & 11 deletions src/date-picker/base/Table.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { useMemo } from 'react';
import classNames from 'classnames';
import { useLocaleReceiver } from '../../locale/LocalReceiver';
import useConfig from '../../hooks/useConfig';
Expand Down Expand Up @@ -36,8 +36,7 @@ const DatePickerTable = (props: DatePickerTableProps) => {

const showThead = mode === 'date' || mode === 'week';

// 高亮周区间
const weekRowClass = (value, format, targetValue) => {
const valueYearWeek = useMemo(() => {
if (mode !== 'week' || !value) return {};

if (Array.isArray(value)) {
Expand All @@ -48,13 +47,31 @@ const DatePickerTable = (props: DatePickerTableProps) => {
const endYear = endObj?.year?.();
const endWeek = endObj?.locale?.(local.dayjsLocale)?.week?.();

const targetObj = parseToDayjs(targetValue, format);
const targetYear = targetObj.year();
const targetWeek = targetObj.week();
return { startYear, startWeek, endYear, endWeek };
}

const valueObj = parseToDayjs(value, format).locale(local.dayjsLocale);
return { year: valueObj.year(), week: valueObj.week() };
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [mode, value, format]);

// 高亮周区间
const weekRowClass = (value, targetDayjs) => {
if (mode !== 'week' || !value) return {};

if (Array.isArray(value)) {
if (!value.length) return {};

const targetYear = targetDayjs.year();
const targetWeek = targetDayjs.week();
const isActive =
(targetYear === startYear && targetWeek === startWeek) || (targetYear === endYear && targetWeek === endWeek);
(targetYear === valueYearWeek.startYear && targetWeek === valueYearWeek.startWeek) ||
(targetYear === valueYearWeek.endYear && targetWeek === valueYearWeek.endWeek);
const isRange =
targetYear >= startYear && targetYear <= endYear && targetWeek > startWeek && targetWeek < endWeek;
targetYear >= valueYearWeek.startYear &&
targetYear <= valueYearWeek.endYear &&
targetWeek > valueYearWeek.startWeek &&
targetWeek < valueYearWeek.endWeek;
return {
// 同年同周
[`${classPrefix}-date-picker__table-${mode}-row--active`]: isActive,
Expand All @@ -64,8 +81,7 @@ const DatePickerTable = (props: DatePickerTableProps) => {

return {
[`${classPrefix}-date-picker__table-${mode}-row--active`]:
parseToDayjs(value, format).locale(local.dayjsLocale).week() ===
parseToDayjs(targetValue, format).locale(local.dayjsLocale).week(),
valueYearWeek.year === targetDayjs.year() && valueYearWeek.week === targetDayjs.week(),
};
};

Expand All @@ -86,7 +102,7 @@ const DatePickerTable = (props: DatePickerTableProps) => {
<tr
key={i}
className={classNames(`${classPrefix}-date-picker__table-${mode}-row`, {
...weekRowClass(value, format, row[0].value),
...weekRowClass(value, row[0].dayjsObj),
})}
>
{row.map((col: any, j: number) => (
Expand Down

0 comments on commit 87e9d85

Please sign in to comment.