-
Notifications
You must be signed in to change notification settings - Fork 1
/
conditionalField.js
101 lines (82 loc) · 3.22 KB
/
conditionalField.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import PropTypes from "prop-types";
import React from "react";
import * as PathUtils from '@sanity/util/paths'
import { FormBuilderInput, withDocument, withValuePath } from "part:@sanity/form-builder";
/**
* ConditionalField input component will wrap any regular input field configured
* with `options.condition` in the schema.
*
* `option.condition` should be a function that accepts two parameters, `document`
* and `parent`, and returns `true` if the wrapped field should be shown.
*
* Inspired by the following code examples:
* - https://sanity-io-land.slack.com/archives/C9Z7RC3V1/p1583362492389300?thread_ts=1583335061.341000&cid=C9Z7RC3V1
* - https://github.com/sanity-io/sanity-recipes/blob/master/snippets/conditionalFieldsCustomInputComponent.js
*/
class ConditionalField extends React.Component {
// Declare shape of React properties
static propTypes = {
type: PropTypes.shape({
title: PropTypes.string,
options: PropTypes.shape({
condition: PropTypes.func.isRequired
}).isRequired
}).isRequired,
level: PropTypes.number,
focusPath: PropTypes.array,
onFocus: PropTypes.func.isRequired,
onChange: PropTypes.func.isRequired,
onBlur: PropTypes.func.isRequired
};
// Reference to input element
_inputElement = React.createRef();
// Called by the Sanity form-builder when this input should receive focus
focus() {
if (this._inputElement.current) {
this._inputElement.current.focus();
}
}
// Renders the form input if condition function returns true
render() {
const { document, getValuePath, value, level, focusPath, onFocus, onBlur, onChange } = this.props;
// Extract type without 'inputComponent' (self reference) to avoid infinite loop
const { inputComponent, ...type } = this.props.type;
// Find the active field's parent object in the document
const parentPath = getValuePath().slice(0, -1); // <- Remove current field from path
const parent = PathUtils.get(document, parentPath);
// Condition to evaluate if component should be shown, defaults to true
const defaultCondition = () => false;
const condition = (type.options && type.options.condition) || defaultCondition;
if (!condition(document, parent)) {
// Hide component
return null;
/**
* ----------------------------------------------------------------
* QUESTION
* How can we also delete the field value from the stored document?
* Running patch unset seems inefficient if it unsets the field
* every time the Studio UI is rendered, even though the stored
* document has not changed, but might be an acceptable solution.
* ----------------------------------------------------------------
*/
} else {
// Render component
return (
<div style={{ marginBottom: 20 }}>
<FormBuilderInput
level={level}
ref={this._inputElement}
type={type}
value={value}
onChange={onChange}
path={[]}
focusPath={focusPath}
onFocus={onFocus}
onBlur={onBlur}
/>
</div>
);
}
}
}
export default withValuePath(withDocument(ConditionalField));