# Form — accessible headless form with validation and submission
#
# Wraps native <form> with submit handling, validation state, and
# loading indicator support. Prevents default submission and emits
# a 'submit' event. Ships zero CSS.
#
# Usage:
#   Form @submit: handleSubmit
#     Field label: "Name"
#       Input value <=> name
#     Button
#       "Submit"

export Form = component
  @disabled := false

  submitting := false
  submitted := false
  errors := {}

  _onSubmit: (e) ->
    e.preventDefault()
    return if @disabled or submitting
    submitting = true
    submitted = true
    @emit 'submit', { form: e.target }

  setErrors: (errs) ->
    errors = errs or {}

  setSubmitting: (val) ->
    submitting = val

  render
    form @submit: @_onSubmit, novalidate: true
      $disabled: @disabled?!
      $submitting: submitting?!
      $submitted: submitted?!
      slot
