Last active
October 16, 2020 22:45
-
-
Save adelin-b/f70e39c410529435f6f3a1cdfa9b5f41 to your computer and use it in GitHub Desktop.
Auto-generate forms from typescript interface
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ts-node | |
import * as TJS from 'typescript-json-schema' | |
import path from 'path' | |
import fs from 'fs' | |
try { | |
const filePath = path.join(__dirname, 'OptionsApp.tsx') | |
const outputPath = path.join(__dirname, 'form.json') | |
const program = TJS.getProgramFromFiles([filePath]) | |
const schema = TJS.generateSchema(program, 'OptionSettings', { ignoreErrors: true }) | |
fs.writeFileSync(outputPath, JSON.stringify(schema)) | |
console.log('Schema generated') | |
} catch (error) { | |
console.error('Schema not generated', error) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as React from 'react' | |
import styled from 'styled-components' | |
import schemaJson from './form.json' | |
// import Form from '@rjsf/material-ui' | |
import Form from '@rjsf/core' | |
// console.log('schema: ', schema) | |
export interface OptionSettings { | |
whitelist: string[] | |
/** | |
* @items {"type":"string","format":"email"} | |
* */ | |
blacklist: string[] | |
enabled: boolean | |
} | |
console.log('schemaJson: ', schemaJson) | |
const OptionsApp = () => ( | |
<OptionsAppContainer> | |
<Form | |
schema={schemaJson} | |
/> | |
</OptionsAppContainer> | |
) | |
export default OptionsApp | |
const OptionsAppContainer = styled('div')` | |
min-width: 400px; | |
padding: 10px; | |
box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19); | |
` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment