helper.ts 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import type { ValidationRule } from 'ant-design-vue/lib/form/Form';
  2. import type { ComponentType } from './types/index';
  3. import { useI18n } from '/@/hooks/web/useI18n';
  4. import { dateUtil } from '/@/utils/dateUtil';
  5. import { isNumber, isObject } from '/@/utils/is';
  6. const { t } = useI18n();
  7. /**
  8. * @description: 生成placeholder
  9. */
  10. export function createPlaceholderMessage(component: ComponentType) {
  11. if (component.includes('Input') || component.includes('Complete')) {
  12. return t('common.inputText');
  13. }
  14. if (component.includes('Picker')) {
  15. return t('common.chooseText');
  16. }
  17. if (
  18. component.includes('Select') ||
  19. component.includes('Cascader') ||
  20. component.includes('Checkbox') ||
  21. component.includes('Radio') ||
  22. component.includes('Switch')
  23. ) {
  24. // return `请选择${label}`;
  25. return t('common.chooseText');
  26. }
  27. return '';
  28. }
  29. const DATE_TYPE = ['DatePicker', 'MonthPicker', 'WeekPicker', 'TimePicker'];
  30. function genType() {
  31. return [...DATE_TYPE, 'RangePicker'];
  32. }
  33. export function setComponentRuleType(rule: ValidationRule, component: ComponentType, valueFormat: string) {
  34. if (['DatePicker', 'MonthPicker', 'WeekPicker', 'TimePicker'].includes(component)) {
  35. rule.type = valueFormat ? 'string' : 'object';
  36. } else if (['RangePicker', 'Upload', 'CheckboxGroup', 'TimePicker'].includes(component)) {
  37. rule.type = 'array';
  38. } else if (['InputNumber'].includes(component)) {
  39. rule.type = 'number';
  40. }
  41. }
  42. export function processDateValue(attr: Recordable, component: string) {
  43. const { valueFormat, value } = attr;
  44. if (valueFormat) {
  45. attr.value = isObject(value) ? dateUtil(value).format(valueFormat) : value;
  46. } else if (DATE_TYPE.includes(component) && value) {
  47. attr.value = dateUtil(attr.value);
  48. }
  49. }
  50. export function handleInputNumberValue(component?: ComponentType, val?: any) {
  51. if (!component) return val;
  52. if (['Input', 'InputPassword', 'InputSearch', 'InputTextArea'].includes(component)) {
  53. return val && isNumber(val) ? `${val}` : val;
  54. }
  55. return val;
  56. }
  57. /**
  58. * 时间字段
  59. */
  60. export const dateItemType = genType();