|
| 1 | +import { defineComponent, type App, DefineComponent, Plugin } from 'vue'; |
| 2 | +import dayjs from 'dayjs'; |
| 3 | +import { fieldRangePickerProps, FieldRangePickerProps, RangesType } from './types'; |
| 4 | +import { RangePicker } from 'ant-design-vue'; |
| 5 | +import { getSlot } from '@ant-design-vue/pro-utils'; |
| 6 | +import type { VueNode } from 'ant-design-vue/lib/_util/type'; |
| 7 | + |
| 8 | +const formatDate = (text: any, format: any) => { |
| 9 | + if (!text) { |
| 10 | + return '-'; |
| 11 | + } |
| 12 | + if (typeof format === 'function') { |
| 13 | + return format(dayjs(text)); |
| 14 | + } else { |
| 15 | + return dayjs(text).format(format || 'YYYY-MM-DD'); |
| 16 | + } |
| 17 | +}; |
| 18 | + |
| 19 | +export const slots = [ |
| 20 | + 'suffixIcon', |
| 21 | + 'prevIcon', |
| 22 | + 'nextIcon', |
| 23 | + 'superPrevIcon', |
| 24 | + 'superNextIcon', |
| 25 | + 'renderExtraFooter', |
| 26 | + 'dateRender', |
| 27 | +]; |
| 28 | + |
| 29 | +const FieldRangePicker = defineComponent({ |
| 30 | + name: 'FieldRangePicker', |
| 31 | + inheritAttrs: false, |
| 32 | + props: fieldRangePickerProps, |
| 33 | + slots, |
| 34 | + setup(props, { slots }) { |
| 35 | + const suffixIcon = getSlot<() => VueNode>(slots, props.fieldProps as Record<string, any>, 'suffixIcon'); |
| 36 | + const prevIcon = getSlot<() => VueNode>(slots, props.fieldProps as Record<string, any>, 'prevIcon'); |
| 37 | + const nextIcon = getSlot<() => VueNode>(slots, props.fieldProps as Record<string, any>, 'nextIcon'); |
| 38 | + const superPrevIcon = getSlot<() => VueNode>(slots, props.fieldProps as Record<string, any>, 'superPrevIcon'); |
| 39 | + const superNextIcon = getSlot<() => VueNode>(slots, props.fieldProps as Record<string, any>, 'superNextIcon'); |
| 40 | + const renderExtraFooter = getSlot<() => VueNode>( |
| 41 | + slots, |
| 42 | + props.fieldProps as Record<string, any>, |
| 43 | + 'renderExtraFooter' |
| 44 | + ); |
| 45 | + const dateRender = getSlot<() => VueNode>(slots, props.fieldProps as Record<string, any>, 'dateRender'); |
| 46 | + |
| 47 | + const render = getSlot(slots, props.fieldProps as Record<string, any>, 'render') as any; |
| 48 | + const renderFormItem = getSlot(slots, props.fieldProps as Record<string, any>, 'renderFormItem') as any; |
| 49 | + |
| 50 | + return () => { |
| 51 | + const { mode, text, fieldProps } = props; |
| 52 | + const { placeholder, ranges, format = 'YYYY-MM-DD' } = fieldProps || {}; |
| 53 | + const [startText, endText] = Array.isArray(text) ? text : []; |
| 54 | + |
| 55 | + if (mode === 'read') { |
| 56 | + const parsedStartText: string = startText ? formatDate(startText, format) : ''; |
| 57 | + const parsedEndText: string = endText ? formatDate(endText, format) : ''; |
| 58 | + const dom = ( |
| 59 | + <div> |
| 60 | + <div>{parsedStartText || '-'}</div> |
| 61 | + <div>{parsedEndText || '-'}</div> |
| 62 | + </div> |
| 63 | + ); |
| 64 | + if (render) { |
| 65 | + return render(text, { mode, ...fieldProps }, <>{dom}</>); |
| 66 | + } |
| 67 | + return dom; |
| 68 | + } |
| 69 | + if (mode === 'edit' || mode === 'update') { |
| 70 | + const dom = ( |
| 71 | + <RangePicker |
| 72 | + v-slots={{ |
| 73 | + suffixIcon, |
| 74 | + prevIcon, |
| 75 | + nextIcon, |
| 76 | + superPrevIcon, |
| 77 | + superNextIcon, |
| 78 | + renderExtraFooter, |
| 79 | + dateRender, |
| 80 | + }} |
| 81 | + {...fieldProps} |
| 82 | + format={format} |
| 83 | + ranges={ranges as RangesType} |
| 84 | + placeholder={placeholder || ['请选择', '请选择']} |
| 85 | + allowClear |
| 86 | + /> |
| 87 | + ); |
| 88 | + if (renderFormItem) { |
| 89 | + return renderFormItem(text, { mode, ...fieldProps }, dom); |
| 90 | + } |
| 91 | + return dom; |
| 92 | + } |
| 93 | + return null; |
| 94 | + }; |
| 95 | + }, |
| 96 | +}); |
| 97 | + |
| 98 | +FieldRangePicker.install = (app: App) => { |
| 99 | + app.component(FieldRangePicker.name, FieldRangePicker); |
| 100 | + return app; |
| 101 | +}; |
| 102 | + |
| 103 | +export default FieldRangePicker as DefineComponent<FieldRangePickerProps> & Plugin; |
0 commit comments