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

Set custom renderer for a single hyperForm Instance #55

Open
n1ru4l opened this issue Apr 26, 2017 · 2 comments
Open

Set custom renderer for a single hyperForm Instance #55

n1ru4l opened this issue Apr 26, 2017 · 2 comments

Comments

@n1ru4l
Copy link

n1ru4l commented Apr 26, 2017

It is possible to set the global renderer (https://hyperform.js.org/examples.html#file=global_message).
I wanted to ask if it is possible to set a renderer for a single hyperform instance via the constructor. Otherwise this would be a feature request :)

@Boldewyn
Copy link
Contributor

Thanks for the suggestion! No, it's not possible currently, but I toyed with that thought myself. Out of interest, how would your use case look like? (I.e., what would a specific form renderer need, that a global one can't provide?)

@n1ru4l
Copy link
Author

n1ru4l commented Apr 26, 2017

I am currently implementing a form like in the example and I thought about the fact that in the future there could be an other form on the same page. In that situation, both should render their errors in their own error container. You could put some logic into the render function to do this - imho you could make this simpler by allowing to declaring the renderer per instance.

In my case I could do something like this, to create render functions for the different form instances:

function createGlobalRenderFunction(errorContainer) {
  return function attachWarningRenderer(warning, element) {
    const childNodeArray = Array.prototype.slice.apply(errorContainer.childNodes);
    if (childNodeArray.every(node => node.id !== warning.id)) {
      errorContainer.appendChild(warning)
    }
  }
}

const form1 = document.querySelector('[data-contact-form]')
const attachWarningRenderer1 = createGlobalRenderFunction(
  form1.querySelector('[data-errors]')
)
const $formInstance1 = hyperform(form1, { 
  renderer: { type: `attachWarning`, handler: attachWarningRenderer1 }
})

const form2 = document.querySelector('[data-contest-form]')
const attachWarningRenderer2 = createGlobalRenderFunction(
  form2.querySelector('[data-errors]')
)
const $formInstance2 = hyperform(form2, { 
  renderer: { type: `attachWarning`, handler: attachWarningRenderer2 }
})

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