-
Notifications
You must be signed in to change notification settings - Fork 426
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[form-builder] Remove createFormBuilderFactory (#151)
- Loading branch information
Showing
14 changed files
with
234 additions
and
187 deletions.
There are no files selected for viewing
4 changes: 2 additions & 2 deletions
4
packages/@sanity/form-builder/docs/components/form-builder.md
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
# The FormBuilder Component | ||
|
||
The `FormBuilder` component returned by `createFormBuilder(...)` is responsible for rendering the generated form and takes the following props: | ||
The `FormBuilder` component is responsible for rendering the generated form and takes the following props: | ||
|
||
## Props | ||
- **`value`** - required. The current form builder value. It should always be an instance of FormBuilderValue | ||
- **`onChange`** - required. This is the function that will be called whenever a change happens. The change must be applied for it to update the form field. If the change is not applied the form will effectively be read-only. | ||
- **`validation`** - optional. An instance of `ValidationResult` | ||
- **`validation`** - optional. An instance of `ValidationResult` |
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import PropTypes from 'prop-types' | ||
import React from 'react' | ||
import {FormBuilderInput} from './FormBuilderInput' | ||
import * as defaultConfig from './defaultConfig' | ||
import Schema from '@sanity/schema' | ||
import pubsub from 'nano-pubsub' | ||
|
||
const NOOP = () => {} | ||
|
||
function resolve(type, providedResolve = NOOP, defaultResolve = NOOP) { | ||
let itType = type | ||
while (itType) { | ||
const resolved = providedResolve(itType) || defaultResolve(itType) | ||
if (resolved) { | ||
return resolved | ||
} | ||
itType = itType.type | ||
} | ||
return undefined | ||
} | ||
|
||
export default class FormBuilder extends React.Component { | ||
static createPatchChannel = () => { | ||
const channel = pubsub() | ||
return {receivePatches: channel.publish} | ||
} | ||
static propTypes = { | ||
value: PropTypes.any, | ||
schema: PropTypes.instanceOf(Schema).isRequired, | ||
type: PropTypes.object, | ||
children: PropTypes.any, | ||
onChange: PropTypes.func, | ||
patchChannel: PropTypes.shape({ | ||
onPatch: PropTypes.func | ||
}), | ||
resolveInputComponent: PropTypes.func, | ||
resolvePreviewComponent: PropTypes.func | ||
} | ||
|
||
static childContextTypes = { | ||
getValuePath: PropTypes.func, | ||
onPatch: PropTypes.func, | ||
formBuilder: PropTypes.shape({ | ||
schema: PropTypes.instanceOf(Schema), | ||
resolveInputComponent: PropTypes.func, | ||
document: PropTypes.any | ||
}) | ||
} | ||
|
||
getDocument = () => { | ||
return this.props.value | ||
} | ||
|
||
resolveInputComponent = type => { | ||
const {resolveInputComponent} = this.props | ||
return resolve(type, resolveInputComponent, defaultConfig.resolveInputComponent) | ||
|| defaultConfig.jsonTypeFallbacks[type.jsonType] | ||
} | ||
|
||
resolvePreviewComponent = type => { | ||
const {resolvePreviewComponent} = this.props | ||
|
||
return resolve(type, resolvePreviewComponent, defaultConfig.resolvePreviewComponent) | ||
} | ||
|
||
getChildContext() { | ||
const {schema, patchChannel} = this.props | ||
return { | ||
getValuePath: () => ([]), | ||
formBuilder: { | ||
onPatch: patchChannel ? patchChannel.subscribe : () => { | ||
// eslint-disable-next-line no-console | ||
console.log('No patch channel provided to form-builder. If you need input based patch updates, please provide one') | ||
}, | ||
schema: schema, | ||
resolveInputComponent: this.resolveInputComponent, | ||
resolvePreviewComponent: this.resolvePreviewComponent, | ||
getDocument: this.getDocument | ||
} | ||
} | ||
} | ||
|
||
render() { | ||
const {schema, value, type, onChange} = this.props | ||
|
||
if (!schema) { | ||
throw new TypeError('You must provide a schema to <FormBuilder (...)') | ||
} | ||
|
||
return ( | ||
<FormBuilderInput | ||
value={value} | ||
type={type} | ||
onChange={onChange} | ||
level={0} | ||
isRoot | ||
autoFocus | ||
/> | ||
) | ||
} | ||
} |
Oops, something went wrong.