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

Implement non-standard behavior for empty values based on OF specification. #45

Open
svenvandescheur opened this issue May 11, 2023 · 0 comments

Comments

@svenvandescheur
Copy link
Collaborator

          I think the most explicit and straightforward way would be:
const EMPTY_VALUES_BY_COMPONENT_TYPE = {
  textfield: '',
  numberfield: null, // 
  file: [],
  editgrid: [],
} as const;

type EmptyValueKeys = keyof typeof EMPTY_VALUES_BY_COMPONENT_TYPE;
type EmptyValueTypes = typeof EMPTY_VALUES_BY_COMPONENT_TYPE[EmptyValueKeys];


const getEmptyValueForComponent = (component: ComponentSchema): EmptyValueTypes | [] => {
  // I *think* this should cover it in a generic way, but I'm not sure about exotic components like editgrid
  if (component.multiple) {
    return [];
  }
  if (!EMPTY_VALUES_BY_COMPONENT_TYPE[component.type]) {
    throw new Error(`Ensure the component type ${component.type} is added to the mapping.`);
  }
  return EMPTY_VALUES_BY_COMPONENT_TYPE[component.type];
};
  • This keeps the data type uniform with the "not-empty value" datatype where possible
  • It's a simple lookup table

Empty value vs unset for a form is not a relevant distinction - it needs to normalize to a "string-like" thing for keyboard input anyway. For data processing in the backend it's also far easier to have the "guarantee" that the data type is always as expected. As for whether the input was touched/changed, a comparison against component.defaultValue should be sufficient (which is also what happens in the linked backend code).

Let's try this approach and see how far we get?

Originally posted by @sergei-maertens in #28 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants