-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: slightly change API, disable strictNullChecks
- Loading branch information
Showing
7 changed files
with
83 additions
and
60 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
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
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,39 +1,43 @@ | ||
import { useRef, useCallback, useContext, useLayoutEffect, FC } from 'react' | ||
import PropTypes, { InferProps } from 'prop-types' | ||
import { ItemRef, ThumbRef } from './types' | ||
import { ItemRef, ThumbnailRef } from './types' | ||
import { Context } from './context' | ||
|
||
const propTypes = { | ||
full: PropTypes.string.isRequired, | ||
thumb: PropTypes.string, | ||
original: PropTypes.string.isRequired, | ||
thumbnail: PropTypes.string, | ||
width: PropTypes.oneOfType([PropTypes.number, PropTypes.string]).isRequired, | ||
height: PropTypes.oneOfType([PropTypes.number, PropTypes.string]).isRequired, | ||
title: PropTypes.string, | ||
children: PropTypes.func.isRequired, | ||
} | ||
|
||
const defaultProps = { | ||
thumb: null, | ||
title: null, | ||
export interface ItemProps | ||
extends Omit<InferProps<typeof propTypes>, 'children'> { | ||
children: (props: { | ||
open: () => void | ||
thumbnailRef: ThumbnailRef | ||
}) => JSX.Element | ||
} | ||
|
||
export type ItemProps = InferProps<typeof propTypes> | ||
|
||
export const Item: FC<ItemProps> = ({ children, ...restProps }) => { | ||
const ref: ItemRef = useRef() | ||
const thumbRef: ThumbRef = useRef(null) | ||
const thumbnailRef: ThumbnailRef = useRef() | ||
const { remove, update, handleClick } = useContext(Context) | ||
const open = useCallback(() => handleClick(ref), []) | ||
|
||
useLayoutEffect(() => { | ||
remove(ref) | ||
update(ref, { thumbRef, ...restProps }) | ||
update(ref, { thumbnailRef, ...restProps }) | ||
return () => remove(ref) | ||
}) | ||
|
||
return children({ open, thumbRef }) | ||
return children({ open, thumbnailRef }) | ||
} | ||
|
||
Item.propTypes = propTypes | ||
|
||
Item.defaultProps = defaultProps | ||
Item.defaultProps = { | ||
thumbnail: null, | ||
title: null, | ||
} |
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
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
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
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