Skip to content

Commit

Permalink
schema form fix
Browse files Browse the repository at this point in the history
  • Loading branch information
airishmyntra committed Jan 31, 2024
1 parent eac8772 commit a2253e8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
5 changes: 4 additions & 1 deletion components/schema-form/src/json-schema-renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export type UI = UIField | UIArrayField | UIObjectField
export interface SchemaRendererOptions {
resolveComponent(name: string): ComponentType
resolveOptions(format: string, schema: any): Array<any> | void // TODO: Accept promise.
optionsProcessed: boolean
}

interface PropPostProcessor {
Expand All @@ -44,6 +45,7 @@ interface GeneratorContext {
props: Record<string, any>
layout: Record<string, any>
}
optionsProcessed: boolean
}

export function createRenderFactory(
Expand Down Expand Up @@ -128,7 +130,8 @@ function generateAnyField(
) {
schema = normalizeSchema(schema)

if (schemaToUI.has(schema)) return schemaToUI.get(schema)
if (schemaToUI.has(schema) && !context.optionsProcessed)
return schemaToUI.get(schema)

const generator =
'type' in schema
Expand Down
22 changes: 13 additions & 9 deletions components/schema-form/src/schema-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@ interface Props extends FormProps {
*/
export default class SchemaForm extends Component<Props, { ui: UI | null }> {
optionsCache: Record<string, any[]>
optionsPromise: Record<string, boolean>

constructor(props) {
super(props)

this.optionsCache = {}
this.optionsPromise = {}
this.state = {
ui: this.createUI(props.schema),
}
Expand All @@ -54,10 +56,11 @@ export default class SchemaForm extends Component<Props, { ui: UI | null }> {
console.log('Deleting SchemaForm')
}

createUI(schema: Schema) {
createUI(schema: Schema, optionsProcessed: boolean = false) {
return createRenderFactory(schema, {
resolveComponent: this.componentProvider,
resolveOptions: this.optionsProvider,
optionsProcessed,
})
}

Expand All @@ -76,14 +79,15 @@ export default class SchemaForm extends Component<Props, { ui: UI | null }> {
if (Array.isArray(result)) return result
if (result === null) return
if (result.then) {
result.then((result) => {
if (this.optionsCache[format] !== result) {
if (!this.optionsPromise[format]) {
result.then((result) => {
this.optionsPromise[format] = true
this.optionsCache[format] = result
console.log('Option loaded.')
this.forceUpdate() // Options Loaded.
}
})

this.setState({
ui: this.createUI(this.props.schema, true),
})
})
}
return this.optionsCache[format] || []
}
}
Expand Down

0 comments on commit a2253e8

Please sign in to comment.