Giter Site home page Giter Site logo

Comments (5)

cherepanov avatar cherepanov commented on May 24, 2024 1

@tulusibrahim

Yes, this is possible. Follow these steps:

  • Request data on the form level, inside onDidMount
  • Propagate it to the form's shared state.
  • Use the calculated value for component data.

The key here is e.store.formData.state, a shared object that you can access across all components.

async function Action (e, args) {
    fetch ('https://gist.githubusercontent.com/rogargon/5534902/raw/434445021e155240ca78e378f10f70391dd594ea/countries.json')
        .then (data => data.json())
        .then (data => {
            const preparedData = data.map(value => ({value, label: value}))

            e.store.formData.state.options = preparedData 
        })
}
function Calculation (form) {
    return form.state.options
}

Here is fixed example https://stackblitz.com/edit/vitejs-vite-djtftw

And minimal form definition
{
  "version": "1",
  "actions": {
    "test": {
      "body": "    const foo = e.data['CommaValues'];\n    debugger",
      "params": {}
    },
    "preloadData": {
      "body": "    fetch ('https://gist.githubusercontent.com/rogargon/5534902/raw/434445021e155240ca78e378f10f70391dd594ea/countries.json')\n        .then (data => data.json())\n        .then (data => {\n            const preparedData = data.map(value => ({value, label: value}))\n\n            e.store.formData.state.options = preparedData       \n        })",
      "params": {
        "staticArg": "string"
      }
    },
    "dng": {
      "body": "    // debugger\n    console.log(e.data)\n    console.log(e.state)\n    console.log(e.data.state)\n    debugger",
      "params": {}
    },
    "estersr": {
      "body": "    debugger\n    // e.sender.field.componentStore = e.store.form.state",
      "params": {}
    }
  },
  "form": {
    "key": "Screen",
    "type": "Screen",
    "props": {},
    "css": {
      "any": {
        "object": {},
        "string": "    text-align: left;"
      }
    },
    "events": {
      "onDidMount": [
        {
          "name": "preloadData",
          "type": "code",
          "args": {
            "staticArg": "123"
          }
        }
      ]
    },
    "children": [
      {
        "key": "status",
        "type": "RsDropdown",
        "props": {
          "label": {
            "value": "Status"
          },
          "data": {
            "computeType": "function",
            "fnSource": "    return form.state.options"
          }
        },
        "wrapperCss": {
          "any": {
            "object": {}
          }
        },
        "htmlAttributes": [],
        "events": {
          "onSearch": []
        }
      }
    ]
  },
  "localization": {},
  "languages": [
    {
      "code": "en",
      "dialect": "US",
      "name": "English",
      "description": "American English",
      "bidi": "ltr"
    }
  ],
  "defaultLanguage": "en-US"
}

from formengine.

cherepanov avatar cherepanov commented on May 24, 2024

@tulusibrahim
Hi!

Please explain what you intend to implement.
What would be the end-user use case?
To create dependent form fields with various options, refer to the comment below.

from formengine.

cherepanov avatar cherepanov commented on May 24, 2024

You can use calculated value and get all fields data inside it.

