helper.ts 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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 { isNumber } from '/@/utils/is';
  5. const { t } = useI18n();
  6. /**
  7. * @description: 生成placeholder
  8. */
  9. export function createPlaceholderMessage(component: ComponentType) {
  10. if (component.includes('Input') || component.includes('Complete')) {
  11. return t('component.form.input');
  12. }
  13. if (component.includes('Picker')) {
  14. return t('component.form.choose');
  15. }
  16. if (
  17. component.includes('Select') ||
  18. component.includes('Cascader') ||
  19. component.includes('Checkbox') ||
  20. component.includes('Radio') ||
  21. component.includes('Switch')
  22. ) {
  23. // return `请选择${label}`;
  24. return t('component.form.choose');
  25. }
  26. return '';
  27. }
  28. function genType() {
  29. return ['DatePicker', 'MonthPicker', 'RangePicker', 'WeekPicker', 'TimePicker'];
  30. }
  31. export function setComponentRuleType(rule: ValidationRule, component: ComponentType) {
  32. if (['DatePicker', 'MonthPicker', 'WeekPicker', 'TimePicker'].includes(component)) {
  33. rule.type = 'object';
  34. } else if (['RangePicker', 'Upload', 'CheckboxGroup', 'TimePicker'].includes(component)) {
  35. rule.type = 'array';
  36. } else if (['InputNumber'].includes(component)) {
  37. rule.type = 'number';
  38. }
  39. }
  40. export function handleInputNumberValue(component?: ComponentType, val: any) {
  41. if (!component) return val;
  42. if (['Input', 'InputPassword', 'InputSearch', 'InputTextArea'].includes(component)) {
  43. return val && isNumber(val) ? `${val}` : val;
  44. }
  45. return val;
  46. }
  47. /**
  48. * 时间字段
  49. */
  50. export const dateItemType = genType();