Skip to content

Commit

Permalink
[field] Fix diff/path/annotation passed down to DOM element
Browse files Browse the repository at this point in the history
  • Loading branch information
rexxars committed Oct 6, 2020
1 parent a882179 commit 1dcf933
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions packages/@sanity/field/src/diff/annotations/DiffAnnotationCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@ import React, {createElement} from 'react'
import {useAnnotationColor} from './hooks'
import {AnnotationProps, AnnotatedDiffProps} from './DiffAnnotation'
import {getAnnotationAtPath} from './helpers'
import {Diff, Annotation} from '../../types'
import {Path} from '../../paths'

interface BaseDiffAnnotationCardProps {
as?: React.ElementType | keyof JSX.IntrinsicElements
diff?: Diff
path?: Path | string
annotation?: Annotation
}

export type DiffAnnotationCardProps = (AnnotationProps | AnnotatedDiffProps) &
Expand All @@ -13,10 +18,17 @@ export type DiffAnnotationCardProps = (AnnotationProps | AnnotatedDiffProps) &
export function DiffAnnotationCard(
props: DiffAnnotationCardProps & React.HTMLProps<HTMLDivElement>
): React.ReactElement {
const {as = 'div', children, style = {}, ...restProps} = props
const annotation =
'diff' in props ? getAnnotationAtPath(props.diff, props.path || []) : props.annotation
const {
as = 'div',
children,
path,
diff,
annotation: specifiedAnnotation,
style = {},
...restProps
} = props

const annotation = diff ? getAnnotationAtPath(diff, path || []) : specifiedAnnotation
const color = useAnnotationColor(annotation)
const colorStyle = color ? {background: color.background, color: color.text} : {}
return createElement(as, {...restProps, style: {...colorStyle, ...style}}, children)
Expand Down

0 comments on commit 1dcf933

Please sign in to comment.