How can I create a web component that acts like a form element?

时光总嘲笑我的痴心妄想 提交于 2019-11-28 13:35:38

You can create your <custom-input> custom element that will be interpreted by a form, just by adding inside your template a hidden input element with the name and value pair your want.

<template>
    <input type="hidden" name="foo" value="defaultVal">
</template>

The default value (and name) can by updated by your custom element internal logic.

This hidden input must not be inserted inside a Shadow DOM to be detected by the container form.

(function() {

  var iconDoc = (document._currentScript || document.currentScript).ownerDocument;
  var objectPrototype = Object.create(HTMLInputElement.prototype);

  objectPrototype.createdCallback = function() {
    //var shadow = this.createShadowRoot();
    var template = iconDoc.querySelector('#test');
    this.appendChild(template.content.cloneNode(true));
  };

  document.registerElement('custom-input', {
    prototype: objectPrototype
  });

})();

console.log(
  $('form').serialize()
)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<template id="test">
  <input type="hidden" name="foo" value="bar">
</template>

<form>
  <input name="regular" value="input">
  <custom-input name="foo" value="bar"></custom-input>
</form>

KevBot,

You seem to think that the element includes itself in the form. That is not the case. It is the form that searches its children elements by tag name, to decide which elements it should include. It will simply ignore those with unknown tag names.

Your custom-input name is not among the elements the form searches. Therefore, it is not included on the form. It doesn't matter the custom element's prototype. That's why it works if you use is, since then the tag name is maintained.

Of course, you may implement your own custom-form that behaves differently, if you want.

You can do this:

(function() {
  var newInputExtended = Object.create(HTMLInputElement.prototype);

  newInputExtended.createdCallback = function() {
    this.value = 'baz';
  };

  document.registerElement('foo-input', {
    extends: 'input',
    prototype: newInputExtended
  });
  
  
  window.something = function(form, event) {
    $('<p>')
    .text($(form).serialize())
    .appendTo('body')
  
    event.preventDefault();
  }
})();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form onsubmit="something(this, event)">
  <input is="foo-input" name="foo" value="bar">
  <button>Hi</button>
</form>

But you get an error if you try to create a new shadow root. It looks like you are limited to only extending the data/logic around the element's user-agent shadow root.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!