-
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.
[components] Refactor and improve validation components
- Loading branch information
1 parent
352d6f1
commit 5ec8e03
Showing
9 changed files
with
281 additions
and
308 deletions.
There are no files selected for viewing
142 changes: 69 additions & 73 deletions
142
packages/@sanity/components/src/validation/ValidationList.tsx
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,94 +1,90 @@ | ||
import React from 'react' | ||
import {Path} from '@sanity/types' | ||
import React, {useCallback} from 'react' | ||
import {ObjectSchemaType, Path} from '@sanity/types' | ||
import {Marker} from '../types' | ||
import ValidationListItem from './ValidationListItem' | ||
|
||
import styles from './ValidationList.css' | ||
|
||
type Props = { | ||
kind?: string | ||
interface ValidationListProps { | ||
documentType?: ObjectSchemaType | ||
kind?: 'simple' | ||
markers: Marker[] | ||
onFocus?: (path: Path) => void | ||
onClose?: () => void | ||
showLink?: boolean | ||
// showLink?: boolean | ||
truncate?: boolean | ||
documentType?: { | ||
fields: { | ||
name: string | ||
type: { | ||
title: string | ||
} | ||
}[] | ||
} | ||
markers: Marker[] | ||
} | ||
|
||
// @todo: refactor to functional component | ||
export default class ValidationList extends React.PureComponent<Props> { | ||
static defaultProps = { | ||
markers: [], | ||
documentType: null, | ||
onClose: () => undefined, | ||
showLink: false, | ||
onFocus: () => undefined | ||
} | ||
function ValidationList(props: ValidationListProps) { | ||
const { | ||
documentType, | ||
kind, | ||
markers, | ||
onFocus, | ||
onClose, | ||
truncate | ||
// showLink, | ||
} = props | ||
|
||
handleClick = (path: Path = []) => { | ||
const {onFocus, onClose} = this.props | ||
const validation = markers.filter(marker => marker.type === 'validation') | ||
const errors = validation.filter(marker => marker.level === 'error') | ||
const warnings = validation.filter(marker => marker.level === 'warning') | ||
|
||
if (onFocus) onFocus(path) | ||
if (onClose) onClose() | ||
} | ||
const handleClick = useCallback( | ||
(path: Path = []) => { | ||
if (onFocus) onFocus(path) | ||
if (onClose) onClose() | ||
}, | ||
[onFocus, onClose] | ||
) | ||
|
||
resolvePathTitle(path: Path) { | ||
const type = this.props.documentType | ||
const fields = type && type.fields | ||
const resolvePathTitle = (path: Path) => { | ||
const fields = documentType && documentType.fields | ||
const field = fields && fields.find(curr => curr.name === path[0]) | ||
|
||
return field ? field.type.title : '' | ||
return (field && field.type.title) || '' | ||
} | ||
|
||
render() { | ||
const {kind, markers, showLink, truncate} = this.props | ||
const validation = markers.filter(marker => marker.type === 'validation') | ||
const errors = validation.filter(marker => marker.level === 'error') | ||
const warnings = validation.filter(marker => marker.level === 'warning') | ||
const hasErrors = errors.length > 0 | ||
const hasWarnings = warnings.length > 0 | ||
|
||
if (errors.length === 0 && warnings.length === 0) { | ||
return <div /> | ||
} | ||
if (!hasErrors && !hasWarnings) { | ||
return null | ||
} | ||
|
||
return ( | ||
<ul className={styles.root} data-kind={kind}> | ||
{errors.length > 0 && | ||
errors.map((error, i) => ( | ||
// eslint-disable-next-line react/no-array-index-key | ||
<li className={styles.item} key={i}> | ||
<ValidationListItem | ||
kind={kind} | ||
truncate={truncate} | ||
path={this.resolvePathTitle(error.path)} | ||
marker={error} | ||
onClick={this.handleClick} | ||
showLink={showLink} | ||
/> | ||
</li> | ||
))} | ||
return ( | ||
<ul className={styles.root} data-kind={kind}> | ||
{hasErrors && | ||
errors.map((error, i) => ( | ||
// eslint-disable-next-line react/no-array-index-key | ||
<li className={styles.item} key={i}> | ||
<ValidationListItem | ||
kind={kind} | ||
truncate={truncate} | ||
path={resolvePathTitle(error.path)} | ||
marker={error} | ||
onClick={handleClick} | ||
// showLink={showLink} | ||
/> | ||
</li> | ||
))} | ||
|
||
{warnings.length > 0 && | ||
warnings.map((warning, i) => ( | ||
// eslint-disable-next-line react/no-array-index-key | ||
<li className={styles.item} key={i}> | ||
<ValidationListItem | ||
kind={kind} | ||
truncate={truncate} | ||
path={this.resolvePathTitle(warning.path)} | ||
marker={warning} | ||
onClick={this.handleClick} | ||
showLink={showLink} | ||
/> | ||
</li> | ||
))} | ||
</ul> | ||
) | ||
} | ||
{hasWarnings && | ||
warnings.map((warning, i) => ( | ||
// eslint-disable-next-line react/no-array-index-key | ||
<li className={styles.item} key={i}> | ||
<ValidationListItem | ||
kind={kind} | ||
truncate={truncate} | ||
path={resolvePathTitle(warning.path)} | ||
marker={warning} | ||
onClick={handleClick} | ||
// showLink={showLink} | ||
/> | ||
</li> | ||
))} | ||
</ul> | ||
) | ||
} | ||
|
||
export default ValidationList |
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
Oops, something went wrong.