amis/examples/components/Form/Schem.jsx

160 lines
3.3 KiB
React
Raw Normal View History

2019-06-11 17:00:02 +08:00
import React from 'react';
2019-04-30 11:11:25 +08:00
import TitleBar from '../../../src/components/TitleBar';
import {render} from '../../../src/index';
const Schema = {
2019-11-07 10:41:14 +08:00
title: 'Person',
type: 'object',
properties: {
firstName: {
title: 'First Name',
type: 'string'
},
lastName: {
type: 'string'
},
age: {
description: 'Age in years',
type: 'integer',
minimum: 0
},
tag: {
type: 'array',
description: 'Tags',
default: ['IT'],
items: {
type: 'text'
}
2019-04-30 11:11:25 +08:00
},
2019-11-07 10:41:14 +08:00
clients: {
type: 'array',
description: 'Tags',
items: {
type: 'object',
properties: {
firstName: {
title: 'First Name',
type: 'string'
},
lastName: {
type: 'string'
}
2019-04-30 11:11:25 +08:00
}
2019-11-07 10:41:14 +08:00
}
2019-04-30 11:11:25 +08:00
}
2019-11-07 10:41:14 +08:00
},
required: ['firstName', 'lastName']
};
2019-04-30 11:11:25 +08:00
2019-11-07 10:41:14 +08:00
function property2control(property, key, schema) {
const requiredList = schema.required || [];
const rest = {};
const validations = {};
let type = 'text';
if (property.type === 'integer') {
type = 'number';
typeof property.minimum === 'number' && (rest.min = property.minimum);
// property.max
} else if (property.type === 'array') {
type = 'combo';
const items = property.items;
if (items.type === 'object') {
rest.controls = makeControls(items.properties, items);
rest.multiLine = true;
} else {
type = 'array';
rest.inline = true;
rest.items = property2control(items, 'item', property);
2019-04-30 11:11:25 +08:00
}
2019-11-07 10:41:14 +08:00
}
if (typeof property.minimum === 'number') {
validations.minimum = property.minimum;
}
return {
name: key,
type,
required: !!~requiredList.indexOf(key),
label: property.title || property.description,
desc: property.title && property.description,
value: property.default,
validations,
...rest
};
2019-04-30 11:11:25 +08:00
}
function makeControls(properties, schema) {
2019-11-07 10:41:14 +08:00
const keys = Object.keys(properties);
return keys.map(key => property2control(properties[key], key, schema));
2019-04-30 11:11:25 +08:00
}
function JSONSchme2AMisSchema(schema) {
2019-11-07 10:41:14 +08:00
if (schema.type !== 'object') {
throw new Error('JSONSchme2AMisSchema 只支持 object 转换');
}
return {
title: schema.title,
type: 'form',
mode: 'horizontal',
controls: makeControls(schema.properties, schema)
};
2019-04-30 11:11:25 +08:00
}
const amisFormSchema = JSONSchme2AMisSchema(Schema);
export default class JSONSchemaForm extends React.Component {
2019-11-07 10:41:14 +08:00
state = {
data: {}
};
renderForm() {
return render({
type: 'page',
title: '',
body: {
...amisFormSchema,
onChange: values =>
this.setState({
data: {
...values
2019-04-30 11:11:25 +08:00
}
2019-11-07 10:41:14 +08:00
})
}
});
}
render() {
return (
<div>
<TitleBar title="JSON Schema Form" />
<div className="wrapper">
<div>
<h3>Schema</h3>
<pre>
<code>{JSON.stringify(Schema, null, 2)}</code>
</pre>
</div>
<div>
<h3>Form</h3>
{this.renderForm()}
</div>
<div>
<h3>Data</h3>
<pre>
<code>{JSON.stringify(this.state.data, null, 2)}</code>
</pre>
</div>
</div>
</div>
);
}
2019-04-30 11:11:25 +08:00
}