Form code
{
  "version": "1",
  "actions": {
    "loadData": {
      "body": "\n    const [searchValue, loadCallback, currentDataLength] = e.args\n\n    fetch ('https://gist.githubusercontent.com/rogargon/5534902/raw/434445021e155240ca78e378f10f70391dd594ea/countries.json')\n        .then (data => data.json())\n        .then (data => {\n            const preparedData = data\n                .filter(value => value.toLowerCase().includes(searchValue.toLowerCase()))\n                .slice(currentDataLength, currentDataLength + 20)\n                .map(value => ({value, label: value}))\n\n            loadCallback(preparedData)        \n        })\n",
      "params": {}
    }
  },
  "form": {
    "key": "Screen",
    "type": "Screen",
    "props": {},
    "css": {
      "any": {
        "object": {},
        "string": "    text-align: left;"
      }
    },
    "children": [
      {
        "key": "CommaValues",
        "type": "RsInput",
        "props": {}
      },
      {
        "key": "status",
        "type": "RsDropdown",
        "props": {
          "label": {
            "value": "Status"
          },
          "data": {
            "computeType": "function",
            "fnSource": "// enter your code here\n    const sourceFiledValue = form?.fields?.get('CommaValues').value;\n    const dropdownOptions = (sourceFiledValue || '').split(/(?:,| |;)+/);\n\n    return dropdownOptions.map((v) => ({label: v, value: v}));"
          }
        },
        "wrapperCss": {
          "any": {
            "object": {}
          }
        },
        "htmlAttributes": []
      },
      {
        "key": "unit",
        "type": "RsDropdown",
        "props": {
          "label": {
            "value": "Unit"
          },
          "readOnly": {
            "computeType": "function",
            "value": false
          }
        },
        "events": {
          "onLoadData": [
            {
              "name": "loadData",
              "type": "code"
            }
          ]
        }
      },
      {
        "key": "remark",
        "type": "RsTextArea",
        "props": {
          "label": {
            "value": "Remark"
          },
          "size": {
            "value": "md"
          }
        }
      },
      {
        "key": "feedback",
        "type": "RsTextArea",
        "props": {
          "label": {
            "value": "Feedback"
          }
        }
      },
      {
        "key": "RsButton 1",
        "type": "RsButton",
        "props": {
          "children": {
            "value": "Submit"
          }
        },
        "events": {
          "onClick": []
        }
      }
    ]
  },
  "localization": {},
  "languages": [
    {
      "code": "en",
      "dialect": "US",
      "name": "English",
      "description": "American English",
      "bidi": "ltr"
    }
  ],
  "defaultLanguage": "en-US"
}

2024-05-02_17-39

from formengine.

cherepanov avatar cherepanov commented on May 24, 2024

And the same inside Actions callback.

Form code ```json { "version": "1", "actions": { "loadData": { "body": "\n const [searchValue, loadCallback, currentDataLength] = e.args\n const statusValue = e.data['status'];\n\n alert(`Selected status: ${statusValue}`);\n\n fetch ('https://gist.githubusercontent.com/rogargon/5534902/raw/434445021e155240ca78e378f10f70391dd594ea/countries.json')\n .then (data => data.json())\n .then (data => {\n const preparedData = data\n .filter(value => value.toLowerCase().includes(searchValue.toLowerCase()))\n .slice(currentDataLength, currentDataLength + 20)\n .map(value => ({value, label: value}))\n\n loadCallback(preparedData) \n })\n", "params": { "staticArg": "string" } }, "test": { "body": " const foo = e.data['CommaValues'];\n debugger", "params": {} } }, "form": { "key": "Screen", "type": "Screen", "props": {}, "css": { "any": { "object": {}, "string": " text-align: left;" } }, "children": [ { "key": "CommaValues", "type": "RsInput", "props": {} }, { "key": "status", "type": "RsDropdown", "props": { "label": { "value": "Status" }, "data": { "computeType": "function", "fnSource": "// enter your code here\n const sourceFiledValue = form?.fields?.get('CommaValues').value;\n const dropdownOptions = (sourceFiledValue || '').split(/(?:,| |;)+/);\n\n return dropdownOptions.map((v) => ({label: v, value: v}));" } }, "wrapperCss": { "any": { "object": {} } }, "htmlAttributes": [], "events": { "onSearch": [] } }, { "key": "unit", "type": "RsDropdown", "props": { "label": { "value": "Unit" }, "readOnly": { "computeType": "function", "value": false } }, "events": { "onLoadData": [ { "name": "loadData", "type": "code" } ], "onSearch": [ { "name": "test", "type": "code" } ] } }, { "key": "remark", "type": "RsTextArea", "props": { "label": { "value": "Remark" }, "size": { "value": "md" } } }, { "key": "feedback", "type": "RsTextArea", "props": { "label": { "value": "Feedback" } } }, { "key": "RsButton 1", "type": "RsButton", "props": { "children": { "value": "Submit" } }, "events": { "onClick": [] } } ] }, "localization": {}, "languages": [ { "code": "en", "dialect": "US", "name": "English", "description": "American English", "bidi": "ltr" } ], "defaultLanguage": "en-US" } ```

2024-05-02_17-50

from formengine.

tulusibrahim avatar tulusibrahim commented on May 24, 2024

@tulusibrahim Hi!

Please explain what you intend to implement. What would be the end-user use case? To create dependent form fields with various options, refer to the comment below.

i want to have one custom action to fetch all data needed (hit multiple external API) and set it to multiple input within the form. can i do it?

from formengine.

Related Issues (16)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.