2020-04-28 06:52:53 +00:00
|
|
|
import * as React from "react";
|
2021-12-27 12:04:45 +00:00
|
|
|
import { useState } from "react";
|
2020-04-28 06:52:53 +00:00
|
|
|
import styled from "styled-components";
|
|
|
|
|
import BaseControl, { ControlProps } from "./BaseControl";
|
|
|
|
|
import { ControlType } from "constants/PropertyControlConstants";
|
2021-09-09 15:10:22 +00:00
|
|
|
import { BaseButton } from "components/designSystems/appsmith/BaseButton";
|
2021-10-12 08:04:51 +00:00
|
|
|
import { ButtonVariantTypes } from "components/constants";
|
2021-10-04 15:34:37 +00:00
|
|
|
import { Colors } from "constants/Colors";
|
2023-01-23 03:50:47 +00:00
|
|
|
import { FilePickerV2, FileType, SetProgress } from "design-system-old";
|
2021-12-27 12:04:45 +00:00
|
|
|
import {
|
|
|
|
|
Field,
|
|
|
|
|
WrappedFieldInputProps,
|
|
|
|
|
WrappedFieldMetaProps,
|
|
|
|
|
} from "redux-form";
|
2023-01-23 03:50:47 +00:00
|
|
|
import { DialogComponent } from "design-system-old";
|
2021-12-27 12:04:45 +00:00
|
|
|
import { useEffect, useCallback } from "react";
|
2021-12-07 09:45:18 +00:00
|
|
|
import { replayHighlightClass } from "globalStyles/portals";
|
2020-04-28 06:52:53 +00:00
|
|
|
|
|
|
|
|
const StyledDiv = styled.div`
|
|
|
|
|
flex: 1;
|
|
|
|
|
border: 1px solid #d3dee3;
|
|
|
|
|
border-right: none;
|
|
|
|
|
padding: 6px 12px;
|
|
|
|
|
font-size: 14px;
|
|
|
|
|
color: #768896;
|
2022-09-09 15:59:47 +00:00
|
|
|
white-space: nowrap;
|
|
|
|
|
text-overflow: ellipsis;
|
|
|
|
|
overflow: hidden;
|
2020-04-28 06:52:53 +00:00
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
const SelectButton = styled(BaseButton)`
|
|
|
|
|
&&&& {
|
|
|
|
|
max-width: 59px;
|
|
|
|
|
margin: 0 0px;
|
|
|
|
|
min-height: 32px;
|
2021-12-27 12:04:45 +00:00
|
|
|
border-radius: 0px;
|
2020-04-28 06:52:53 +00:00
|
|
|
font-weight: bold;
|
2021-10-04 15:34:37 +00:00
|
|
|
background-color: #fff;
|
|
|
|
|
border-color: ${Colors.PRIMARY_ORANGE} !important;
|
2020-04-28 06:52:53 +00:00
|
|
|
font-size: 14px;
|
|
|
|
|
&.bp3-button {
|
2022-09-09 15:59:47 +00:00
|
|
|
padding: 6px 0px;
|
|
|
|
|
flex-shrink: 0;
|
2020-04-28 06:52:53 +00:00
|
|
|
}
|
2021-10-04 15:34:37 +00:00
|
|
|
span {
|
|
|
|
|
color: ${Colors.PRIMARY_ORANGE} !important;
|
|
|
|
|
font-weight: 400;
|
|
|
|
|
}
|
|
|
|
|
&:hover:enabled,
|
|
|
|
|
&:active:enabled {
|
|
|
|
|
background: rgba(248, 106, 43, 0.1) !important;
|
|
|
|
|
}
|
2020-04-28 06:52:53 +00:00
|
|
|
}
|
|
|
|
|
`;
|
|
|
|
|
|
2021-12-27 12:04:45 +00:00
|
|
|
const FilePickerWrapper = styled.div`
|
|
|
|
|
width: 100%;
|
|
|
|
|
display: flex;
|
|
|
|
|
align-items: center;
|
|
|
|
|
justify-content: center;
|
|
|
|
|
`;
|
2020-04-28 06:52:53 +00:00
|
|
|
|
2021-12-27 12:04:45 +00:00
|
|
|
type RenderFilePickerProps = FilePickerControlProps & {
|
|
|
|
|
input?: WrappedFieldInputProps;
|
|
|
|
|
meta?: WrappedFieldMetaProps;
|
2022-01-06 14:39:21 +00:00
|
|
|
disabled?: boolean;
|
2021-12-27 12:04:45 +00:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
[],
|
|
|
|
|
);
|
2020-04-28 06:52:53 +00:00
|
|
|
|
2021-12-27 12:04:45 +00:00
|
|
|
const onRemoveFile = useCallback(() => setAppFileToBeUploaded(null), []);
|
2020-04-28 06:52:53 +00:00
|
|
|
|
2021-12-27 12:04:45 +00:00
|
|
|
useEffect(() => {
|
|
|
|
|
if (appFileToBeUploaded?.file) {
|
2020-04-28 06:52:53 +00:00
|
|
|
const reader = new FileReader();
|
2021-12-27 12:04:45 +00:00
|
|
|
reader.readAsDataURL(appFileToBeUploaded?.file);
|
2020-04-28 06:52:53 +00:00
|
|
|
reader.onloadend = () => {
|
|
|
|
|
const base64data = reader.result;
|
2021-12-27 12:04:45 +00:00
|
|
|
props.input?.onChange({
|
|
|
|
|
name: appFileToBeUploaded?.file.name,
|
2020-04-28 06:52:53 +00:00
|
|
|
base64Content: base64data,
|
|
|
|
|
});
|
|
|
|
|
};
|
2021-12-27 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
}, [appFileToBeUploaded]);
|
2020-04-28 06:52:53 +00:00
|
|
|
|
2021-12-27 12:04:45 +00:00
|
|
|
return (
|
|
|
|
|
<>
|
2021-12-07 09:45:18 +00:00
|
|
|
<div
|
|
|
|
|
className={replayHighlightClass}
|
2022-01-14 13:50:54 +00:00
|
|
|
style={{ flexDirection: "row", display: "flex", width: "20vw" }}
|
2021-12-07 09:45:18 +00:00
|
|
|
>
|
2022-09-09 15:59:47 +00:00
|
|
|
<StyledDiv title={props?.input?.value?.name}>
|
|
|
|
|
{props?.input?.value?.name}
|
|
|
|
|
</StyledDiv>
|
2020-04-28 06:52:53 +00:00
|
|
|
<SelectButton
|
2021-08-24 13:53:15 +00:00
|
|
|
buttonStyle="PRIMARY"
|
2021-10-12 08:04:51 +00:00
|
|
|
buttonVariant={ButtonVariantTypes.SECONDARY}
|
2022-01-06 14:39:21 +00:00
|
|
|
disabled={props.disabled}
|
2020-04-28 06:52:53 +00:00
|
|
|
onClick={() => {
|
2021-12-27 12:04:45 +00:00
|
|
|
setIsOpen(true);
|
2020-04-28 06:52:53 +00:00
|
|
|
}}
|
2021-04-28 10:28:39 +00:00
|
|
|
text={"Select"}
|
2020-04-28 06:52:53 +00:00
|
|
|
/>
|
|
|
|
|
</div>
|
2021-12-27 12:04:45 +00:00
|
|
|
{isOpen ? (
|
|
|
|
|
<DialogComponent
|
|
|
|
|
canOutsideClickClose
|
|
|
|
|
isOpen={isOpen}
|
|
|
|
|
maxHeight={"540px"}
|
|
|
|
|
setModalClose={() => setIsOpen(false)}
|
|
|
|
|
>
|
|
|
|
|
<FilePickerWrapper>
|
|
|
|
|
<FilePickerV2
|
|
|
|
|
delayedUpload
|
|
|
|
|
fileType={FileType.ANY}
|
|
|
|
|
fileUploader={FileUploader}
|
|
|
|
|
onFileRemoved={onRemoveFile}
|
|
|
|
|
/>
|
|
|
|
|
</FilePickerWrapper>
|
|
|
|
|
</DialogComponent>
|
|
|
|
|
) : null}
|
|
|
|
|
</>
|
|
|
|
|
);
|
2020-04-28 06:52:53 +00:00
|
|
|
}
|
|
|
|
|
class FilePickerControl extends BaseControl<FilePickerControlProps> {
|
|
|
|
|
constructor(props: FilePickerControlProps) {
|
|
|
|
|
super(props);
|
|
|
|
|
this.state = {
|
|
|
|
|
isOpen: false,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
render() {
|
2022-01-06 14:39:21 +00:00
|
|
|
const { configProperty, disabled } = this.props;
|
|
|
|
|
return (
|
|
|
|
|
<Field
|
|
|
|
|
component={RenderFilePicker}
|
|
|
|
|
disabled={disabled}
|
|
|
|
|
name={configProperty}
|
|
|
|
|
/>
|
|
|
|
|
);
|
2020-04-28 06:52:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getControlType(): ControlType {
|
|
|
|
|
return "FILE_PICKER";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface FilePickerComponentState {
|
|
|
|
|
isOpen: boolean;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export type FilePickerControlProps = ControlProps;
|
|
|
|
|
|
|
|
|
|
export default FilePickerControl;
|