Skip to content

Commit

Permalink
chore(form): rename function callConditionalProperty => resolveCondit…
Browse files Browse the repository at this point in the history
…ionalProperty
  • Loading branch information
bjoerge authored and hermanwikner committed Aug 11, 2022
1 parent 6741dab commit a7b66d7
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './callConditionalProperty'
export * from './resolveConditionalProperty'
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export interface ConditionalPropertyCallbackContext {
value: unknown
}

export function callConditionalProperty(
export function resolveConditionalProperty(
property: ConditionalProperty,
context: ConditionalPropertyCallbackContext
) {
Expand Down
52 changes: 34 additions & 18 deletions packages/sanity/src/form/store/formState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {FIXME} from '../types'
import {FormFieldPresence} from '../../presence'
import {isRecord} from '../../util'
import {PrimitiveFormNode, StateTree} from './types'
import {callConditionalProperty} from './conditional-property'
import {resolveConditionalProperty} from './conditional-property'
import {MAX_FIELD_DEPTH} from './constants'
import {getItemType, getPrimitiveItemType} from './utils/getItemType'
import {
Expand Down Expand Up @@ -268,7 +268,7 @@ function prepareFieldMember(props: {

const readOnly =
parent.readOnly ||
callConditionalProperty(field.type.readOnly, {
resolveConditionalProperty(field.type.readOnly, {
value: fieldValue,
parent: parent.value,
document: parent.document,
Expand Down Expand Up @@ -336,7 +336,7 @@ function prepareFieldMember(props: {

const readOnly =
parent.readOnly ||
callConditionalProperty(field.type.readOnly, {
resolveConditionalProperty(field.type.readOnly, {
value: fieldValue,
parent: parent.value,
document: parent.document,
Expand Down Expand Up @@ -390,22 +390,22 @@ function prepareFieldMember(props: {
? parentComparisonValue?.[field.name]
: undefined

const conditionalFieldContext = {
const conditionalPropertyContext = {
value: fieldValue,
parent: parent.value,
document: parent.document,
currentUser: parent.currentUser,
}

// note: we *only* want to call the conditional props here, as it's handled by the prepare<Object|Array>InputProps otherwise
const hidden = callConditionalProperty(field.type.hidden, conditionalFieldContext)
const hidden = resolveConditionalProperty(field.type.hidden, conditionalPropertyContext)

if (hidden) {
return null
}

const readOnly =
parent.readOnly || callConditionalProperty(field.type.hidden, conditionalFieldContext)
parent.readOnly || resolveConditionalProperty(field.type.readOnly, conditionalPropertyContext)

const fieldState = preparePrimitiveInputState({
...parent,
Expand Down Expand Up @@ -470,28 +470,30 @@ function prepareObjectInputState<T>(
return null
}

const conditionalFieldContext = {
const conditionalPropertyContext = {
value: props.value,
parent: props.parent,
document: props.document,
currentUser: props.currentUser,
}

const hidden =
enableHiddenCheck && callConditionalProperty(props.schemaType.hidden, conditionalFieldContext)
enableHiddenCheck &&
resolveConditionalProperty(props.schemaType.hidden, conditionalPropertyContext)

if (hidden) {
return null
}

const readOnly =
props.readOnly || callConditionalProperty(props.schemaType.readOnly, conditionalFieldContext)
props.readOnly ||
resolveConditionalProperty(props.schemaType.readOnly, conditionalPropertyContext)

const schemaTypeGroupConfig = props.schemaType.groups || []
const defaultGroupName = (schemaTypeGroupConfig.find((g) => g.default) || ALL_FIELDS_GROUP)?.name

const groups = [ALL_FIELDS_GROUP, ...schemaTypeGroupConfig].flatMap((group): FormFieldGroup[] => {
const groupHidden = callConditionalProperty(group.hidden, conditionalFieldContext)
const groupHidden = resolveConditionalProperty(group.hidden, conditionalPropertyContext)
const selected = group.name === (props.fieldGroupState?.value || defaultGroupName)
return groupHidden
? []
Expand Down Expand Up @@ -538,7 +540,7 @@ function prepareObjectInputState<T>(

// it's an actual fieldset
const fieldsetFieldNames = fieldSet.fields.map((f) => f.name)
const fieldsetHidden = callConditionalProperty(fieldSet.hidden, {
const fieldsetHidden = resolveConditionalProperty(fieldSet.hidden, {
currentUser: props.currentUser,
document: props.document,
parent: props.value,
Expand Down Expand Up @@ -629,22 +631,23 @@ function prepareArrayOfPrimitivesInputState<T extends (boolean | string | number
return null
}

const conditionalFieldContext = {
const conditionalPropertyContext = {
comparisonValue: props.comparisonValue,
value: props.value,
parent: props.parent,
document: props.document,
currentUser: props.currentUser,
}

const hidden = callConditionalProperty(props.schemaType.hidden, conditionalFieldContext)
const hidden = resolveConditionalProperty(props.schemaType.hidden, conditionalPropertyContext)

if (hidden) {
return null
}

const readOnly =
props.readOnly || callConditionalProperty(props.schemaType.readOnly, conditionalFieldContext)
props.readOnly ||
resolveConditionalProperty(props.schemaType.readOnly, conditionalPropertyContext)

// Todo: improve error handling at the parent level so that the value here is either undefined or an array
const items = Array.isArray(props.value) ? props.value : []
Expand Down Expand Up @@ -679,19 +682,21 @@ function prepareArrayOfObjectsInputState<T extends {_key: string}[]>(
return null
}

const conditionalFieldContext = {
const conditionalPropertyContext = {
value: props.value,
parent: props.parent,
document: props.document,
currentUser: props.currentUser,
}
const hidden = callConditionalProperty(props.schemaType.hidden, conditionalFieldContext)
const hidden = resolveConditionalProperty(props.schemaType.hidden, conditionalPropertyContext)

if (hidden) {
return null
}

const readOnly =
props.readOnly || callConditionalProperty(props.schemaType.readOnly, conditionalFieldContext)
props.readOnly ||
resolveConditionalProperty(props.schemaType.readOnly, conditionalPropertyContext)

// Todo: improve error handling at the parent level so that the value here is either undefined or an array
const items = Array.isArray(props.value) ? props.value : []
Expand Down Expand Up @@ -757,6 +762,16 @@ function prepareArrayOfObjectsMember(props: {
const itemPath = pathFor([...parent.path, {_key: key}])
const itemLevel = parent.level + 1

const conditionalPropertyContext = {
value: parent.value,
parent: props.parent,
document: parent.document,
currentUser: parent.currentUser,
}
const readOnly =
parent.readOnly ||
resolveConditionalProperty(parent.schemaType.readOnly, conditionalPropertyContext)

const collapsedItemPaths = parent.collapsedPaths?.children?.[key]
const comparisonValue =
(Array.isArray(parent.comparisonValue) &&
Expand All @@ -777,6 +792,7 @@ function prepareArrayOfObjectsMember(props: {
collapsedPaths: collapsedItemPaths,
presence: parent.presence,
validation: parent.validation,
readOnly,
},
false
)
Expand Down Expand Up @@ -835,7 +851,7 @@ function prepareArrayOfPrimitivesMember(props: {

const readOnly =
parent.readOnly ||
callConditionalProperty(itemType.readOnly, {
resolveConditionalProperty(itemType.readOnly, {
value: itemValue,
parent: parent.value,
document: parent.document,
Expand Down

0 comments on commit a7b66d7

Please sign in to comment.