Skip to content
Snippets Groups Projects
Select Git revision
  • 0950a1fcfd0b38a7880a43a0c88820e5fa79648c
  • MCCS-163 default
  • main
  • sar-277-update-docs-with-examples-for-lrc
  • st-946-automate
  • sar_302-log-fix
  • sar-287_subarray_commands_to_lrc
  • sar_302-POC_await_sub_device_state
  • sat_302_fix_pipelines
  • sar-286_lrc_one_subarry_command
  • sar-286_lrc_improvements
  • sar-288-async-controller
  • sar-276-combine-tango-queue
  • sar-255_remove_nexus_reference
  • sar-275-add-LRC
  • sar-273-add-lrc-attributes
  • sar-272
  • sp-1106-marvin-1230525148-ska-tango-base
  • sp-1106-marvin-813091765-ska-tango-base
  • sar-255/Publish-package-to-CAR
  • mccs-661-device-under-test-fixture
  • mccs-659-pep257-docstring-linting
  • 0.11.3
  • 0.11.2
  • 0.11.1
  • 0.11.0
  • 0.10.1
  • 0.10.0
  • 0.9.1
  • 0.9.0
  • 0.8.1
  • 0.8.0
  • 0.7.2
  • 0.7.1
  • 0.7.0
  • 0.6.6
  • 0.6.5
  • 0.6.4
  • 0.6.3
  • 0.6.2
  • 0.6.1
  • 0.6.0
42 results

index.rst

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;