index.tsx 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 default forwardStatics(
  29. React.forwardRef<DatePicker, DatePickerProps>((props, ref) => {
  30. const propsObj = { ...props };
  31. const { type, format, rangeSeparator } = propsObj;
  32. if (typeof format === 'string' && format) {
  33. if (!/[Hhms]+/.test(format)) {
  34. if (type === 'dateTime') {
  35. propsObj.type = 'date';
  36. } else if (type === 'dateTimeRange') {
  37. propsObj.type = 'dateRange';
  38. }
  39. }
  40. }
  41. // Add spaces at both ends to prevent conflicts with characters in the date when separating
  42. if (rangeSeparator && typeof rangeSeparator === 'string') {
  43. propsObj.rangeSeparator = ` ${rangeSeparator.trim()} `;
  44. }
  45. if (propsObj.insetInput) {
  46. if (!propsObj.position) {
  47. propsObj.position = strings.POSITION_INLINE_INPUT;
  48. }
  49. /**
  50. * When insetInput is `true` and `position` includes `over`, use 1px `spacing` to solve the problem of border-radius leakage in the upper left corner
  51. *
  52. * @see https://user-images.githubusercontent.com/26477537/158817185-126a5f33-41f7-414a-8e36-8d1be2dda5cd.png
  53. */
  54. if (propsObj.position.includes('Over') && !propsObj.spacing) {
  55. propsObj.spacing = numbers.SPACING_INSET_INPUT;
  56. }
  57. }
  58. return (
  59. <ConfigContext.Consumer>
  60. {({ timeZone }: { timeZone?: string | number }) => (
  61. <LocaleConsumer componentName={'DatePicker'}>
  62. {(locale: Locale['DatePicker'], localeCode: string, dateFnsLocale: Locale['dateFnsLocale']) => (
  63. <DatePicker
  64. timeZone={timeZone}
  65. localeCode={localeCode}
  66. locale={locale}
  67. dateFnsLocale={dateFnsLocale}
  68. {...propsObj}
  69. ref={ref}
  70. />
  71. )}
  72. </LocaleConsumer>
  73. )}
  74. </ConfigContext.Consumer>
  75. );
  76. }),
  77. DatePicker
  78. );