UNPKG

@airplane/views

Version:

A React library for building Airplane views. Views components are optimized in style and functionality to produce internal apps that are easy to build and maintain.

73 lines (72 loc) 2.11 kB
import { jsxs, jsx } from "react/jsx-runtime"; import { LoadingOverlay, FileInput } from "@mantine/core"; import { AirplaneFile } from "airplane"; import { forwardRef, useState, useCallback } from "react"; import { showNotification } from "../notification/showNotification.js"; import { useUploadAirplaneFiles } from "./useUploadAirplaneFiles.js"; const BasicFileInputComponent = /* @__PURE__ */ forwardRef(({ accept, getUploadURL, value, onChange, ...restProps }, ref) => { const [loading, setLoading] = useState(false); const { onDrop } = useUploadAirplaneFiles({ onChange, onLoad: (file) => { setLoading(false); showNotification({ title: "Upload successful", message: `${file.name} uploaded successfully`, type: "success" }); }, onError: (file, e) => { setLoading(false); showNotification({ title: "Upload failed", message: `${file.name} upload failed: ${e.message}`, type: "error" }); }, getUploadURL }); const onMantineChange = (f) => { if (f === null) { onDrop([]); } else if (f instanceof File) { setLoading(true); onDrop([f]); } else { setLoading(true); onDrop(f); } }; const container = useCallback((children) => { return /* @__PURE__ */ jsxs("div", { style: { position: "relative" }, children: [ /* @__PURE__ */ jsx(LoadingOverlay, { visible: loading }), " ", children ] }); }, [loading]); return /* @__PURE__ */ jsx(FileInput, { accept: accept == null ? void 0 : accept.join(","), value: componentValueToMantineValue(value), onChange: onMantineChange, inputContainer: container, ...restProps, ref }); }); const componentValueToMantineValue = (v) => { if (v === void 0) { return null; } else if (v instanceof AirplaneFile) { return new File([], v.name); } else { return v.map((f) => new File([], f.name)); } }; BasicFileInputComponent.displayName = "BasicFileInputComponent"; export { BasicFileInputComponent }; //# sourceMappingURL=BasicFileInput.js.map