|
| 1 | +import * as React from "react"; |
| 2 | + |
| 3 | +import strings from "ControlStrings"; |
| 4 | +import { useAtom } from "jotai"; |
| 5 | + |
| 6 | +import { |
| 7 | + Button, |
| 8 | + Image, |
| 9 | +} from "@fluentui/react-components"; |
| 10 | +import { |
| 11 | + Delete16Regular, |
| 12 | + Image20Regular, |
| 13 | +} from "@fluentui/react-icons"; |
| 14 | +import { BaseComponentContext } from "@microsoft/sp-component-base"; |
| 15 | + |
| 16 | +import { contextState } from "./atoms/contextState"; |
| 17 | +import { IFilePickerResult } from "./IFilePickerResult"; |
| 18 | +/* import { RenderSpinner } from "./RenderSpninner/RenderSpinner"; */ |
| 19 | +import { SelectFromSharePoint } from "./selectFromSharePoint"; |
| 20 | +import { useImagePickerStyles } from "./useImagePickerStyles"; |
| 21 | + |
| 22 | +export interface IImagePickerProps { |
| 23 | + onFileSelected: (file: IFilePickerResult) => void; |
| 24 | + onDeleteFile: () => void; |
| 25 | + selectedFileUrl: string; |
| 26 | + context: BaseComponentContext; |
| 27 | +} |
| 28 | + |
| 29 | +/** |
| 30 | + * Renders the preview image component. |
| 31 | + * |
| 32 | + * @param props - The component props. |
| 33 | + * @param props.selectedImageFileUrl - The URL of the selected image file. |
| 34 | + * @returns The JSX element representing the preview image component. |
| 35 | + */ |
| 36 | +const RenderPreviewImage = (props: { selectedImageFileUrl: string }): JSX.Element => { |
| 37 | + const { selectedImageFileUrl } = props; |
| 38 | + |
| 39 | + const maxWidth = 200; |
| 40 | + const maxHeight = 200; |
| 41 | + const styles = useImagePickerStyles(); |
| 42 | + |
| 43 | + if (!selectedImageFileUrl) { |
| 44 | + return null; |
| 45 | + } |
| 46 | + |
| 47 | + return ( |
| 48 | + <> |
| 49 | + <div className={styles.renderImageContainer}> |
| 50 | + <Image |
| 51 | + src={selectedImageFileUrl} |
| 52 | + fit="cover" |
| 53 | + style={{ minWidth: maxWidth, maxWidth: maxWidth, height: maxHeight }} |
| 54 | + alt="Selected Image" |
| 55 | + /> |
| 56 | + </div> |
| 57 | + </> |
| 58 | + ); |
| 59 | +}; |
| 60 | + |
| 61 | +/** |
| 62 | + * Renders an image picker component. |
| 63 | + * |
| 64 | + * @component |
| 65 | + * @example |
| 66 | + * ```tsx |
| 67 | + * <ImagePicker |
| 68 | + * onFileSelected={handleFileSelected} |
| 69 | + * onDeleteFile={handleDeleteFile} |
| 70 | + * selectedFileUrl={selectedImageUrl} |
| 71 | + * context={appContext} |
| 72 | + * /> |
| 73 | + * ``` |
| 74 | + */ |
| 75 | + |
| 76 | +export const ImagePicker: React.FunctionComponent<IImagePickerProps> = ( |
| 77 | + props: React.PropsWithChildren<IImagePickerProps> |
| 78 | +) => { |
| 79 | + const { onFileSelected, onDeleteFile, selectedFileUrl, context } = props; |
| 80 | + const [isOpen, setIsOpen] = React.useState<boolean>(false); |
| 81 | + const styles = useImagePickerStyles(); |
| 82 | + const ref = React.useRef<HTMLDivElement>(null); |
| 83 | + const [appContext, setAppContext] = useAtom(contextState); |
| 84 | + |
| 85 | + React.useEffect(() => { |
| 86 | + setAppContext({ |
| 87 | + ...appContext, |
| 88 | + context: context, |
| 89 | + }); |
| 90 | + }, []); |
| 91 | + |
| 92 | + const [selectedImageFileUrl, setSelectedImageFileUrl] = React.useState<string>(selectedFileUrl); |
| 93 | + |
| 94 | + const onDismiss = (): void => { |
| 95 | + setIsOpen(false); |
| 96 | + }; |
| 97 | + |
| 98 | + const isFileSelected = React.useMemo(() => { |
| 99 | + return !!selectedImageFileUrl; |
| 100 | + }, [selectedImageFileUrl]); |
| 101 | + |
| 102 | + const onDeleteFileCLick = React.useCallback(() => { |
| 103 | + setSelectedImageFileUrl(undefined); |
| 104 | + onDeleteFile(); |
| 105 | + }, []); |
| 106 | + |
| 107 | + const styleButtonDelete: React.CSSProperties = { display: !isFileSelected ? "none" : "inline-flex" }; |
| 108 | + |
| 109 | + if (!context) return null; |
| 110 | + |
| 111 | + return ( |
| 112 | + <> |
| 113 | + <div className={styles.root} ref={ref}> |
| 114 | + <div className={styles.buttonContainer}> |
| 115 | + <Button icon={<Image20Regular />} shape="circular" onClick={() => setIsOpen(true)}> |
| 116 | + {strings.ImagePickderSelectLabel} |
| 117 | + </Button> |
| 118 | + <Button |
| 119 | + icon={<Delete16Regular />} |
| 120 | + shape="circular" |
| 121 | + style={styleButtonDelete} |
| 122 | + onClick={() => onDeleteFileCLick()} |
| 123 | + > |
| 124 | + {strings.ImagePickerDeleteImageLabel} |
| 125 | + </Button> |
| 126 | + </div> |
| 127 | + <SelectFromSharePoint |
| 128 | + isOpen={isOpen} |
| 129 | + onDismiss={onDismiss} |
| 130 | + onFileSelected={async (file: IFilePickerResult) => { |
| 131 | + onFileSelected(file); |
| 132 | + setSelectedImageFileUrl(file.previewDataUrl); |
| 133 | + |
| 134 | + onDismiss(); |
| 135 | + }} |
| 136 | + /> |
| 137 | + {<RenderPreviewImage selectedImageFileUrl={selectedImageFileUrl} />} |
| 138 | + </div> |
| 139 | + </> |
| 140 | + ); |
| 141 | +}; |
0 commit comments