Skip to content

Instantly share code, notes, and snippets.

@avwilks
Last active May 27, 2021 15:13
Show Gist options
  • Select an option

  • Save avwilks/fa6a6a96c89a6fa535660b4112af1236 to your computer and use it in GitHub Desktop.

Select an option

Save avwilks/fa6a6a96c89a6fa535660b4112af1236 to your computer and use it in GitHub Desktop.
Conditional Fields Example Document
{
"field1":{
"field1a":{
"when":"(a) => a.includes('USA')"
},
"field1b":{
"when":"(a) => a.includes('Mexico')"
},
"field1c":{
"when":"(a) => a.includes('Canada')",
"field1ca": {
"when": "(a) => a.includes('Ontario')",
"field1caa": {
"when": "(a) => a.length > 0"
}
}
}
},
"field2":{
"field2a": {
"when":"(a) => a === 'Yes'"
}
}
}
@cjohnson741

cjohnson741 commented May 26, 2021

Copy link
Copy Markdown

Maybe we can revise this to be something like (pretty much a trie):

{
    fieldName: "my silly field",
    ...(other metadata)
    CURRENT_STATE: True || False,
    AND: [document_id, document_id ...],
    OR: [document_id, document_id ...],
    TRUE: document_id
    FALSE: document_id
}

We can have function to traverse each of the AND's and OR's, then we can evaluate the "truthiness" of the node and "hop" to the next if the state is true or false based on the user input (we would halt on NULL)

We can also have the frontend display all the node metadata when it hits the node

@avwilks

avwilks commented May 26, 2021

Copy link
Copy Markdown
Author

Where do we store the show condition in this concept? With the rest of the metadata?

@cjohnson741

Copy link
Copy Markdown

The "show condition" is implied with this method. As we visit each node - and whatever AND/OR nodes are associated with it - we render the field in the DOM

@cjohnson741

Copy link
Copy Markdown

a quick example of how I imagine node traversal working with this: https://codesandbox.io/s/staging-darkness-15r62?file=/src/App.js

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment