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

Support init fields assignments #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions src/components/Importer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,31 @@ export function Importer<Row extends BaseRow>(
onClose,
children: content,
locale: userLocale,
fieldAssignments,
...customPapaParseConfig
} = props;

// helper to combine our displayed content and the user code that provides field definitions
const initialFieldsState = useMemo(
() => (fieldAssignments ? { fieldAssignments } : null),
[fieldAssignments]
);
const [fields, userFieldContentWrapper] = useFieldDefinitions();

const [fileState, setFileState] = useState<FileStepState | null>(null);
const [fileAccepted, setFileAccepted] = useState<boolean>(false);

const [fieldsState, setFieldsState] = useState<FieldsStepState | null>(null);
const [fieldsState, setFieldsState] = useState<FieldsStepState | null>(
initialFieldsState
);
const [fieldsAccepted, setFieldsAccepted] = useState<boolean>(false);

// reset field assignments when file changes
const activeFile = fileState && fileState.file;
useEffect(() => {
if (activeFile) {
setFieldsState(null);
setFieldsState(initialFieldsState);
}
}, [activeFile]);
}, [activeFile, initialFieldsState]);

const externalPreview = useMemo<ImporterFilePreview | null>(() => {
// generate stable externally-visible data objects
Expand Down Expand Up @@ -142,7 +148,7 @@ export function Importer<Row extends BaseRow>(
// reset all state
setFileState(null);
setFileAccepted(false);
setFieldsState(null);
setFieldsState(initialFieldsState);
setFieldsAccepted(false);
}
: undefined
Expand Down
8 changes: 7 additions & 1 deletion src/components/ImporterProps.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import React from 'react';
import { ImporterLocale } from '../locale';
import { CustomizablePapaParseConfig, ParseCallback, BaseRow } from '../parser';
import {
CustomizablePapaParseConfig,
ParseCallback,
BaseRow,
FieldAssignmentMap
} from '../parser';

// information for displaying a spreadsheet-style column
export interface ImporterPreviewColumn {
Expand Down Expand Up @@ -66,4 +71,5 @@ export type ImporterProps<Row extends BaseRow> = ImporterDataHandlerProps<
onClose?: (info: ImportInfo) => void;
children?: ImporterContentRenderProp | React.ReactNode;
locale?: ImporterLocale;
fieldAssignments?: FieldAssignmentMap;
} & CustomizablePapaParseConfig;
4 changes: 4 additions & 0 deletions src/components/fields-step/FieldsStep.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ export const FieldsStep: React.FC<{

// make sure there are no extra fields
useEffect(() => {
if (fields.length === 0) {
// Defer removal logic until fields have loaded from children components
return;
}
const removedFieldNames = Object.keys(fieldAssignments).filter(
(existingFieldName) =>
!fields.some((field) => field.name === existingFieldName)
Expand Down