-
Notifications
You must be signed in to change notification settings - Fork 394
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(form): simplify and optimize conditional callback resolution a …
…little bit
- Loading branch information
1 parent
fb78bb7
commit 6741dab
Showing
5 changed files
with
80 additions
and
110 deletions.
There are no files selected for viewing
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
29 changes: 29 additions & 0 deletions
29
packages/sanity/src/form/store/conditional-property/callConditionalProperty.ts
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,29 @@ | ||
/* eslint-disable no-nested-ternary */ | ||
import {ConditionalProperty, CurrentUser} from '@sanity/types' | ||
|
||
export interface ConditionalPropertyCallbackContext { | ||
parent?: unknown | ||
document?: Record<string, unknown> | ||
currentUser: Omit<CurrentUser, 'role'> | null | ||
value: unknown | ||
} | ||
|
||
export function callConditionalProperty( | ||
property: ConditionalProperty, | ||
context: ConditionalPropertyCallbackContext | ||
) { | ||
const {currentUser, document, parent, value} = context | ||
|
||
if (typeof property === 'boolean' || property === undefined) { | ||
return Boolean(property) | ||
} | ||
|
||
return ( | ||
property({ | ||
document: document as any, | ||
parent, | ||
value, | ||
currentUser, | ||
}) === true // note: we can't strictly "trust" the return value here, so the conditional property should probably be typed as unknown | ||
) | ||
} |
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 +1 @@ | ||
export * from './resolveConditionalProperties' | ||
export * from './callConditionalProperty' |
57 changes: 0 additions & 57 deletions
57
packages/sanity/src/form/store/conditional-property/resolveConditionalProperties.ts
This file was deleted.
Oops, something went wrong.
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