Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix title #54

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
11 changes: 10 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@
"name": "react-jsonschema-form-pagination",
"description": "Extension of react-jsonschema-form with support for multi-nav forms",
"private": false,
"author": "[email protected]",
"authors": [
"[email protected]"
],
"contributors": [
{
"name": "Ayush",
"url": "https://github.com/Ayush1959"
}
],
"version": "0.4.1",
"scripts": {
"build:lib": "rimraf lib && cross-env NODE_ENV=production babel -d lib/ src/",
Expand All @@ -13,6 +21,7 @@
"dist": "npm run build:lib && npm run build:dist",
"lint": "eslint src test playground --fix",
"precommit": "lint-staged",
"prepare": "npm run build:lib",
"publish-to-gh-pages": "npm run build:playground && gh-pages --dist build/",
"publish-to-npm": "npm run dist && npm publish && npm version patch",
"start": "webpack-dev-server",
Expand Down
21 changes: 17 additions & 4 deletions src/render/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const COMPONENT_TYPES = {
integer: "NumberField",
number: "NumberField",
object: "ObjectField",
undefined: "StringField",
string: "StringField",
};

Expand All @@ -17,13 +18,20 @@ export function getFieldComponent(schema, uiSchema, fields) {
if (typeof field === "string" && field in fields) {
return fields[field];
}
const componentName = COMPONENT_TYPES[schema.type];
let componentName = COMPONENT_TYPES[schema.type];
if (
Array.isArray(schema.type) &&
schema.type.includes("string") &&
schema.type.includes("object")
) {
componentName = "StringField";
}
return componentName in fields
? fields[componentName]
: () => <h1>Unknown field type {schema.type}</h1>;
}

const REQUIRED_FIELD_SYMBOL = "*";
const REQUIRED_FIELD_SYMBOL = "* ";

function DefaultLabel(props) {
const { label, required, id } = props;
Expand All @@ -33,7 +41,10 @@ function DefaultLabel(props) {
}
return (
<label className="control-label" htmlFor={id}>
{required ? label + REQUIRED_FIELD_SYMBOL : label}
{required && (
<span style={{ color: "#ff4d4f" }}>{REQUIRED_FIELD_SYMBOL}</span>
)}
{label}
</label>
);
}
Expand All @@ -53,7 +64,9 @@ export function Label({
if (type === "boolean" && !uiSchema["ui:widget"]) {
displayLabel = false;
}

if (uiSchema["ui:widget"] && uiSchema["ui:widget"] === "hidden") {
displayLabel = false;
}
if (displayLabel) {
return (
<DefaultLabel label={label} required={required} id={idSchema["$id"]} />
Expand Down
4 changes: 3 additions & 1 deletion src/splitter/NavTree.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@ export default class NavTree {

extractSubUiSchema(fields, aliases, this.uiSchema, uiSchema, this.schema);

const firstInUi = activeNav[0] && uiSchema && uiSchema[activeNav[0]] && uiSchema[activeNav[0]]['ui:order'] && uiSchema[activeNav[0]]['ui:order'][0];
const firstField = firstInUi ? activeNav[0] + '.' + firstInUi : null;
if (navConfs.length > 0) {
asNavField(fields[0], navConfs, uiSchema);
asNavField(firstField || fields[0], navConfs, uiSchema);
}
navConfs = [];
}
Expand Down
11 changes: 8 additions & 3 deletions src/splitter/extractTree.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
toArray,
getNavAliases,
findFieldNavs,
UI_ORDER,
UI_ORDER
} from "../utils";

export function findRelTree(tree, navs) {
Expand All @@ -19,7 +19,7 @@ function pushField(tree, field, uiAlias) {
if (tree[GENERIC_NAV] === undefined) {
tree[GENERIC_NAV] = {
fields: [],
aliases: {},
aliases: {}
};
}
tree[GENERIC_NAV].fields.push(field);
Expand All @@ -33,7 +33,12 @@ function fillSchemaConf(tree, schema, uiSchema, prefix = "") {
const fieldSchema = schema.properties[field];
const fieldUiSchema = uiSchema[field];
if (fieldSchema.type === "object" && fieldUiSchema) {
fillSchemaConf(tree, fieldSchema, fieldUiSchema, field + ".");
fillSchemaConf(
tree,
fieldSchema,
fieldUiSchema,
prefix.length ? prefix + field + "." : field + "."
);
} else {
let navs = findFieldNavs(field, uiSchema);
let subTree = findRelTree(tree, navs);
Expand Down
13 changes: 11 additions & 2 deletions src/splitter/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export const asNavField = (field, navConfs, uiSchema) => {
uiSchema[field] = {
navConfs,
"ui:field": "nav",
origUiSchema: uiSchema[field],
origUiSchema: uiSchema[field]
};
} else {
const parentField = field.substr(0, separatorIndex);
Expand All @@ -17,13 +17,22 @@ export const asNavField = (field, navConfs, uiSchema) => {
function asHiddenField(field, uiSchema) {
uiSchema[field] = {
"ui:widget": "hidden",
"ui:field": "hidden",
"ui:field": "hidden"
};
}

export const toHiddenUiSchema = ({ properties }, uiSchema) => {
let cleanUiSchema = Object.keys(properties).reduce((agg, field) => {
asHiddenField(field, agg);
if (typeof properties[field] == "object") {
if ("properties" in properties[field]) {
Object.assign(
agg[field],
agg[field],
toHiddenUiSchema(properties[field], agg[field])
);
}
}
return agg;
}, Object.assign({}, uiSchema));
return cleanUiSchema;
Expand Down