chore: refactor dayjs import (#1065)
Reviewed-on: daoyoucloud/tachybase#1065
This commit is contained in:
		
							parent
							
								
									90c4a66889
								
							
						
					
					
						commit
						8ba025066f
					
				@ -1,82 +0,0 @@
 | 
			
		||||
import { ArrayField } from '@tachybase/schema';
 | 
			
		||||
import { useField } from '@tachybase/schema';
 | 
			
		||||
import { error } from '@tachybase/utils/client';
 | 
			
		||||
import React from 'react';
 | 
			
		||||
import { SchemaComponentOptions } from '..';
 | 
			
		||||
import { useAPIClient, useRequest } from '../api-client';
 | 
			
		||||
 | 
			
		||||
const useChinaRegionDataSource = (options) => {
 | 
			
		||||
  const field = useField<ArrayField>();
 | 
			
		||||
  const maxLevel = field.componentProps.maxLevel;
 | 
			
		||||
  return useRequest(
 | 
			
		||||
    {
 | 
			
		||||
      resource: 'chinaRegions',
 | 
			
		||||
      action: 'list',
 | 
			
		||||
      params: {
 | 
			
		||||
        sort: 'code',
 | 
			
		||||
        paginate: false,
 | 
			
		||||
        filter: {
 | 
			
		||||
          level: 1,
 | 
			
		||||
        },
 | 
			
		||||
      },
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
      ...options,
 | 
			
		||||
      onSuccess(data) {
 | 
			
		||||
        options?.onSuccess({
 | 
			
		||||
          data:
 | 
			
		||||
            data?.data?.map((item) => {
 | 
			
		||||
              if (maxLevel !== 1) {
 | 
			
		||||
                item.isLeaf = false;
 | 
			
		||||
              }
 | 
			
		||||
              return item;
 | 
			
		||||
            }) || [],
 | 
			
		||||
        });
 | 
			
		||||
      },
 | 
			
		||||
    },
 | 
			
		||||
  );
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
const useChinaRegionLoadData = () => {
 | 
			
		||||
  const api = useAPIClient();
 | 
			
		||||
  const field = useField<ArrayField>();
 | 
			
		||||
  const maxLevel = field.componentProps.maxLevel;
 | 
			
		||||
  return (selectedOptions) => {
 | 
			
		||||
    const targetOption = selectedOptions[selectedOptions.length - 1];
 | 
			
		||||
    if (targetOption?.children?.length > 0) {
 | 
			
		||||
      return;
 | 
			
		||||
    }
 | 
			
		||||
    targetOption.loading = true;
 | 
			
		||||
    api
 | 
			
		||||
      .resource('chinaRegions')
 | 
			
		||||
      .list({
 | 
			
		||||
        sort: 'code',
 | 
			
		||||
        paginate: false,
 | 
			
		||||
        filter: {
 | 
			
		||||
          parentCode: targetOption.code,
 | 
			
		||||
        },
 | 
			
		||||
      })
 | 
			
		||||
      .then(({ data }) => {
 | 
			
		||||
        targetOption.loading = false;
 | 
			
		||||
        targetOption.children =
 | 
			
		||||
          data?.data?.map((item) => {
 | 
			
		||||
            if (maxLevel > item.level) {
 | 
			
		||||
              item.isLeaf = false;
 | 
			
		||||
            }
 | 
			
		||||
            return item;
 | 
			
		||||
          }) || [];
 | 
			
		||||
        field.dataSource = [...field.dataSource];
 | 
			
		||||
      })
 | 
			
		||||
      .catch((err) => {
 | 
			
		||||
        error(err);
 | 
			
		||||
      });
 | 
			
		||||
  };
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
export const ChinaRegionProvider = (props) => {
 | 
			
		||||
  return (
 | 
			
		||||
    <SchemaComponentOptions scope={{ useChinaRegionDataSource, useChinaRegionLoadData }}>
 | 
			
		||||
      {props.children}
 | 
			
		||||
    </SchemaComponentOptions>
 | 
			
		||||
  );
 | 
			
		||||
};
 | 
			
		||||
@ -1,12 +1,3 @@
 | 
			
		||||
// 解决 build 报 dayjs 相关类型错误的问题
 | 
			
		||||
import 'dayjs/plugin/isBetween';
 | 
			
		||||
import 'dayjs/plugin/isSameOrAfter';
 | 
			
		||||
import 'dayjs/plugin/isSameOrBefore';
 | 
			
		||||
import 'dayjs/plugin/isoWeek';
 | 
			
		||||
import 'dayjs/plugin/localeData';
 | 
			
		||||
import 'dayjs/plugin/quarterOfYear';
 | 
			
		||||
import 'dayjs/plugin/utc';
 | 
			
		||||
import 'dayjs/plugin/weekday';
 | 
			
		||||
// 重置浏览器样式
 | 
			
		||||
import 'antd/dist/reset.css';
 | 
			
		||||
import './global.less';
 | 
			
		||||
@ -19,7 +10,6 @@ export * from './appInfo';
 | 
			
		||||
export * from './application';
 | 
			
		||||
export * from './async-data-provider';
 | 
			
		||||
export * from './block-provider';
 | 
			
		||||
export * from './china-region';
 | 
			
		||||
export * from './collection-manager';
 | 
			
		||||
 | 
			
		||||
export * from './common';
 | 
			
		||||
 | 
			
		||||
@ -1,30 +1,14 @@
 | 
			
		||||
import React from 'react';
 | 
			
		||||
import * as components from '.';
 | 
			
		||||
import { Plugin } from '../../application/Plugin';
 | 
			
		||||
import * as common from '../common';
 | 
			
		||||
import { SchemaComponentOptions } from '../core';
 | 
			
		||||
import { useFilterActionProps } from './filter/useFilterActionProps';
 | 
			
		||||
import { requestChartData } from './g2plot/requestChartData';
 | 
			
		||||
 | 
			
		||||
import { actionSettings } from './action';
 | 
			
		||||
import { useFilterActionProps } from './filter/useFilterActionProps';
 | 
			
		||||
import { formV1Settings } from './form';
 | 
			
		||||
import { filterFormItemSettings, formItemSettings } from './form-item';
 | 
			
		||||
import { formDetailsSettings, formSettings, readPrettyFormSettings } from './form-v2';
 | 
			
		||||
import { requestChartData } from './g2plot/requestChartData';
 | 
			
		||||
import { pageSettings, pageTabSettings } from './page';
 | 
			
		||||
 | 
			
		||||
// TODO: delete this, replaced by `AntdSchemaComponentPlugin`
 | 
			
		||||
export const AntdSchemaComponentProvider = (props) => {
 | 
			
		||||
  const { children } = props;
 | 
			
		||||
  return (
 | 
			
		||||
    <SchemaComponentOptions
 | 
			
		||||
      scope={{ requestChartData, useFilterActionProps }}
 | 
			
		||||
      components={{ ...components, ...common } as any}
 | 
			
		||||
    >
 | 
			
		||||
      {children}
 | 
			
		||||
    </SchemaComponentOptions>
 | 
			
		||||
  );
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
export class AntdSchemaComponentPlugin extends Plugin {
 | 
			
		||||
  async load() {
 | 
			
		||||
    this.addComponents();
 | 
			
		||||
 | 
			
		||||
@ -1,5 +1,4 @@
 | 
			
		||||
import { dayjs, getDefaultFormat, str2moment, toGmt, toLocal } from '@tachybase/utils/client';
 | 
			
		||||
import type { Dayjs } from 'dayjs';
 | 
			
		||||
import { dayjs, getDefaultFormat, str2moment, toGmt, toLocal, type Dayjs } from '@tachybase/utils/client';
 | 
			
		||||
 | 
			
		||||
const toStringByPicker = (value, picker, timezone: 'gmt' | 'local') => {
 | 
			
		||||
  if (!dayjs.isDayjs(value)) return value;
 | 
			
		||||
 | 
			
		||||
@ -1,6 +1,4 @@
 | 
			
		||||
import { getDefaultFormat, str2moment, toGmt, toLocal } from '@tachybase/utils/client';
 | 
			
		||||
import type { Dayjs } from 'dayjs';
 | 
			
		||||
import dayjs from 'dayjs';
 | 
			
		||||
import { dayjs, getDefaultFormat, str2moment, toGmt, toLocal, type Dayjs } from '@tachybase/utils/client';
 | 
			
		||||
 | 
			
		||||
const toStringByPicker = (value, picker, timezone: 'gmt' | 'local') => {
 | 
			
		||||
  if (!dayjs.isDayjs(value)) return value;
 | 
			
		||||
 | 
			
		||||
@ -1,3 +1,5 @@
 | 
			
		||||
import './index.less';
 | 
			
		||||
 | 
			
		||||
export * from './AntdSchemaComponentProvider';
 | 
			
		||||
export { genStyleHook } from './__builtins__';
 | 
			
		||||
export * from './action';
 | 
			
		||||
@ -54,5 +56,3 @@ export * from './custom-cascader';
 | 
			
		||||
export * from './unixTimestamp';
 | 
			
		||||
export * from './nanoIDInput';
 | 
			
		||||
export { PageHeader } from '@ant-design/pro-layout';
 | 
			
		||||
 | 
			
		||||
import './index.less';
 | 
			
		||||
 | 
			
		||||
@ -1,6 +1,6 @@
 | 
			
		||||
import lodash from 'lodash';
 | 
			
		||||
 | 
			
		||||
import { dayjs } from './dayjs';
 | 
			
		||||
import { dayjs, type Dayjs } from './dayjs';
 | 
			
		||||
 | 
			
		||||
export * from './collections-graph';
 | 
			
		||||
export * from './common';
 | 
			
		||||
@ -18,6 +18,6 @@ export * from './registry';
 | 
			
		||||
export * from './isPortalInBody';
 | 
			
		||||
export * from './uid';
 | 
			
		||||
export * from './url';
 | 
			
		||||
export { dayjs, lodash };
 | 
			
		||||
export { dayjs, lodash, Dayjs };
 | 
			
		||||
export * from './parseHTML';
 | 
			
		||||
export * from './dom';
 | 
			
		||||
 | 
			
		||||
@ -1,18 +1,28 @@
 | 
			
		||||
import dayjs from 'dayjs';
 | 
			
		||||
import dayjs, { type Dayjs } from 'dayjs';
 | 
			
		||||
import advancedFormat from 'dayjs/plugin/advancedFormat';
 | 
			
		||||
import calendar from 'dayjs/plugin/calendar';
 | 
			
		||||
import customParseFormat from 'dayjs/plugin/customParseFormat';
 | 
			
		||||
import IsBetween from 'dayjs/plugin/isBetween';
 | 
			
		||||
import isoWeek from 'dayjs/plugin/isoWeek';
 | 
			
		||||
import IsSameOrAfter from 'dayjs/plugin/isSameOrAfter';
 | 
			
		||||
import isSameOrBefore from 'dayjs/plugin/isSameOrBefore';
 | 
			
		||||
import isoWeek from 'dayjs/plugin/isoWeek';
 | 
			
		||||
import localeData from 'dayjs/plugin/localeData';
 | 
			
		||||
import quarterOfYear from 'dayjs/plugin/quarterOfYear';
 | 
			
		||||
import tz from 'dayjs/plugin/timezone';
 | 
			
		||||
import utc from 'dayjs/plugin/utc';
 | 
			
		||||
import weekday from 'dayjs/plugin/weekday';
 | 
			
		||||
import weekOfYear from 'dayjs/plugin/weekOfYear';
 | 
			
		||||
import weekYear from 'dayjs/plugin/weekYear';
 | 
			
		||||
import weekday from 'dayjs/plugin/weekday';
 | 
			
		||||
import calendar from 'dayjs/plugin/calendar';
 | 
			
		||||
 | 
			
		||||
// 解决 build 报 dayjs 相关类型错误的问题
 | 
			
		||||
import 'dayjs/plugin/isBetween';
 | 
			
		||||
import 'dayjs/plugin/isSameOrAfter';
 | 
			
		||||
import 'dayjs/plugin/isSameOrBefore';
 | 
			
		||||
import 'dayjs/plugin/isoWeek';
 | 
			
		||||
import 'dayjs/plugin/localeData';
 | 
			
		||||
import 'dayjs/plugin/quarterOfYear';
 | 
			
		||||
import 'dayjs/plugin/utc';
 | 
			
		||||
import 'dayjs/plugin/weekday';
 | 
			
		||||
 | 
			
		||||
dayjs.extend(weekday);
 | 
			
		||||
dayjs.extend(localeData);
 | 
			
		||||
@ -29,4 +39,4 @@ dayjs.extend(customParseFormat);
 | 
			
		||||
dayjs.extend(advancedFormat);
 | 
			
		||||
dayjs.extend(calendar);
 | 
			
		||||
 | 
			
		||||
export { dayjs };
 | 
			
		||||
export { dayjs, Dayjs };
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user