Skip to content

Commit

Permalink
feat: between
Browse files Browse the repository at this point in the history
  • Loading branch information
Liberty-liu committed May 9, 2023
1 parent 0fb6d43 commit e7b6a56
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 3 deletions.
11 changes: 8 additions & 3 deletions packages/hooks/use-logic/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ const lt = (logicValue, value, fieldType) => {
const lte = (logicValue, value, fieldType) => {
return _.lte(value === undefined ? 0 : value, logicValue)
}
const between = (logicValue, value, fieldType) => {
const [min, max] = logicValue
return lte(max, value) && gte(min, value)
}
export const validator = (logic, value, field) => {
let result = false
switch (logic.operator) {
Expand Down Expand Up @@ -122,14 +126,15 @@ export const validator = (logic, value, field) => {
result = lt(logic.value, value, field.type)
break
case 'less_than_equal':
result = lte(logic.value, value, field.type)
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)
result = lte(logic.value, value, field.type)
break
case 'between':
result = between(logic.value, value, field.type)
break
}
return result
Expand Down
105 changes: 105 additions & 0 deletions test/logic/logic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,41 @@ describe('validator', () => {
},
1.9,
field)).toBeTruthy()
expect(
validator({
operator: 'between',
value: [1, 2]
},
1,
field)).toBeTruthy()
expect(
validator({
operator: 'between',
value: [1, 2]
},
2.0,
field)).toBeTruthy()
expect(
validator({
operator: 'between',
value: [1, 2]
},
null,
field)).toBeFalsy()
expect(
validator({
operator: 'between',
value: [1, 2]
},
'',
field)).toBeFalsy()
expect(
validator({
operator: 'between',
value: [1, 2]
},
undefined,
field)).toBeFalsy()
})
test('Radio', () => {
const field = { type: 'radio', label: 'Radio', icon: 'radio', key: 'radio_FQWZ5Tf_KQryjLOj_-Oso', id: 'FQWZ5Tf_KQryjLOj_-Oso', options: { dataKey: 'FQWZ5Tf_KQryjLOj_-Oso', displayStyle: 'block', defaultValue: '', labelWidth: 100, isShowLabel: true, disabled: false, required: false }, style: { width: { pc: '100%', mobile: '100%' } } }
Expand Down Expand Up @@ -1174,6 +1209,41 @@ describe('validator', () => {
},
1.9,
field)).toBeTruthy()
expect(
validator({
operator: 'between',
value: [1, 2]
},
1,
field)).toBeTruthy()
expect(
validator({
operator: 'between',
value: [1, 2]
},
2.0,
field)).toBeTruthy()
expect(
validator({
operator: 'between',
value: [1, 2]
},
null,
field)).toBeFalsy()
expect(
validator({
operator: 'between',
value: [1, 2]
},
'',
field)).toBeFalsy()
expect(
validator({
operator: 'between',
value: [1, 2]
},
undefined,
field)).toBeFalsy()
})
test('Switch', () => {
const field = { type: 'switch', label: 'Switch', icon: 'switch', key: 'switch_XpvOZGZjVMTVdHWZiuehy', id: 'XpvOZGZjVMTVdHWZiuehy', options: { defaultValue: true, labelWidth: 100, isShowLabel: true, disabled: false }, style: { width: { pc: '100%', mobile: '100%' } } }
Expand Down Expand Up @@ -1313,6 +1383,41 @@ describe('validator', () => {
},
1.9,
field)).toBeTruthy()
expect(
validator({
operator: 'between',
value: [1, 2]
},
1,
field)).toBeTruthy()
expect(
validator({
operator: 'between',
value: [1, 2]
},
2.0,
field)).toBeTruthy()
expect(
validator({
operator: 'between',
value: [1, 2]
},
null,
field)).toBeFalsy()
expect(
validator({
operator: 'between',
value: [1, 2]
},
'',
field)).toBeFalsy()
expect(
validator({
operator: 'between',
value: [1, 2]
},
undefined,
field)).toBeFalsy()
})
test('Html', () => {
const field = { type: 'html', label: 'Html', icon: 'html', key: 'html_cBsbj6NW7wNicW2umHzsE', id: 'cBsbj6NW7wNicW2umHzsE', options: { defaultValue: '<p>123</p>', placeholder: 'Please enter', action: 'http://localhost:8001/Everright-api/lowCode/uploads', size: 1, labelWidth: 100, isShowLabel: true, required: false, disabled: false }, style: { width: { pc: '100%', mobile: '100%' } } }
Expand Down

0 comments on commit e7b6a56

Please sign in to comment.