diff --git a/README.md b/README.md index d0238ae..db0d896 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,12 @@ considerations. ## Quick start ### From NPM: -1. Install the package: `npm install @uploadcare/react-uploader` +1. Install the package: + +```bash +npm install @uploadcare/react-uploader +``` + 2. Connect React Uploader from your script file: ```jsx import { FileUploaderRegular } from "@uploadcare/react-uploader"; @@ -49,7 +54,7 @@ We provide a full set of props that are used in File Uploader. For review we sug For convenience, we provide the ability to access the File Uploader API using `apiRef`. You can see what methods are available in `apiRef` in the [documentation][uc-docs-file-uploader-api]. -It is important to note that you need to use `InstanceType` utility type to define `apiRef` type. +It is important to note that we now pass all InstanceType from UploadCtxProvider. ```jsx import React, {useRef, useEffect} from "react"; @@ -60,9 +65,9 @@ import { import "@uploadcare/react-uploader/core.css"; const Example = () => { - const uploaderRef = useRef | null>(null); + const uploaderRef = useRef | null>(null); - ; + ; } ``` @@ -78,22 +83,27 @@ import { FileUploaderRegular } from "@uploadcare/react-uploader"; import "@uploadcare/react-uploader/core.css"; { - console.log('modal-open') - }} + pubkey="YOUR_PUBLIC_KEY" + onModalOpen={() => { + console.log('modal-open') + }} /> ``` - ## Styles You can customize the appearance of the React Uploader by using the `className` prop, which lets you apply custom CSS classes to the `FileUploader` wrapper, including the `Regular`, `Minimal`, and `Inline` variations. +However, if you need to assign a class specifically to the uploader file. Use the `classNameUploader` prop. + ```jsx import { FileUploaderRegular } from "@uploadcare/react-uploader"; import "@uploadcare/react-uploader/core.css"; -; +; ``` ```css diff --git a/package-lock.json b/package-lock.json index 030d1ca..a68c839 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2476,9 +2476,9 @@ } }, "node_modules/@uploadcare/file-uploader": { - "version": "1.2.0", - "resolved": "https://registry.npmjs.org/@uploadcare/file-uploader/-/file-uploader-1.2.0.tgz", - "integrity": "sha512-ME+nzhOcP+SWr6kIqLp/PjfzYyaD5gCM+pNxPxtDJaPh2ncgptZ+CMm5PU/TkcjNB0LJjTO5m5S9CgD2NyGJmg==", + "version": "1.9.0", + "resolved": "https://registry.npmjs.org/@uploadcare/file-uploader/-/file-uploader-1.9.0.tgz", + "integrity": "sha512-jmdwrfbmVERctmslqitRKfeMvJhJ2a7RbumsvdjqOl5jbXyAYH7YFCQS0FaomLnlWwFzXwtHe4vz7w+AIDleJQ==", "dependencies": { "@symbiotejs/symbiote": "^1.11.7", "@uploadcare/image-shrink": "^6.14.1", @@ -11283,7 +11283,7 @@ "version": "1.1.0", "license": "MIT", "dependencies": { - "@uploadcare/file-uploader": "^1.2.0", + "@uploadcare/file-uploader": "^1.9.0", "@uploadcare/react-adapter": "^0.3.0" }, "peerDependencies": { diff --git a/packages/react-uploader/README.md b/packages/react-uploader/README.md index 7858b51..db0d896 100644 --- a/packages/react-uploader/README.md +++ b/packages/react-uploader/README.md @@ -32,7 +32,12 @@ considerations. ## Quick start ### From NPM: -1. Install the package: `npm install @uploadcare/react-uploader` +1. Install the package: + +```bash +npm install @uploadcare/react-uploader +``` + 2. Connect React Uploader from your script file: ```jsx import { FileUploaderRegular } from "@uploadcare/react-uploader"; @@ -60,9 +65,9 @@ import { import "@uploadcare/react-uploader/core.css"; const Example = () => { - const uploaderRef = useRef | null>(null); + const uploaderRef = useRef | null>(null); - ; + ; } ``` @@ -78,22 +83,27 @@ import { FileUploaderRegular } from "@uploadcare/react-uploader"; import "@uploadcare/react-uploader/core.css"; { - console.log('modal-open') - }} + pubkey="YOUR_PUBLIC_KEY" + onModalOpen={() => { + console.log('modal-open') + }} /> ``` - ## Styles You can customize the appearance of the React Uploader by using the `className` prop, which lets you apply custom CSS classes to the `FileUploader` wrapper, including the `Regular`, `Minimal`, and `Inline` variations. +However, if you need to assign a class specifically to the uploader file. Use the `classNameUploader` prop. + ```jsx import { FileUploaderRegular } from "@uploadcare/react-uploader"; import "@uploadcare/react-uploader/core.css"; -; +; ``` ```css diff --git a/packages/react-uploader/package.json b/packages/react-uploader/package.json index c3d141b..569c6ef 100644 --- a/packages/react-uploader/package.json +++ b/packages/react-uploader/package.json @@ -33,7 +33,7 @@ "next": "13 || 14" }, "dependencies": { - "@uploadcare/file-uploader": "^1.2.0", + "@uploadcare/file-uploader": "^1.9.0", "@uploadcare/react-adapter": "^0.3.0" }, "repository": { diff --git a/packages/react-uploader/src/Uploader/Inline/FileUploaderInline.tsx b/packages/react-uploader/src/Uploader/Inline/FileUploaderInline.tsx index 7bdc418..bae5b98 100644 --- a/packages/react-uploader/src/Uploader/Inline/FileUploaderInline.tsx +++ b/packages/react-uploader/src/Uploader/Inline/FileUploaderInline.tsx @@ -20,7 +20,7 @@ const AdapterFileUploaderInline = customElementToReactComponent({ elClass: UC.FileUploaderMinimal, }); -export const FileUploaderInline: FC = ({ +export const FileUploaderInline: FC> = ({ ctxName, className, classNameUploader, @@ -31,7 +31,7 @@ export const FileUploaderInline: FC = ({ const CTX_NAME = useMemo(() => ctxName ?? UC.UID.generate(), [ctxName]); const { eventHandlers, config } = useMemo( - () => getCalcPropertyOfProps(props), + () => getCalcPropertyOfProps<"Inline">(props), [props], ); diff --git a/packages/react-uploader/src/Uploader/Minimal/FileUploaderMinimal.tsx b/packages/react-uploader/src/Uploader/Minimal/FileUploaderMinimal.tsx index 07db0f3..d8f3114 100644 --- a/packages/react-uploader/src/Uploader/Minimal/FileUploaderMinimal.tsx +++ b/packages/react-uploader/src/Uploader/Minimal/FileUploaderMinimal.tsx @@ -20,7 +20,7 @@ const AdapterFileUploaderMinimal = customElementToReactComponent({ elClass: UC.FileUploaderMinimal, }); -export const FileUploaderMinimal: FC = ({ +export const FileUploaderMinimal: FC> = ({ ctxName, className, classNameUploader, @@ -31,7 +31,7 @@ export const FileUploaderMinimal: FC = ({ const CTX_NAME = useMemo(() => ctxName ?? UC.UID.generate(), [ctxName]); const { eventHandlers, config } = useMemo( - () => getCalcPropertyOfProps(props), + () => getCalcPropertyOfProps<"Minimal">(props), [props], ); diff --git a/packages/react-uploader/src/Uploader/Regular/FileUploaderRegular.tsx b/packages/react-uploader/src/Uploader/Regular/FileUploaderRegular.tsx index fe691ac..d8e36cc 100644 --- a/packages/react-uploader/src/Uploader/Regular/FileUploaderRegular.tsx +++ b/packages/react-uploader/src/Uploader/Regular/FileUploaderRegular.tsx @@ -21,7 +21,7 @@ const AdapterFileUploaderRegular = customElementToReactComponent({ elClass: UC.FileUploaderRegular, }); -export const FileUploaderRegular: FC = ({ +export const FileUploaderRegular: FC> = ({ ctxName, className, classNameUploader, @@ -31,8 +31,8 @@ export const FileUploaderRegular: FC = ({ }) => { const CTX_NAME = useMemo(() => ctxName ?? UC.UID.generate(), [ctxName]); - const { eventHandlers, config } = useMemo( - () => getCalcPropertyOfProps(props), + const { eventHandlers, config, uploader } = useMemo( + () => getCalcPropertyOfProps<"Regular">(props), [props], ); @@ -50,7 +50,7 @@ export const FileUploaderRegular: FC = ({ {...eventHandlers} /> {/* @ts-ignore */} - + ); diff --git a/packages/react-uploader/src/Uploader/types.ts b/packages/react-uploader/src/Uploader/types.ts index 26e540c..6019492 100644 --- a/packages/react-uploader/src/Uploader/types.ts +++ b/packages/react-uploader/src/Uploader/types.ts @@ -14,29 +14,64 @@ type TExtraPrefixOn = `on${Capitalize}`; type TPrefixOnAndCamelCase = TExtraPrefixOn>; -export type TEventsSchema = { - [K in keyof EventMap as TPrefixOnAndCamelCase]: ( - event: EventMap[K]["detail"], - ) => void; + +export type FileUploaderModes = 'Regular' | 'Minimal' | 'Inline' + +type CommonProps = { + /** + * The class name for wrapper over uploader + */ + className?: string; + + /** + * The class name for uploader + */ + classNameUploader?: string; + + /** + * The unique ID + */ + ctxName?: string; }; -type TRefUploadCtxProvider = { - apiRef?: Ref>; +type FileUploaderRegularProps = { + /** + * The prop controls the button's visibility in regular mode. + */ + headless?: boolean; }; -type TPropsWithEvents = Partial; +type FileUploaderMinimalProps = {}; -type TPropsWithConfig = Partial; +type FileUploaderInlineProps = {}; -type TDefaultProps = { - className?: string; - classNameUploader?: string; - ctxName?: string; -} & Pick; +type UploadCtxPropviderProps = { + apiRef?: Ref>; +}; + +export type EventProps = Partial; + +export type ConfigProps = Partial> & { + pubkey: ConfigType["pubkey"]; +}; -export type TProps = TDefaultProps & - TRefUploadCtxProvider & - TPropsWithEvents & - TPropsWithConfig; +export type TProps = CommonProps & + ConfigProps & + EventProps & + UploadCtxPropviderProps & + Pick & + (T extends "Regular" + ? FileUploaderRegularProps + : T extends "Minimal" + ? FileUploaderMinimalProps + : T extends "Inline" + ? FileUploaderInlineProps + : never); export { UploadCtxProvider } from "@uploadcare/file-uploader"; + +export type TEventsSchema = { + [K in keyof EventMap as TPrefixOnAndCamelCase]: ( + event: EventMap[K]["detail"], + ) => void; +}; diff --git a/packages/react-uploader/src/utils/getCalcPropertyOfProps.ts b/packages/react-uploader/src/utils/getCalcPropertyOfProps.ts index 25fee54..3a80d37 100644 --- a/packages/react-uploader/src/utils/getCalcPropertyOfProps.ts +++ b/packages/react-uploader/src/utils/getCalcPropertyOfProps.ts @@ -1,17 +1,27 @@ -export const getCalcPropertyOfProps = (props: T) => { +import type { FileUploaderModes, TProps } from "../Uploader/types"; + +export const getCalcPropertyOfProps = (props: TProps) => { const eventHandlers: Record = {}; const config: Record = {}; + const uploader: Record = {}; + + for (const [prop, value] of Object.entries(props)) { + if (prop.startsWith("on")) { + eventHandlers[prop] = value; + continue; + } - Object.entries(props).forEach(([key, value]) => { - if (key.startsWith("on")) { - eventHandlers[key] = value; - } else { - config[key] = value; + if (prop === "headless") { + uploader[prop] = value; + continue; } - }); + + config[prop] = value; + } return { eventHandlers, + uploader, config, }; };