getInsetInputFormatToken.ts 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import { strings } from '../constants';
  2. import { getDefaultFormatTokenByType } from './getDefaultFormatToken';
  3. /**
  4. * 获取 insetInput 输入框的 placeholder
  5. * Get the placeholder of the insetInput input
  6. *
  7. * If type is time related, we only recognize the format like `dateFormat timeFormat`
  8. * - 'yyyy-MM-dd HH:mm:ss' // ok
  9. * - 'yyyy-MM-dd HH:mm:ss' // bad format
  10. *
  11. * @example
  12. * 'yyyy-MM-dd' => 'yyyy-MM-dd'
  13. * 'yyyy-MM' => 'yyyy-MM'
  14. * 'yyyy-MM-dd HH:mm:ss' => 'yyyy-MM-dd HH:mm:ss'
  15. * 'yyyy-MM-dd HH:mm' => 'yyyy-MM-dd HH:mm'
  16. * 'Pp' => 'yyyy-MM-dd'
  17. */
  18. export default function getInsetInputFormatToken(options: { format: string; type: typeof strings.TYPE_SET[number] }) {
  19. const { format, type } = options;
  20. const dateReg = /([yMd]{0,4}[^a-z\s]*[yMd]{0,4}[^a-z\s]*[yMd]{0,4})/i;
  21. const dateTimeReg = /([yMd]{0,4}[^a-z\s]*[yMd]{0,4}[^a-z\s]*[yMd]{0,4}) (H{0,2}[^a-z\s]*m{0,2}[^a-z\s]*s{0,2})/i;
  22. const defaultToken = getDefaultFormatTokenByType(type);
  23. let insetInputFormat: string;
  24. switch (type) {
  25. case 'dateTime':
  26. case 'dateTimeRange':
  27. const dateTimeResult = dateTimeReg.exec(format);
  28. insetInputFormat = (dateTimeResult && dateTimeResult[1] && dateTimeResult[2]) ? `${dateTimeResult[1]} ${dateTimeResult[2]}` : defaultToken;
  29. break;
  30. case 'date':
  31. case 'month':
  32. case 'monthRange':
  33. case 'dateRange':
  34. default:
  35. const dateResult = dateReg.exec(format);
  36. insetInputFormat = dateResult && dateResult[1] || defaultToken;
  37. break;
  38. }
  39. return insetInputFormat;
  40. }