1
0

index.tsx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import React from 'react';
  2. import { forwardStatics } from '@douyinfe/semi-foundation/utils/object';
  3. import { numbers, strings } from '@douyinfe/semi-foundation/datePicker/constants';
  4. import DatePicker, { DatePickerProps } from './datePicker';
  5. import ConfigContext from '../configProvider/context';
  6. import LocaleConsumer from '../locale/localeConsumer';
  7. import { Locale } from '../locale/interface';
  8. export type {
  9. BaseValueType,
  10. DayStatusType,
  11. DisabledDateOptions,
  12. DisabledDateType,
  13. DisabledTimeType,
  14. InputSize,
  15. Position,
  16. PresetType,
  17. PresetsType,
  18. TriggerRenderProps,
  19. ValidateStatus,
  20. ValueType,
  21. } from '@douyinfe/semi-foundation/datePicker/foundation';
  22. export type { DateInputProps } from './dateInput';
  23. export type { DatePickerProps } from './datePicker';
  24. export type { MonthProps } from './month';
  25. export type { MonthsGridProps } from './monthsGrid';
  26. export type { QuickControlProps } from './quickControl';
  27. export type { YearAndMonthProps } from './yearAndMonth';
  28. export type { InsetInputProps } from '@douyinfe/semi-foundation/datePicker/inputFoundation';
  29. export type { DatePicker as BaseDatePicker };
  30. export default forwardStatics(
  31. React.forwardRef<DatePicker, DatePickerProps>((props, ref) => {
  32. const propsObj = { ...props };
  33. const { type, format, rangeSeparator } = propsObj;
  34. if (typeof format === 'string' && format) {
  35. if (!/[Hhms]+/.test(format)) {
  36. if (type === 'dateTime') {
  37. propsObj.type = 'date';
  38. } else if (type === 'dateTimeRange') {
  39. propsObj.type = 'dateRange';
  40. }
  41. }
  42. }
  43. // Add spaces at both ends to prevent conflicts with characters in the date when separating
  44. if (rangeSeparator && typeof rangeSeparator === 'string') {
  45. propsObj.rangeSeparator = ` ${rangeSeparator.trim()} `;
  46. }
  47. if (propsObj.insetInput) {
  48. if (!propsObj.position) {
  49. propsObj.position = strings.POSITION_INLINE_INPUT;
  50. }
  51. /**
  52. * When insetInput is `true` and `position` includes `over`, use 1px `spacing` to solve the problem of border-radius leakage in the upper left corner
  53. *
  54. * @see https://user-images.githubusercontent.com/26477537/158817185-126a5f33-41f7-414a-8e36-8d1be2dda5cd.png
  55. */
  56. if (propsObj.position.includes('Over') && !propsObj.spacing) {
  57. propsObj.spacing = numbers.SPACING_INSET_INPUT;
  58. }
  59. }
  60. return (
  61. <ConfigContext.Consumer>
  62. {({ timeZone }: { timeZone?: string | number }) => (
  63. <LocaleConsumer componentName={'DatePicker'}>
  64. {(locale: Locale['DatePicker'], localeCode: string, dateFnsLocale: Locale['dateFnsLocale']) => (
  65. <DatePicker
  66. timeZone={timeZone}
  67. localeCode={localeCode}
  68. locale={locale}
  69. dateFnsLocale={dateFnsLocale}
  70. {...propsObj}
  71. ref={ref}
  72. />
  73. )}
  74. </LocaleConsumer>
  75. )}
  76. </ConfigContext.Consumer>
  77. );
  78. }),
  79. DatePicker
  80. );