-
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.
feat(form): add helper function to get correct field level in `inputR…
…esolver`
- Loading branch information
1 parent
c66d937
commit 86f8b48
Showing
2 changed files
with
41 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,38 @@ | ||
import {SchemaType} from '@sanity/types' | ||
import {get} from 'lodash' | ||
import {ArrayFieldProps, ObjectFieldProps} from '../../types' | ||
|
||
export function getOption(type: SchemaType, optionName: string) { | ||
return get(type.options, optionName) | ||
} | ||
|
||
const PSEUDO_OBJECTS = ['array', 'file', 'image', 'reference'] | ||
const HIDDEN_FIELDS = ['asset', 'crop', 'hotspot', '_ref', '_weak'] | ||
|
||
export function getObjectFieldLevel(field: ObjectFieldProps): number { | ||
const {type, fields, options} = field.schemaType | ||
const fieldType = type?.name || '' | ||
|
||
const isPseudoObject = PSEUDO_OBJECTS.includes(fieldType) | ||
|
||
const hasVisibleFields = fields?.filter((f) => !HIDDEN_FIELDS.includes(f.name)).length > 0 | ||
const hasListOptions = options?.list?.length > 0 | ||
|
||
if (hasVisibleFields || hasListOptions || !isPseudoObject) { | ||
return field.level | ||
} | ||
|
||
return 0 | ||
} | ||
|
||
export function getArrayFieldLevel(field: ArrayFieldProps): number { | ||
const {options} = field.schemaType | ||
|
||
const hasListOptions = (options?.list || [])?.length > 0 | ||
|
||
if (hasListOptions) { | ||
return field.level | ||
} | ||
|
||
return 0 | ||
} |
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