PromucFlow_constructor/app/client/src/components/formControls/FilePickerControl.tsx

177 lines
4.3 KiB
TypeScript
Raw Normal View History

import * as React from "react";
import { useState } from "react";
import styled from "styled-components";
import BaseControl, { ControlProps } from "./BaseControl";
import { ControlType } from "constants/PropertyControlConstants";
import { BaseButton } from "components/designSystems/appsmith/BaseButton";
import { ButtonVariantTypes } from "components/constants";
import { Colors } from "constants/Colors";
chore: migrate toast (#17208) * Refactor toast to be passed the dispatch hook externally * Add comments explaining dilemma * use store.dispatch instead of a hook * use alpha version * Change imports * Refactor DebugButton out * update release * fix issue with incorrectly merged package.lock * fix syntax of alpha version * bump ds vesion * copy lock from release * update lock to have alpha * make changes * delete Toast * DS package version updated * import change from release * use new alpha version * update ds version * update ds version * chore: migrate editable text and friends (#17285) * Delete empty components * use alpha for ds * Deleted EditableTextSubComponent, import changes * Delete EditableText, import changes * use ds alpha 10 * Delete EditableTextWrapper.tsx * update ds to use next minor version * use new alpha * fix issue with merge Co-authored-by: Albin <albin@appsmith.com> * chore: migrate file picker v2 (#17308) * use alpha ds * Delete FilePickerV2, import changes * Delete FilePicker, change imports * update alpha version * chore: move copy url form into setting components (#17322) * move CopyUrlForm to src/pages/settings/formgroup * update ds version to use next minor release * feat: Migrate table component to design system (#17329) * feat: Migrate table component to design system * removed commented code in ads index file * fix: table no data hover effect removed Co-authored-by: Tanvi Bhakta <tanvibhakta@gmail.com> * feat: Banner message component migrated to design system (#17327) * feat: Banner image component migrated to design system * Version update for design system package * design system version updated Co-authored-by: Tanvi Bhakta <tanvibhakta@gmail.com> * feat: Tabs component migrated to design system (#17321) * feat: Tabs component migrated to design system * design system package version updated * Update app/client/src/components/editorComponents/form/FormDialogComponent.tsx * Update app/client/src/pages/Editor/PropertyPane/PropertyPaneTab.tsx * Tab component expand issue fix Co-authored-by: Tanvi Bhakta <tanvibhakta@gmail.com> Co-authored-by: Albin <albin@appsmith.com> Co-authored-by: albinAppsmith <87797149+albinAppsmith@users.noreply.github.com>
2022-10-13 20:13:44 +00:00
import { FilePickerV2, FileType, SetProgress } from "design-system";
import {
Field,
WrappedFieldInputProps,
WrappedFieldMetaProps,
} from "redux-form";
import { DialogComponent } from "design-system";
import { useEffect, useCallback } from "react";
2021-12-07 09:45:18 +00:00
import { replayHighlightClass } from "globalStyles/portals";
const StyledDiv = styled.div`
flex: 1;
border: 1px solid #d3dee3;
border-right: none;
padding: 6px 12px;
font-size: 14px;
color: #768896;
white-space: nowrap;
text-overflow: ellipsis;
overflow: hidden;
`;
const SelectButton = styled(BaseButton)`
&&&& {
max-width: 59px;
margin: 0 0px;
min-height: 32px;
border-radius: 0px;
font-weight: bold;
background-color: #fff;
border-color: ${Colors.PRIMARY_ORANGE} !important;
font-size: 14px;
&.bp3-button {
padding: 6px 0px;
flex-shrink: 0;
}
span {
color: ${Colors.PRIMARY_ORANGE} !important;
font-weight: 400;
}
&:hover:enabled,
&:active:enabled {
background: rgba(248, 106, 43, 0.1) !important;
}
}
`;
const FilePickerWrapper = styled.div`
width: 100%;
display: flex;
align-items: center;
justify-content: center;
`;
type RenderFilePickerProps = FilePickerControlProps & {
input?: WrappedFieldInputProps;
meta?: WrappedFieldMetaProps;
disabled?: boolean;
onChange: (event: any) => void;
};
function RenderFilePicker(props: RenderFilePickerProps) {
const [isOpen, setIsOpen] = useState(false);
const [appFileToBeUploaded, setAppFileToBeUploaded] = useState<{
file: File;
setProgress: SetProgress;
} | null>(null);
const FileUploader = useCallback(
async (file: File, setProgress: SetProgress) => {
if (!!file) {
setAppFileToBeUploaded({
file,
setProgress,
});
} else {
setAppFileToBeUploaded(null);
}
},
[],
);
const onRemoveFile = useCallback(() => setAppFileToBeUploaded(null), []);
useEffect(() => {
if (appFileToBeUploaded?.file) {
const reader = new FileReader();
reader.readAsDataURL(appFileToBeUploaded?.file);
reader.onloadend = () => {
const base64data = reader.result;
props.input?.onChange({
name: appFileToBeUploaded?.file.name,
base64Content: base64data,
});
};
}
}, [appFileToBeUploaded]);
return (
<>
2021-12-07 09:45:18 +00:00
<div
className={replayHighlightClass}
style={{ flexDirection: "row", display: "flex", width: "20vw" }}
2021-12-07 09:45:18 +00:00
>
<StyledDiv title={props?.input?.value?.name}>
{props?.input?.value?.name}
</StyledDiv>
<SelectButton
buttonStyle="PRIMARY"
buttonVariant={ButtonVariantTypes.SECONDARY}
disabled={props.disabled}
onClick={() => {
setIsOpen(true);
}}
text={"Select"}
/>
</div>
{isOpen ? (
<DialogComponent
canOutsideClickClose
isOpen={isOpen}
maxHeight={"540px"}
setModalClose={() => setIsOpen(false)}
>
<FilePickerWrapper>
<FilePickerV2
delayedUpload
fileType={FileType.ANY}
fileUploader={FileUploader}
onFileRemoved={onRemoveFile}
/>
</FilePickerWrapper>
</DialogComponent>
) : null}
</>
);
}
class FilePickerControl extends BaseControl<FilePickerControlProps> {
constructor(props: FilePickerControlProps) {
super(props);
this.state = {
isOpen: false,
};
}
render() {
const { configProperty, disabled } = this.props;
return (
<Field
component={RenderFilePicker}
disabled={disabled}
name={configProperty}
/>
);
}
getControlType(): ControlType {
return "FILE_PICKER";
}
}
export interface FilePickerComponentState {
isOpen: boolean;
}
export type FilePickerControlProps = ControlProps;
export default FilePickerControl;