beamworks / react-csv-importer

Uploader + CSV parser + raw file preview + UI for custom user column mapping, all in one
https://www.npmjs.com/package/react-csv-importer
MIT License
231 stars 95 forks source link

Support passing `fieldAssignments` as an input to pre-assign columns #111

Open n2o1988 opened 11 months ago

n2o1988 commented 11 months ago

Hi, thanks so much for this importer, I'm using it in a side project and finding it very useful.

I've one feature request: could you add a property to pre-define column assignments? For instance: if I pass a CSV with columns "one, two, three", and I want to assign those to the fields "a, b, c", I want to be able to pass

<Importer
  ...
  fieldsAssignments={{ 'a': 'one', 'b': 'two', 'c': 'three' }}
  // or by index
  fieldsAssignments={{ 'a': 0, 'b': 1, 'c': 2 }}
>

This would have the effect of having the columns already assigned (but still editable), once the user lands on the Fields selection step. My use case is that I'll be dealing with csvs from different sources pretty regularly, with different formats, and I'd like to automate that step by letting the user save some presets with the mapping on the server side.

Thanks

n2o1988 commented 11 months ago

An alternative would be to add a property to the ImporterField to preselect a column

<ImporterField name="a" label="A" defaultColumn="one" />
<ImporterField name="b" label="B" defaultColumn="two" />
<ImporterField name="c" label="C" defaultColumn="three" />
n2o1988 commented 11 months ago

Example implementation: https://github.com/n2o1988/react-csv-importer/pull/1 Let me know if you want me to open that PR against your repo