Open-Federation / json-schema-editor-visual

A json-schema editor of high efficient and easy-to-use, base on React.
https://hellosean1025.github.io/json-schema-visual-editor/
MIT License
997 stars 220 forks source link

创建同名字兄弟节点覆盖 #87

Open chengandguo opened 6 months ago

chengandguo commented 6 months ago

如果子节点包含field2, 创建兄弟节点需要从3开始 `一种可能得修复方法 getRandomName(propertiesData) { const currentField = "field" + fieldNum++; for (const key in propertiesData) { if (key === currentField) { return this.getRandomName(propertiesData); } } return currentField; },

addFieldAction: function (state, action, oldState) { const keys = action.prefix; let oldData = oldState.data; let name = action.name; let propertiesData = utils.getData(oldData, keys); let newPropertiesData = {};

let parentKeys = utils.getParentKeys(keys);
let parentData = utils.getData(oldData, parentKeys);
let requiredData = [].concat(parentData.required || []);

if (!name) {
  newPropertiesData = Object.assign({}, propertiesData);
  let ranName = "field_" + fieldNum++;
  newPropertiesData[ranName] = utils.defaultSchema.string;
  requiredData.push(ranName);
} else {
  for (let i in propertiesData) {
    newPropertiesData[i] = propertiesData[i];
    if (i === name) {
      let ranName = this.getRandomName(propertiesData);   // 不能简单自增需要判断
      newPropertiesData[ranName] = utils.defaultSchema.string;
      requiredData.push(ranName);
    }
  }
}
utils.setData(state.data, keys, newPropertiesData);
// add required
parentKeys.push("required");
utils.setData(state.data, parentKeys, requiredData);

} `