|
| 1 | +import { observer } from '@formily/reactive-vue' |
| 2 | +import { BaseFieldSelect as FieldSelect } from '@tap/form' |
| 3 | +import i18n from '@tap/i18n' |
| 4 | +import { defineComponent } from 'vue' |
| 5 | +import { RelativeTimePicker } from '../relative-time-picker' |
| 6 | +import './style.scss' |
| 7 | + |
| 8 | +const OPERATOR_OPTIONS = [ |
| 9 | + { label: '>', value: 1 }, |
| 10 | + { label: '>=', value: 2 }, |
| 11 | + { label: '<', value: 3 }, |
| 12 | + { label: '<=', value: 4 }, |
| 13 | + { label: '=', value: 5 }, |
| 14 | +] |
| 15 | + |
| 16 | +const DATE_VIEW_OPTIONS = [ |
| 17 | + { label: i18n.t('public_date_specific'), value: false }, |
| 18 | + { label: i18n.t('public_date_relative'), value: true }, |
| 19 | +] |
| 20 | + |
| 21 | +const DATE_TYPE_REG = /timestamp|date/i |
| 22 | + |
| 23 | +const isDateField = (field: any) => DATE_TYPE_REG.test(field?.type || '') |
| 24 | + |
| 25 | +const createItem = () => ({ |
| 26 | + key: '', |
| 27 | + value: '', |
| 28 | + operator: 5, |
| 29 | + fastQuery: false, |
| 30 | + number: 1, |
| 31 | + unit: 'DAY', |
| 32 | + form: 'BEFORE', |
| 33 | +}) |
| 34 | + |
| 35 | +export const ConditionsEditor = observer( |
| 36 | + defineComponent({ |
| 37 | + name: 'ConditionsEditor', |
| 38 | + inheritAttrs: false, |
| 39 | + props: { |
| 40 | + value: { type: Array, default: () => [] }, |
| 41 | + disabled: Boolean, |
| 42 | + fields: { type: Array, default: () => [] }, |
| 43 | + fieldsLoading: Boolean, |
| 44 | + offsetHours: { type: Number, default: 0 }, |
| 45 | + }, |
| 46 | + setup(props) { |
| 47 | + const getField = (key: string) => |
| 48 | + (props.fields as any[])?.find((item) => item.value === key) |
| 49 | + |
| 50 | + const handleAdd = () => { |
| 51 | + ;(props.value as any[]).push(createItem()) |
| 52 | + } |
| 53 | + |
| 54 | + const handleRemove = (index: number) => { |
| 55 | + ;(props.value as any[]).splice(index, 1) |
| 56 | + } |
| 57 | + |
| 58 | + const handleFieldChange = (item: any, val: string) => { |
| 59 | + item.key = val |
| 60 | + item.value = '' |
| 61 | + } |
| 62 | + |
| 63 | + return () => { |
| 64 | + const items = (props.value || []) as any[] |
| 65 | + |
| 66 | + return ( |
| 67 | + <div class="conditions-editor"> |
| 68 | + {items.map((item, index) => { |
| 69 | + const field = getField(item.key) |
| 70 | + const dateField = isDateField(field) |
| 71 | + |
| 72 | + return ( |
| 73 | + <div class="conditions-editor__row" key={index}> |
| 74 | + <div class="conditions-editor__main"> |
| 75 | + <FieldSelect |
| 76 | + class="conditions-editor__field" |
| 77 | + modelValue={item.key} |
| 78 | + options={props.fields} |
| 79 | + loading={props.fieldsLoading} |
| 80 | + filterable |
| 81 | + disabled={props.disabled} |
| 82 | + onUpdate:modelValue={(val: string) => |
| 83 | + handleFieldChange(item, val) |
| 84 | + } |
| 85 | + /> |
| 86 | + <ElSelect |
| 87 | + class="conditions-editor__operator" |
| 88 | + v-model={item.operator} |
| 89 | + disabled={props.disabled || item.fastQuery} |
| 90 | + > |
| 91 | + {OPERATOR_OPTIONS.map((option) => ( |
| 92 | + <ElOption |
| 93 | + key={option.value} |
| 94 | + label={option.label} |
| 95 | + value={option.value} |
| 96 | + ></ElOption> |
| 97 | + ))} |
| 98 | + </ElSelect> |
| 99 | + <div class="conditions-editor__value"> |
| 100 | + {dateField ? ( |
| 101 | + <> |
| 102 | + <ElSegmented |
| 103 | + class="w-100" |
| 104 | + v-model={item.fastQuery} |
| 105 | + options={DATE_VIEW_OPTIONS} |
| 106 | + disabled={props.disabled} |
| 107 | + onChange={(val: boolean) => { |
| 108 | + if (val) { |
| 109 | + item.operator = 5 |
| 110 | + } |
| 111 | + }} |
| 112 | + /> |
| 113 | + |
| 114 | + <div class="conditions-editor__date flex justify-content-end"> |
| 115 | + {item.fastQuery ? ( |
| 116 | + <RelativeTimePicker |
| 117 | + disabled={props.disabled} |
| 118 | + offsetHours={props.offsetHours} |
| 119 | + number={item.number} |
| 120 | + unit={item.unit} |
| 121 | + form={item.form} |
| 122 | + onChange={(val: any) => |
| 123 | + Object.assign(item, val) |
| 124 | + } |
| 125 | + /> |
| 126 | + ) : ( |
| 127 | + <ElDatePicker |
| 128 | + class="w-100" |
| 129 | + modelValue={item.value} |
| 130 | + type="datetime" |
| 131 | + format="YYYY-MM-DD HH:mm:ss" |
| 132 | + value-format="YYYY-MM-DD HH:mm:ss" |
| 133 | + disabled={props.disabled} |
| 134 | + onUpdate:modelValue={(val: string) => |
| 135 | + (item.value = val) |
| 136 | + } |
| 137 | + /> |
| 138 | + )} |
| 139 | + </div> |
| 140 | + </> |
| 141 | + ) : ( |
| 142 | + <ElInput |
| 143 | + modelValue={item.value} |
| 144 | + disabled={props.disabled} |
| 145 | + onUpdate:modelValue={(val: string) => |
| 146 | + (item.value = val) |
| 147 | + } |
| 148 | + /> |
| 149 | + )} |
| 150 | + </div> |
| 151 | + <ElButton |
| 152 | + class="conditions-editor__remove" |
| 153 | + text |
| 154 | + disabled={props.disabled || items.length < 2} |
| 155 | + onClick={() => handleRemove(index)} |
| 156 | + icon={IconLucideTrash2} |
| 157 | + ></ElButton> |
| 158 | + </div> |
| 159 | + </div> |
| 160 | + ) |
| 161 | + })} |
| 162 | + <ElButton |
| 163 | + class="w-100 border-dashed" |
| 164 | + disabled={props.disabled} |
| 165 | + onClick={handleAdd} |
| 166 | + icon={IconLucidePlus} |
| 167 | + ></ElButton> |
| 168 | + </div> |
| 169 | + ) |
| 170 | + } |
| 171 | + }, |
| 172 | + }), |
| 173 | +) |
| 174 | + |
| 175 | +export default ConditionsEditor |
0 commit comments