Skip to content

Commit

Permalink
feat: time
Browse files Browse the repository at this point in the history
  • Loading branch information
Liberty-liu committed May 9, 2023
1 parent e7b6a56 commit e8fa16a
Show file tree
Hide file tree
Showing 9 changed files with 273 additions and 45 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"axios": "^1.2.2",
"dayjs": "^1.11.7",
"element-plus": "^2.2.28",
"everright-filter": "^0.0.20",
"everright-filter": "^0.0.21",
"jss": "^10.9.2",
"jss-preset-default": "^10.9.2",
"lodash-es": "^4.17.21",
Expand Down
8 changes: 5 additions & 3 deletions packages/formEditor/components/FormTypes/Time/mobile.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import hooks from '@ER/hooks'
import { ref, computed, watch } from 'vue'
import dayjs from 'dayjs'
import customParseFormat from 'dayjs/plugin/customParseFormat.js'
dayjs.extend(customParseFormat)
export default {
name: 'er-time',
inheritAttrs: false,
Expand All @@ -16,19 +18,19 @@ const columnsType = ['hour', 'minute', 'second']
watch(() => props.data.options.defaultValue, (newVal) => {
let date = ''
if (newVal) {
date = dayjs(`${dayjs().format('YYYY/MM/DD')} ${newVal.split(':')}`, 'YYYY/MM/DD HH:mm:ss')
date = dayjs(newVal, props.data.options.valueFormat)
} else {
date = dayjs()
}
currentTime.value = date.format('HH:mm:ss').split(':')
currentTime.value = date.format(props.data.options.valueFormat).split(':')
}, {
immediate: true
})
const currentValue = computed({
get () {
let result = ''
if (props.data.options.defaultValue) {
result = dayjs(`${dayjs().format('YYYY/MM/DD')} ${props.data.options.defaultValue.split(':')}`, 'YYYY/MM/DD HH:mm:ss').format(props.data.options.format)
result = dayjs(props.data.options.defaultValue, props.data.options.valueFormat).format(props.data.options.format)
}
return result
},
Expand Down
1 change: 1 addition & 0 deletions packages/formEditor/componentsConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ export const fieldsConfig = [
options: {
clearable: true,
format: 'HH:mm:ss',
valueFormat: 'HH:mm:ss',
defaultValue: null,
placeholder: '',
labelWidth: 100,
Expand Down
6 changes: 6 additions & 0 deletions packages/formEditor/preview.vue
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ const setData2 = (data, value) => {
})
if (!_.isEmpty(value)) {
state.fields.forEach((e) => {
if (e.type === 'time' && !e.options.valueFormat) {
e.options.valueFormat = 'HH:mm:ss'
}
if (value[e.key]) {
e.options.defaultValue = value[e.key]
}
Expand All @@ -80,6 +83,9 @@ const setData1 = (data, value) => {
})
if (!_.isEmpty(value)) {
state.fields.forEach((e) => {
if (e.type === 'time' && !e.options.valueFormat) {
e.options.valueFormat = 'HH:mm:ss'
}
if (value[e.key]) {
e.options.defaultValue = value[e.key]
}
Expand Down
79 changes: 48 additions & 31 deletions packages/hooks/use-logic/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { computed, watch } from 'vue'
import _ from 'lodash-es'
import dayjs from 'dayjs'
import customParseFormat from 'dayjs/plugin/customParseFormat.js'
import isSameOrAfter from 'dayjs/plugin/isSameOrAfter.js'
import isSameOrBefore from 'dayjs/plugin/isSameOrBefore.js'
import utils from '@ER/utils'
dayjs.extend(customParseFormat)
dayjs.extend(isSameOrAfter)
dayjs.extend(isSameOrBefore)
const findValidityRule = (state) => {
const result = {}
for (const logicType in state.logic) {
Expand Down Expand Up @@ -33,10 +40,13 @@ const getDataType = (fieldType) => {
}
return result
}
const equal = (logicValue, value, fieldType) => {
const equal = (logicValue, value, field) => {
// console.log(logicValue)
// console.log(value)
if (fieldType === 'region') {
if (field.type === 'time') {
return dayjs(value, field.options.valueFormat).isSame(dayjs(logicValue, field.options.valueFormat))
}
if (field.type === 'region') {
return _.includes(logicValue, value)
}
if (_.isString(value) || _.isNumber(value)) {
Expand All @@ -52,7 +62,7 @@ const equal = (logicValue, value, fieldType) => {
const notEqual = (...e) => {
return !equal(...e)
}
const contains = (logicValue, value, fieldType) => {
const contains = (logicValue, value, field) => {
if (_.isString(value)) {
return logicValue.some((v) => _.includes(value, v))
}
Expand All @@ -63,77 +73,84 @@ const contains = (logicValue, value, fieldType) => {
const notContains = (...e) => {
return !contains(...e)
}
const empty = (logicValue, value, fieldType) => {
if (fieldType === 'rate') {
const empty = (logicValue, value, field) => {
if (field.type === 'rate') {
return value === 0 || utils.isEmpty(value)
}
return utils.isEmpty(value)
}
const notEmpty = (...e) => {
return !empty(...e)
}
const gt = (logicValue, value, fieldType) => {
const gt = (logicValue, value, field) => {
if (field.type === 'time') {
return dayjs(value, field.options.valueFormat).isAfter(dayjs(logicValue, field.options.valueFormat))
}
return _.gt(value, logicValue)
}
const gte = (logicValue, value, fieldType) => {
const gte = (logicValue, value, field) => {
if (field.type === 'time') {
return dayjs(value, field.options.valueFormat).isSameOrAfter(dayjs(logicValue, field.options.valueFormat))
}
return _.gte(value, logicValue)
}
const lt = (logicValue, value, fieldType) => {
const lt = (logicValue, value, field) => {
if (field.type === 'time') {
return dayjs(value, field.options.valueFormat).isBefore(dayjs(logicValue, field.options.valueFormat))
}
return _.lt(value === undefined ? 0 : value, logicValue)
}
const lte = (logicValue, value, fieldType) => {
const lte = (logicValue, value, field) => {
if (field.type === 'time') {
return dayjs(value, field.options.valueFormat).isSameOrBefore(dayjs(logicValue, field.options.valueFormat))
}
return _.lte(value === undefined ? 0 : value, logicValue)
}
const between = (logicValue, value, fieldType) => {
const between = (logicValue, value, field) => {
const [min, max] = logicValue
return lte(max, value) && gte(min, value)
return lte(max, value, field) && gte(min, value, field)
}
export const validator = (logic, value, field) => {
let result = false
switch (logic.operator) {
case 'equal':
result = equal(logic.value, value, field.type)
result = equal(logic.value, value, field)
break
case 'one_of':
break
case 'not_equal':
result = notEqual(logic.value, value, field.type)
result = notEqual(logic.value, value, field)
break
case 'contains':
result = contains(logic.value, value, field.type)
result = contains(logic.value, value, field)
break
case 'not_contain':
result = notContains(logic.value, value, field.type)
result = notContains(logic.value, value, field)
break
case 'empty':
result = empty(logic.value, value, field.type)
result = empty(logic.value, value, field)
break
case 'not_empty':
result = notEmpty(logic.value, value, field.type)
result = notEmpty(logic.value, value, field)
break
case 'greater_than':
// console.log(logic.value)
// console.log(`操作符的值:${logic.value} type: ${typeof logic.value}`)
// console.log(value)
// console.log(`field的值:${value} type: ${typeof value}`)
// console.log(field)
result = gt(logic.value, value, field.type)
result = gt(logic.value, value, field)
break
case 'greater_than_equal':
result = gte(logic.value, value, field.type)
result = gte(logic.value, value, field)
break
case 'less_than':
result = lt(logic.value, value, field.type)
result = lt(logic.value, value, field)
break
case 'less_than_equal':
result = lte(logic.value, value, field.type)
result = lte(logic.value, value, field)
break
case 'between':
console.log(logic.value)
console.log(`操作符的值:${logic.value} type: ${typeof logic.value}`)
console.log(value)
console.log(`field的值:${value} type: ${typeof value}`)
console.log(field)
// console.log(logic.value)
// console.log(`操作符的值:${logic.value} type: ${typeof logic.value}`)
// console.log(value)
// console.log(`field的值:${value} type: ${typeof value}`)
// console.log(field)
result = between(logic.value, value, field.type)
break
}
Expand Down
2 changes: 1 addition & 1 deletion packages/hooks/use-props/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ export const useProps = (state, data, isPc = true, isRoot = false, specialHandli
case 'time':
result.format = options.format
if (isPc) {
result.valueFormat = 'HH:mm:ss'
result.valueFormat = options.valueFormat
}
break
case 'date':
Expand Down
2 changes: 1 addition & 1 deletion packages/utils/addContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ export const addContext = (node, parent, fn) => {
case 'time':
result.format = options.format
if (isPc) {
result.valueFormat = 'HH:mm:ss'
result.valueFormat = options.valueFormat
}
break
case 'date':
Expand Down
13 changes: 6 additions & 7 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit e8fa16a

Please sign in to comment.