diff --git a/package/components/SchemaComponents/SchemaJson.js b/package/components/SchemaComponents/SchemaJson.js index e48e80f..1084b65 100644 --- a/package/components/SchemaComponents/SchemaJson.js +++ b/package/components/SchemaComponents/SchemaJson.js @@ -29,6 +29,8 @@ import LocaleProvider from '../LocalProvider/index.js'; import utils from '../../utils'; import MockSelect from '../MockSelect/index.js'; + +// 添加字段类型是字段还是数组 const mapping = (name, data, showEdit, showAdv) => { switch (data.type) { case 'array': @@ -43,6 +45,7 @@ const mapping = (name, data, showEdit, showAdv) => { } }; + class SchemaArray extends PureComponent { constructor(props, context) { super(props); @@ -220,6 +223,10 @@ SchemaArray.contextTypes = { isMock: PropTypes.bool }; + + + +// 内容Schema class SchemaItem extends PureComponent { constructor(props, context) { super(props); @@ -312,6 +319,43 @@ class SchemaItem extends PureComponent { this.Model.addFieldAction({ prefix, name }); }; + + // 向上 + handleUpItem = () => { + const { prefix, name, data } = this.props; + let nameArray = this.getPrefix(); + let requiredArr = data.required; + let itemIndex = requiredArr.indexOf(name); + console.log(prefix); + console.log(data.required); + console.log(data.properties); + + if(itemIndex === 0){ + return false; + } + + this.Model.upItemAction({ key: nameArray }); + // this.Model.enableRequireAction({ prefix, name, up:true }); // required:true + } + + // 向下 + handleDownItem = () => { + const { prefix, name, data } = this.props; + let nameArray = this.getPrefix(); + let requiredArr = data.required; + let itemIndex = requiredArr.indexOf(name); + let len = requiredArr.length; + console.log(prefix); + console.log(data.required) + console.log(data.properties); + + if(itemIndex === len-1){ + return false; + } + + this.Model.downItemAction({ key: nameArray }); + } + // 控制三角形按钮 handleClickIcon = () => { let prefix = this.getPrefix(); @@ -341,7 +385,7 @@ class SchemaItem extends PureComponent {
@@ -376,7 +420,6 @@ class SchemaItem extends PureComponent { - - + this.handleShowEdit('description')} />} placeholder={LocaleProvider('description')} @@ -431,8 +473,8 @@ class SchemaItem extends PureComponent { /> - - + {/* 操作按钮 */} + @@ -444,12 +486,18 @@ class SchemaItem extends PureComponent { {value.type === 'object' ? ( ) : ( - + )} + + + + + +
{mapping(prefixArray, value, showEdit, showAdv)}
@@ -464,17 +512,20 @@ SchemaItem.contextTypes = { isMock: PropTypes.bool }; + + +// 字段为对象类型处理 class SchemaObjectComponent extends Component { - shouldComponentUpdate(nextProps) { - if ( - _.isEqual(nextProps.data, this.props.data) && - _.isEqual(nextProps.prefix, this.props.prefix) && - _.isEqual(nextProps.open, this.props.open) - ) { - return false; - } - return true; - } + // shouldComponentUpdate(nextProps) { + // if ( + // _.isEqual(nextProps.data, this.props.data) && + // _.isEqual(nextProps.prefix, this.props.prefix) && + // _.isEqual(nextProps.open, this.props.open) + // ) { + // return false; + // } + // return true; + // } render() { const { data, prefix, showEdit, showAdv } = this.props; @@ -482,7 +533,7 @@ class SchemaObjectComponent extends Component {
{Object.keys(data.properties).map((name, index) => ( { const item = mapping([], props.data, props.showEdit, props.showAdv); - return
{item}
; + return
{item}
; }; export default SchemaJson; diff --git a/package/components/SchemaComponents/schemaJson.css b/package/components/SchemaComponents/schemaJson.css index 12d039d..adb401a 100644 --- a/package/components/SchemaComponents/schemaJson.css +++ b/package/components/SchemaComponents/schemaJson.css @@ -44,10 +44,21 @@ cursor: pointer; } +.ant-dropdown-trigger { + margin-right: 8px!important; +} + .json-schema-react-editor .plus { color: #2395f1 } +.json-schema-react-editor .up { + color: #780650 +} +.json-schema-react-editor .down { + color: #10239e +} + .json-schema-react-editor .close { color: #ff561b } diff --git a/package/models/schema.js b/package/models/schema.js index 11dfafb..0f4a2e2 100644 --- a/package/models/schema.js +++ b/package/models/schema.js @@ -100,7 +100,11 @@ export default { let requiredData = [].concat(parentData.required || []); let index = requiredData.indexOf(action.name); - if (!action.required && index >= 0) { + if(action.up || action.down){ + parentKeys.push('required'); + utils.setData(state.data, parentKeys, requiredData); + } + else if (!action.required && index >= 0) { requiredData.splice(index, 1); parentKeys.push('required'); if (requiredData.length === 0) { @@ -115,6 +119,33 @@ export default { } }, + enableRequireActionUpdate: function(state, action, oldState) { + const keys = action.prefix; + let parentKeys = utils.getParentKeys(keys); + let oldData = oldState.data; + let parentData = utils.getData(oldData, parentKeys); + let requiredData = [].concat(parentData.required || []); + let index = requiredData.indexOf(action.name); + + if(action.up){ + utils.setData(state.data, parentKeys, requiredData); + } + else{ + if (!action.required && index >= 0) { + parentKeys.push('required'); + if (requiredData.length === 0) { + utils.deleteData(state.data, parentKeys); + } else { + utils.setData(state.data, parentKeys, requiredData); + } + } else if (action.required && index === -1) { + requiredData.push(action.name); + parentKeys.push('required'); + utils.setData(state.data, parentKeys, requiredData); + } + } + }, + requireAllAction: function(state, action, oldState) { // let oldData = oldState.data; let data = utils.cloneObject(action.value); @@ -123,9 +154,9 @@ export default { state.data = data; }, + // 删除节点 deleteItemAction: function(state, action, oldState) { const keys = action.key; - let name = keys[keys.length - 1]; let oldData = oldState.data; let parentKeys = utils.getParentKeys(keys); @@ -140,6 +171,84 @@ export default { utils.setData(state.data, parentKeys, newParentData); }, + // 向上 + upItemAction: function(state, action, oldState) { + const keys = action.key; + let name = keys[keys.length - 1]; + let oldData = oldState.data; + let parentKeys = utils.getParentKeys(keys); + let parentData = utils.getData(oldData, parentKeys); + + // 数组key + let requiredData = Object.keys(parentData); + + // 取移动项下标 + let itemIndex = requiredData.indexOf(name); + + // 如果第一个元素就不允许在执行 + if(itemIndex === 0){ + return false; + } + + let pevitemName = requiredData[itemIndex-1]; + + // 将数组下标进行换位 + requiredData[itemIndex] = pevitemName; + requiredData[itemIndex-1] = name; + + // 修改properties + let newParentData = {}; + requiredData.forEach((item) => { + newParentData[item] = parentData[item] + }) + utils.setData(state.data, parentKeys, newParentData); + + // 修改required + let requiredKeys = utils.getParentKeys(parentKeys); + requiredKeys.push('required') + utils.setData(state.data, requiredKeys, requiredData); + }, + + // 向下 + downItemAction: function(state, action, oldState) { + const keys = action.key; + let name = keys[keys.length - 1]; + let oldData = oldState.data; + let parentKeys = utils.getParentKeys(keys); + let parentData = utils.getData(oldData, parentKeys); + + // 数组key + let requiredData = Object.keys(parentData); + let len = requiredData.length + + // 取移动项下标 + let itemIndex = requiredData.indexOf(name); + + // 如果第一个元素就不允许在执行 + if(itemIndex === len-1){ + return false; + } + + let pevitemName = requiredData[itemIndex+1]; + + // 将数组下标进行换位 + requiredData[itemIndex] = pevitemName; + requiredData[itemIndex+1] = name; + + // 修改properties + let newParentData = {}; + requiredData.forEach((item) => { + newParentData[item] = parentData[item] + }) + utils.setData(state.data, parentKeys, newParentData); + + // 修改required + let requiredKeys = utils.getParentKeys(parentKeys); + requiredKeys.push('required') + utils.setData(state.data, requiredKeys, requiredData); + }, + + // 添加子节点 addFieldAction: function(state, action, oldState) { const keys = action.prefix; let oldData = oldState.data; @@ -171,6 +280,8 @@ export default { parentKeys.push('required'); utils.setData(state.data, parentKeys, requiredData); }, + + // 添加子节点 addChildFieldAction: function(state, action, oldState) { const keys = action.key; let oldData = oldState.data;