-
Notifications
You must be signed in to change notification settings - Fork 139
feat(custom-fields-ugn-master) #221
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
base: master
Are you sure you want to change the base?
feat(custom-fields-ugn-master) #221
Conversation
* add customFieldsHook into RsiProps: Runs on Match Columns step for each column when their state did change. Used to define custom fields with keys that are can be generated only from csv column header. * implement customFieldsHook logic * add dropDownLabel into Field - UI-facing label for dropdown option
- revert MatchIcon changes - more generic example of custom fields - mergeCustomFields - add custom fields to end of fields array
Hey @audi2014, thank you for another contribution! Could you expand why similar code to this code doesn't solve the custom field problem:
I understand that this PR adds a custom icon for these fields and an ability to change the column name in the dropdown. But I am not sure why a new hook is needed in this case. EDIT: I am interested in selectHeaderStepHook not matchColumnsStepHook |
Hello, @masiulis! Technically, selectHeaderStepHook covers 100% functionality for the end-user. But from UX perspective this behavior looks wired. diff --git a/src/stories/Default.stories.tsx b/src/stories/Default.stories.tsx
index ce027f4..76b76bc 100644
--- a/src/stories/Default.stories.tsx
+++ b/src/stories/Default.stories.tsx
@@ -1,16 +1,37 @@
import { ReactSpreadsheetImport } from "../ReactSpreadsheetImport"
-import { Box, Link, Code, Button, useDisclosure } from "@chakra-ui/react"
+import { Box, Button, Code, Link, useDisclosure } from "@chakra-ui/react"
import { mockRsiValues } from "./mockRsiValues"
import { useState } from "react"
import type { Result } from "src/types"
+import { Field } from "src/types"
export default {
title: "React spreadsheet import",
}
+const FIELDS: Field<string>[] = [
+ {
+ label: "Name",
+ key: "name",
+ alternateMatches: ["first name", "first"],
+ fieldType: {
+ type: "input",
+ },
+ example: "Stephanie",
+ validations: [
+ {
+ rule: "required",
+ errorMessage: "Name is required",
+ },
+ ],
+ },
+]
+
export const Basic = () => {
const [data, setData] = useState<Result<any> | null>(null)
const { isOpen, onOpen, onClose } = useDisclosure()
+ const [fields, setFields] = useState(FIELDS)
+
return (
<>
<Box py={20} display="flex" gap="8px" alignItems="center">
@@ -22,7 +43,29 @@ export const Basic = () => {
<Link href="./exampleFile.csv" border="2px solid #718096" p="8px" borderRadius="8px" download="exampleCSV">
Download example file
</Link>
- <ReactSpreadsheetImport {...mockRsiValues} isOpen={isOpen} onClose={onClose} onSubmit={setData} />
+ <ReactSpreadsheetImport
+ {...mockRsiValues}
+ fields={fields}
+ selectHeaderStepHook={async (headerValues, data) => {
+ setFields([
+ ...FIELDS,
+ ...headerValues.map(
+ (e) =>
+ ({
+ label: "CF:" + e,
+ key: e,
+ fieldType: {
+ type: "input",
+ },
+ } as Field<string>),
+ ),
+ ])
+ return { headerValues, data }
+ }}
+ isOpen={isOpen}
+ onClose={onClose}
+ onSubmit={setData}
+ />
{!!data && (
<Box pt={64} display="flex" gap="8px" flexDirection="column">
<b>Returned data (showing first 100 rows):</b>
|
@audi2014 I am still trying to wrap my head around the problem from the users perspective. You mentioned:
If we want to give this functionality to the user, maybe allowing marking the column as custom in UI would work? This would avoid a custom hook, and would not clutter dropdown or table columns. But because I don't fully understand the problem, I don't know if this would fully solve what you are trying to solve.
I am not sure how would a user define validation or mapping in your case. By user do you mean the end user importing the file, or the programmer configuring the hooks? |
@masiulis sorry for the delay. I like this approach. It is simple and predictable. However, I do not know your vision of what should happen next. Preconditions:
Case 1 (Default Positive)
Case 2 (Defined field marked as a custom field)
Case 3 (user wants all unknown fields to be automatically marked as custom fields)
In my approach (with customFieldsHook) All these questions can be resolved on the Developer's side:
But my approach (with customFieldsHook) is too complicated. So I am 100% agree. Final Solution should be simpler. We cannot complicate the library for 100% of developers to satisfy 1% of edge cases. |
Hey @audi2014, thanks for your detailed response. I have a proposal that I would like to run by you.
Problems with this solution:
Comparing with your points:
Works automatically, user doesn't need to select anything.
Only unselected fields become custom fields, and because they are in their own namespace
This would be the default (and only) behaviour.
Would not be a problem as all fields would be nested in
Would not be a problem as custom fields would be created after match column step, so there is no manual field creation and automatic matching required.
That would not be possible not, if this is a big requirement it could be a separate hook then that is ran on custom field creation. |
Free-form "custom" fields? #83 Follow-up #202
Goal: add ability to create new fields in runtime from unmapped csv columns
Yes, this logic can be implemented by matchColumnsStepHook but, but in this case user has poor UX:
Yes, this logic can be implemented by selectHeaderStepHook (use local state for fields and update them here)