informel setvalues

by juliendelort

HTML

<script src="https://unpkg.com/informel"></script>

<inform-el>
  <form>
    <label>
      First Name
      <inform-field>
        <input type="text" name="firstName" required value="John" />
      </inform-field>
    </label>
    <label>
      Last Name
      <inform-field>
        <input type="text" name="lastName" required />
      </inform-field>
    </label>
    <button type="submit">Submit</button>
    <button id="setvalues" type="button">Set values!</button>
  </form>
</inform-el>

<div id="dirty">
  Dirty: false
</div>

<script>
  const informEl = document.querySelector('inform-el');

  document.querySelector('#setvalues').addEventListener('click', (e) => {
    informEl.setValues({
      firstName: '',
      lastName: 'Doe'
    });

    setTimeout(() => {
      // Dirty state is updated asynchronously
      document.querySelector('#dirty').textContent = `Dirty: ${informEl.dirty}`;
    }, 0);
  });

  informEl.addEventListener('inform-input', () => {
    setTimeout(() => {
      // Dirty state is updated asynchronously
      document.querySelector('#dirty').textContent = `Dirty: ${informEl.dirty}`;
    }, 0);
  })

</script>

CSS

label {
  display: block;
  margin-bottom: 20px;
}
body {
  background: #f6f8fa;
  font-family: arial;
  padding: 20px;
}

inform-el {
  --error-margin: 5px 0; 

}

#dirty {
  margin-top: 20px;
}