Skip to content

Commit 521a6f6

Browse files
schema form fix (#102)
Co-authored-by: 300074366 <[email protected]>
1 parent eac8772 commit 521a6f6

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

components/schema-form/src/json-schema-renderer.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export type UI = UIField | UIArrayField | UIObjectField
2626
export interface SchemaRendererOptions {
2727
resolveComponent(name: string): ComponentType
2828
resolveOptions(format: string, schema: any): Array<any> | void // TODO: Accept promise.
29+
optionsProcessed: boolean
2930
}
3031

3132
interface PropPostProcessor {
@@ -44,6 +45,7 @@ interface GeneratorContext {
4445
props: Record<string, any>
4546
layout: Record<string, any>
4647
}
48+
optionsProcessed: boolean
4749
}
4850

4951
export function createRenderFactory(
@@ -128,7 +130,8 @@ function generateAnyField(
128130
) {
129131
schema = normalizeSchema(schema)
130132

131-
if (schemaToUI.has(schema)) return schemaToUI.get(schema)
133+
if (schemaToUI.has(schema) && !context.optionsProcessed)
134+
return schemaToUI.get(schema)
132135

133136
const generator =
134137
'type' in schema

components/schema-form/src/schema-form.tsx

+13-9
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,12 @@ interface Props extends FormProps {
3232
*/
3333
export default class SchemaForm extends Component<Props, { ui: UI | null }> {
3434
optionsCache: Record<string, any[]>
35+
optionsPromise: Record<string, boolean>
3536

3637
constructor(props) {
3738
super(props)
38-
39+
this.optionsCache = {}
40+
this.optionsPromise = {}
3941
this.state = {
4042
ui: this.createUI(props.schema),
4143
}
@@ -54,10 +56,11 @@ export default class SchemaForm extends Component<Props, { ui: UI | null }> {
5456
console.log('Deleting SchemaForm')
5557
}
5658

57-
createUI(schema: Schema) {
59+
createUI(schema: Schema, optionsProcessed: boolean = false) {
5860
return createRenderFactory(schema, {
5961
resolveComponent: this.componentProvider,
6062
resolveOptions: this.optionsProvider,
63+
optionsProcessed,
6164
})
6265
}
6366

@@ -76,14 +79,15 @@ export default class SchemaForm extends Component<Props, { ui: UI | null }> {
7679
if (Array.isArray(result)) return result
7780
if (result === null) return
7881
if (result.then) {
79-
result.then((result) => {
80-
if (this.optionsCache[format] !== result) {
82+
if (!this.optionsPromise[format]) {
83+
result.then((result) => {
84+
this.optionsPromise[format] = true
8185
this.optionsCache[format] = result
82-
console.log('Option loaded.')
83-
this.forceUpdate() // Options Loaded.
84-
}
85-
})
86-
86+
this.setState({
87+
ui: this.createUI(this.props.schema, true),
88+
})
89+
})
90+
}
8791
return this.optionsCache[format] || []
8892
}
8993
}

0 commit comments

Comments
 (0)