Skip to content
Snippets Groups Projects
Select Git revision
  • c18d3061a7965f8e428c12610740d2faea0d890c
  • SAR-150_add_transaction_id_in_all_logs default protected
  • master
  • sar-293_fix_lint_issue
  • sar-259/Testing-skallop-docs
  • sar-255/Publish-package-to-CAR
  • test-package
  • SAR-189-use-single-namespace-for-package-naming
  • revert-52d118c0
  • 0.4.1
  • 0.4.0
  • 0.3.0
  • 0.2.1
  • 0.2.0
  • 0.1.0
15 results

test_configuration.py

Blame
  • Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    FileInput.tsx 1.60 KiB
    import {
      ChangeEvent,
      Dispatch,
      SetStateAction,
      useEffect,
      useRef,
      InputHTMLAttributes,
      useId,
    } from "react";
    import Typography from "src/components/Typography";
    
    interface Props extends InputHTMLAttributes<HTMLInputElement> {
      label: string;
      files: File[];
      onFilesChange: Dispatch<SetStateAction<File[]>>;
    }
    
    /**
     * Controlled File Input component
     * @param props
     * @constructor
     */
    const FileInput = (props: Props) => {
      const inputRef = useRef<HTMLInputElement>(null);
      const inputId = useId();
    
      const { files, onFilesChange, label, ...rest } = props;
    
      /**
       * Workaround for not being able to set the value directly on <input type="file">
       *   Using the https://developer.mozilla.org/en-US/docs/Web/API/DataTransfer
       *   API to construct a valid file list; not available in IE!
       */
      useEffect(() => {
        const dataTransfer = new DataTransfer();
        files.forEach((file: File) => dataTransfer.items.add(file));
        if (inputRef.current) inputRef.current.files = dataTransfer.files;
      }, [files]);
    
      const handleChange = (evt: ChangeEvent<HTMLInputElement>) => {
        const fileList = evt.currentTarget.files;
        if (!fileList) {
          onFilesChange([]);
        } else {
          onFilesChange(Array.from(fileList));
        }
      };
    
      return (
        <div className="flex flex-col">
          <label htmlFor={inputId} className="-mt-3.5">
            <Typography text={label} variant="paragraph" />
          </label>
          <input
            className="mt-3"
            id={inputId}
            ref={inputRef}
            type="file"
            onChange={handleChange}
            {...rest}
          />
        </div>
      );
    };
    
    export default FileInput;