Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

html name attribute not added to input in DropZone component #12394

Open
eatoncw opened this issue Jul 12, 2024 · 0 comments
Open

html name attribute not added to input in DropZone component #12394

eatoncw opened this issue Jul 12, 2024 · 0 comments
Labels
Bug Something is broken and not working as intended in the system. untriaged

Comments

@eatoncw
Copy link

eatoncw commented Jul 12, 2024

Summary

The DropZone component does not support a "name" prop. It would be good to be able to add the html "name" attribute to the <input> it renders.

The specific use case is using the component in a multipart/form-data form in a Remix app. Without a name attribute, the input is not picked up in the action using composeUploadHandlers, createMemoryUploadHandler (I feel like this is more on the Polaris than Remix, as adding a standard html attribute seems standard)

Expected behavior

The file upload input takes a "name" and is picked up in Remix's action (for FormData)

Actual behavior

The file upload input is ignored

Steps to reproduce

Remix route

export async function action({ request }: ActionFunctionArgs) {
  const uploadHandler = composeUploadHandlers(
   
    multipartUploadHandler,  // our custom upload handler
    createMemoryUploadHandler() // should pick up everything else when the first handler returns undefined
  );

  const formData = await parseMultipartFormData(request, uploadHandler);
}

// Form

import { BlockStack, Box, Button, DropZone, Select, Text, Thumbnail } from '@shopify/polaris';
import { Form } from '@remix-run/react';
import { useCallback, useState } from 'react';
import { NoteIcon } from '@shopify/polaris-icons';

const options = [
  { label: 'Default', value: 'default' },
  { label: 'Legacy', value: 'legacy' },
];

export default function UploadForm() {
  const [file, setFile] = useState<File>();
  const [selected, setSelected] = useState('default');

  const handleSelectChange = useCallback((value: string) => setSelected(value), []);
  const handleDropZoneDrop = useCallback(
    (_dropFiles: File[], acceptedFiles: File[], _rejectedFiles: File[]) =>
      setFile(acceptedFiles[0]),
    []
  );
  const fileUpload = !file && <DropZone.FileUpload actionHint="Accepts .csv" />;
  const uploadedFile = file && (
    <BlockStack>
      <Thumbnail
        size="small"
        alt={file.name}
        source={file.type === '.csv' ? window.URL.createObjectURL(file) : NoteIcon}
      />
      {file.name}{' '}
      <Text variant="bodySm" as="p">
        {file.size} bytes
      </Text>
    </BlockStack>
  );
  return (
    <div>
      <Text as="h4">Upload Form</Text>
      <p>Upload a CSV file</p>
      <Box paddingBlock={'200'}>
        <Form
          method="post"
          action="/app/subscribers/upload"
          encType="multipart/form-data"
          name="file-upload-form"
          id="file-upload-form"
        >
          <Select
            label="Upload data version"
            options={options}
            onChange={handleSelectChange}
            value={selected}
            name="upload-data-version"
            id="upload-data-version"
          />
          <Box paddingBlockStart={'200'}>
            <DropZone
              accept=".csv"
              type="file"
              errorOverlayText="File type must be .csv"
              allowMultiple={false}
              onDrop={handleDropZoneDrop}
              id="file-upload"
            >
              {uploadedFile}
              {fileUpload}
            </DropZone>
          </Box>
          <Box paddingBlockStart="200">
            <Button submit variant="primary">
              Upload
            </Button>
          </Box>
        </Form>
      </Box>
    </div>
  );
}

When the form is submitted, the DropZone file input is not included as form data in Remix action

Are you using React components?

Yes

Polaris version number

12.27.0

Browser

Chrome

Device

No response

@eatoncw eatoncw added Bug Something is broken and not working as intended in the system. untriaged labels Jul 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Something is broken and not working as intended in the system. untriaged
Projects
None yet
Development

No branches or pull requests

1 participant