Skip to content

Commit

Permalink
fix x-index order algorithm (#724)
Browse files Browse the repository at this point in the history
  • Loading branch information
JerryLyu93 authored Mar 15, 2020
1 parent e1f178b commit 17ae9dd
Show file tree
Hide file tree
Showing 2 changed files with 229 additions and 62 deletions.
278 changes: 223 additions & 55 deletions packages/react-schema-renderer/src/__tests__/schema.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,30 @@ import { Schema } from '../'

describe('major scene', () => {
test('basic field schema', () => {
const s1 = new Schema({
"type":"date",
"title":"test header",
"x-props": {"attr":'x'},
"x-component-props": {"attr": 'xc', replace: true },
}, undefined, 'test')
const s1 = new Schema(
{
type: 'date',
title: 'test header',
'x-props': { attr: 'x' },
'x-component-props': { attr: 'xc', replace: true }
},
undefined,
'test'
)

expect(s1.getExtendsComponentProps()).toEqual({ attr: 'xc', replace: true })
expect(s1.getExtendsComponentProps()).toEqual({ attr: 'xc', replace: true })
expect(s1.getExtendsVisible()).toEqual(undefined)
expect(s1.getExtendsDisplay()).toEqual(undefined)
expect(s1.getExtendsEditable()).toEqual(undefined)

const s2 = new Schema({
"type":"date",
"title":"test header",
}, undefined, 'test')
const s2 = new Schema(
{
type: 'date',
title: 'test header'
},
undefined,
'test'
)

s2.visible = true
s2.display = true
Expand All @@ -27,34 +35,46 @@ describe('major scene', () => {
expect(s2.getExtendsDisplay()).toEqual(true)
expect(s2.getExtendsEditable()).toEqual(true)

const s3 = new Schema({
"type":"date",
"title":"test header",
"x-props": { visible: 'x', display: 'x', editable: 'x' },
"x-component-props": {},
}, undefined, 'test')
const s3 = new Schema(
{
type: 'date',
title: 'test header',
'x-props': { visible: 'x', display: 'x', editable: 'x' },
'x-component-props': {}
},
undefined,
'test'
)

expect(s3.getExtendsVisible()).toEqual('x')
expect(s3.getExtendsDisplay()).toEqual('x')
expect(s3.getExtendsEditable()).toEqual('x')

const s4 = new Schema({
"type":"date",
"title":"test header",
"x-props": {},
"x-component-props": { visible: 'xc', display: 'xc', editable: 'xc' },
}, undefined, 'test')
const s4 = new Schema(
{
type: 'date',
title: 'test header',
'x-props': {},
'x-component-props': { visible: 'xc', display: 'xc', editable: 'xc' }
},
undefined,
'test'
)

expect(s4.getExtendsVisible()).toEqual('xc')
expect(s4.getExtendsDisplay()).toEqual('xc')
expect(s4.getExtendsEditable()).toEqual('xc')

const s5 = new Schema({
"type":"date",
"title":"test header",
"x-props": { visible: 'x', display: 'x', editable: 'x' },
"x-component-props": { visible: 'xc', display: 'xc', editable: 'xc' },
}, undefined, 'test')
const s5 = new Schema(
{
type: 'date',
title: 'test header',
'x-props': { visible: 'x', display: 'x', editable: 'x' },
'x-component-props': { visible: 'xc', display: 'xc', editable: 'xc' }
},
undefined,
'test'
)

s5.visible = true
s5.display = true
Expand All @@ -66,42 +86,190 @@ describe('major scene', () => {
})

test('labelCol and wrapperCol(without filter)', () => {
const s = new Schema({
"type":"date",
"title":"test header",
"x-props": { "labelCol": 2, "wrapperCol": 22 },
"x-component-props": { "labelCol": 6, "wrapperCol": 18 },
}, undefined, 'test')
const s = new Schema(
{
type: 'date',
title: 'test header',
'x-props': { labelCol: 2, wrapperCol: 22 },
'x-component-props': { labelCol: 6, wrapperCol: 18 }
},
undefined,
'test'
)

expect(s.getExtendsComponentProps()).toEqual({
"labelCol": 6, "wrapperCol": 18
})
labelCol: 6,
wrapperCol: 18
})
})

test('labelCol and wrapperCol(with filter)', () => {
const s = new Schema({
"type":"date",
"title":"test header",
"x-props": { "labelCol": 2, "wrapperCol": 22 },
"x-component-props": { "labelCol": 6, "wrapperCol": 18 },
}, undefined, 'test')

expect(s.getExtendsComponentProps()).toEqual({ "labelCol": 6, "wrapperCol": 18 })
const s = new Schema(
{
type: 'date',
title: 'test header',
'x-props': { labelCol: 2, wrapperCol: 22 },
'x-component-props': { labelCol: 6, wrapperCol: 18 }
},
undefined,
'test'
)

expect(s.getExtendsComponentProps()).toEqual({
labelCol: 6,
wrapperCol: 18
})
})

test('deprecate x-item-props', () => {
test('x-index', () => {
// 全排序
const s1 = new Schema({
"type":"date",
"title":"test header",
}, undefined, 'test')
type: 'object',
properties: {
p1: {
'x-index': 1
},
p2: {
'x-index': 3
},
p3: {
'x-index': 2
},
p4: {
'x-index': 4
},
p5: {
'x-index': 5
}
}
})

expect(s1.getExtendsItemProps()).toEqual({})
expect(s1.getOrderProperties().map(k => k.key)).toEqual([
'p1',
'p3',
'p2',
'p4',
'p5'
])

// 多排序
const s2 = new Schema({
"type":"date",
"title":"test header",
"x-item-props": { hello: 'world' },
}, undefined, 'test')
type: 'object',
properties: {
p1: {
'x-index': 1
},
p2: {
'x-index': 5
},
p3: {
'x-index': 7
},
p4: {},
p5: {
'x-index': 2
}
}
})

expect(s2.getOrderProperties().map(k => k.key)).toEqual([
'p1',
'p5',
'p2',
'p3',
'p4'
])

// 多无序
const s3 = new Schema({
type: 'object',
properties: {
p1: {},
p2: {
'x-index': 3
},
p3: {},
p4: {},
p5: {}
}
})

expect(s3.getOrderProperties().map(k => k.key)).toEqual([
'p2',
'p1',
'p3',
'p4',
'p5'
])

// 交叉顺序
const s4 = new Schema({
type: 'object',
properties: {
p1: {
'x-index': 2
},
p2: {},
p3: {
'x-index': 0
},
p4: {
'x-index': 1
},
p5: {}
}
})

expect(s4.getOrderProperties().map(k => k.key)).toEqual([
'p3',
'p4',
'p1',
'p2',
'p5'
])

// 全无序
const s5 = new Schema({
type: 'object',
properties: {
p1: {},
p2: {},
p3: {},
p4: {},
p5: {}
}
})

expect(s5.getOrderProperties().map(k => k.key)).toEqual([
'p1',
'p2',
'p3',
'p4',
'p5'
])
})

test('deprecate x-item-props', () => {
const s1 = new Schema(
{
type: 'date',
title: 'test header'
},
undefined,
'test'
)

expect(s1.getExtendsItemProps()).toEqual({})

const s2 = new Schema(
{
type: 'date',
title: 'test header',
'x-item-props': { hello: 'world' }
},
undefined,
'test'
)
expect(s2.getExtendsItemProps()).toEqual({ hello: 'world' })
})
})
})
13 changes: 6 additions & 7 deletions packages/react-schema-renderer/src/shared/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -502,18 +502,17 @@ export class Schema implements ISchema {
propertiesName: string = 'properties'
) => {
const newSchema = new Schema(schema)
const properties = []
const orderProperties = []
const unorderProperties = []
each(newSchema[propertiesName], (item, key) => {
const index = item['x-index']
if (typeof index === 'number') {
properties[index] = {
schema: item,
key
}
orderProperties[index] = { schema: item, key }
} else {
properties.push({ schema: item, key })
unorderProperties.push({ schema: item, key })
}
})
return properties.filter(item => !!item)

return orderProperties.concat(unorderProperties).filter(item => !!item)
}
}

0 comments on commit 17ae9dd

Please sign in to comment.