Skip to content

Instantly share code, notes, and snippets.

@ljmotta
Last active June 28, 2021 19:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ljmotta/a608e730621e959f1550df58981225ae to your computer and use it in GitHub Desktop.
Save ljmotta/a608e730621e959f1550df58981225ae to your computer and use it in GitHub Desktop.
Uniforms version 3.0.0 bug using `autoField` props
{
"name": "newapp",
"version": "0.1.0",
"private": true,
"dependencies": {
"@rjsf/core": "^2.4.1",
"@testing-library/jest-dom": "^5.11.4",
"@testing-library/react": "^11.1.0",
"@testing-library/user-event": "^12.1.10",
"ajv": "^7.0.3",
"bootstrap": "4.6.0",
"react": "16.13.1",
"react-bootstrap": "^1.6.1",
"react-dom": "16.13.1",
"react-scripts": "4.0.1",
"uniforms": "3.0.0",
"uniforms-bootstrap4": "3.0.0",
"uniforms-bridge-json-schema": "3.0.0",
"web-vitals": "^0.2.4"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
import './App.css';
import React from "react"
import Ajv from 'ajv';
import { JSONSchemaBridge } from 'uniforms-bridge-json-schema';
import { AutoForm, AutoField } from 'uniforms-bootstrap4';
import { connectField } from 'uniforms';
const schema = {
type: 'object',
properties: {
test: {
type: 'string',
format: 'days and time duration'
}
},
phases: ['complete', 'release']
};
const ajv = new Ajv({ allErrors: true, useDefaults: true, strict: false });
function createValidator(schema) {
const validator = ajv.compile(schema);
return (model) => {
validator(model);
return validator.errors?.length ? { details: validator.errors } : null;
};
}
const schemaValidator = createValidator(schema);
const bridge = new JSONSchemaBridge(schema, schemaValidator);
export function App() {
const [model, setModel] = React.useState();
const onSubmit = React.useCallback((model) => {
setModel(model)
}, [])
const onValidate = React.useCallback((model, error) => {
return error;
}, [])
return (
<div style={{ margin: "20px", width: "500px"}}>
<AutoForm
// model={model}
showInlineError={true}
schema={bridge}
onSubmit={onSubmit}
onValidate={onValidate}
placeholder={true}
autoField={MyAutoField}
/>
<div>
<code>{`const model = ${JSON.stringify(model, null, 2)};`}</code>
</div>
</div>
);
}
export function DaysAndTimeDuration() {
return <text>aaa</text>
}
function MyAuto(props) {
const determineComponent = (props) => {
switch (props.field.format) {
case "days and time duration":
return DaysAndTimeDuration;
default:
return AutoField;
}
};
const Component = determineComponent(props);
return <Component {...props} name={""} />;
}
export const MyAutoField = connectField(MyAuto, {
initialValue: false
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment