# Input.form Template option modification and examples

**URL:** <https://talk.observablehq.com/t/input-form-template-option-modification-and-examples/7762>\
**Category:** Help\
**Created:** [March 18, 2023, 5:23pm UTC](https://talk.observablehq.com/t/input-form-template-option-modification-and-examples/7762 "2023-03-18T17:23:18Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ciscorucinski](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/ciscorucinski/32/4305_2.png) [@ciscorucinski](https://talk.observablehq.com/u/ciscorucinski)\
**Post date:** [March 18, 2023, 5:23pm UTC](https://talk.observablehq.com/t/input-form-template-option-modification-and-examples/7762/1 "2023-03-18T17:23:18Z")

</div>

For the [Documentation of Inputs.form()](https://github.com/observablehq/inputs/blob/main/README.md#inputsforminputs-options), it indicates that we can use a `template` to rearrange how the form controls are laid out.

The template code seems to show an overly simple example such as

```auto
function arrayTemplate(inputs) {
  return html`<div>${inputs}`;
}
...

function objectTemplate(inputs) {
  return html`<div>${Object.values(inputs)}`;
}

```

My understanding is that we would just be passing in the html literal value, I just included the whole functions as-is. The biggest issue is that I am relearning HTML and CSS and I am uncertain how to interop between the html and the `Input.form` controls

As an example, right now I have the following `Input.form`, and I am wondering how I would construct a `template`.

```auto
viewof modifier = Inputs.form({
  width: Inputs.range([0.1, 1], {
    label: "Tree Width Modifier",
    value: 1,
    step: 0.1
  }),
  height: Inputs.range([0.6, 4], {
    label: "Tree Height Modifier",
    value: 1,
    step: 0.1
  }),
  IndicatorEnabled: Inputs.toggle({
    label: "Enable Indicators?",
    value: false
  }),
  weekIndicator: Inputs.date({
    label: "Indicators Since Week Of:",
    min: "2021-11-24",
    max: new Date()
  })
})

```

Are there some good notebook examples that aren’t linked in the documentation? I assume after seeing some examples, I could create some `template`s by myself.

For some concrete layout ideas for templates:

1. How would I go about creating 2 colomns and grouping the last 2 `Input.form` controls together and shifting them to the top-right?
2. Is it possible to control the sizing of each of the 3 components for the `Inputs.range` control?
3. What about having 3 columns where the `Input.toggle` is in the middle and the `Input.date` along with an `Input.color` control are grouped together in the 3rd column?

Edit: One thing that I assume is that I don’t have to define the whole html inputs using pure html only, as can be seen in [Form Input / Mike Bostock | Observable](https://observablehq.com/@mbostock/form-input). I assume there is some interop between the html literal and the existing Input controls within `Input.form`

---

<div class="post-metadata">

**Author:** ![mootari](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/mootari/32/581_2.png) [@mootari](https://talk.observablehq.com/u/mootari)\
**Post date:** [March 19, 2023, 1:35pm UTC](https://talk.observablehq.com/t/input-form-template-option-modification-and-examples/7762/2 "2023-03-19T13:35:45Z")

</div>

> [@ciscorucinski](#):
>
> How would I go about creating 2 colomns and grouping the last 2 `Input.form` controls together and shifting them to the top-right?

For example by using the following template callback:

```javascript
({IndicatorEnabled, weekIndicator, ...inputs}) => htl.html`<div style="display:flex; gap: 20px; justify-content: space-between">
  <div>${Object.values(inputs)}</div>
  <div>${IndicatorEnabled}${weekIndicator}</div>
`

```

> [@ciscorucinski](#):
>
> Is it possible to control the sizing of each of the 3 components for the `Inputs.range` control?

You can pass in the `width` option to each, but this will also scale the label. Ideally you’d set a custom scope class on your container element (e.g. via `DOM.uid().id`) and insert a `<style>` element into your container that defines some local overrides. Have a look at [this `addCss` helper](https://observablehq.com/@mootari/phenakistiscope).

For an example on how to target Inputs styles, see [this notebook](https://observablehq.com/@mootari/arc-flip).

> [@ciscorucinski](#):
>
> What about having 3 columns where the `Input.toggle` is in the middle and the `Input.date` along with an `Input.color` control are grouped together in the 3rd column?

In general, you can do pretty much anything. Some examples:

- [a tabs helper](https://observablehq.com/@mootari/tabs) (and [this example](https://observablehq.com/@ambassadors/whats-that-noise) using an earlier version of it)
- [a submit/reset helper](https://observablehq.com/@mootari/inputs-submit)
- [an input sort helper](https://observablehq.com/@mootari/sort-inputs)

At some point however it makes more sense to use native inputs instead of relying on Inputs widgets. Also keep in mind that you can pass arrays and and nest Inputs.form() calls, which further expands your options.

---

<div class="post-metadata">

**Author:** ![ciscorucinski](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/ciscorucinski/32/4305_2.png) [@ciscorucinski](https://talk.observablehq.com/u/ciscorucinski)\
**Post date:** [March 19, 2023, 2:29pm UTC](https://talk.observablehq.com/t/input-form-template-option-modification-and-examples/7762/3 "2023-03-19T14:29:31Z")

</div>

Thank you for all the examples! I got the 2 column template up and running. It took a few tries to get rid of any errors. The rest is just my commentary trying to summarize what I believe is happening for others that come across similar issues.

Note: there are many aspects of javascript, html, and css that I am unsure of.

* * *

Here is the full `Input.form` code as I think looking at the whole thing is so interesting.

```javascript
viewof modifier = Inputs.form(
  {
    width: Inputs.range([0.2, 1], {
      label: "Width Multiplier",
      value: 1,
      step: 0.1
    }),
    height: Inputs.range([0.6, 4], {
      label: "Height Coefficient",
      value: 1,
      step: 0.1
    }),
    IndicatorEnabled: Inputs.toggle({
      label: "Enable Indicators?",
      value: false
    }),
    weekIndicator: Inputs.date({
      label: "Since Week Of:",
      min: "2021-11-24",
      max: new Date()
    })
  },
  {
    template: ({ IndicatorEnabled, weekIndicator, ...inputs }) =>
      htl.html`<div style="display:flex; gap: 20px; justify-content: space-between; width: 650px">
        <div>${Object.values(inputs)}</div>
        <div>${IndicatorEnabled}${weekIndicator}</div>`
  }
)

```

* * *

_ **Note:** Just for reference, here is `Input.form`’s signature_

```auto
Inputs.form(inputs, options)

```

I had some trouble integrating the template correctly into the arguments. I am using the Object syntax for `inputs`, so I needed 2 objects. This is where most of my errors when integrating the suggested code above came from; not using 2 separate objects.

* * *

```auto
{
    template: ({ IndicatorEnabled, weekIndicator, ...inputs }) =>
      htl.html`<div style="display:flex; gap: 20px; justify-content: space-between; width: 650px">
        <div>${Object.values(inputs)}</div>
        <div>${IndicatorEnabled}${weekIndicator}</div>`
  }

```

So for the lambda, since I wanted to specifically modify the last 2 items in my `inputs` object, you passed the names of only the last 2 items along with the rest of the `inputs`.

Note: using `...inputs` like is shown no longer contains the last 2 items. That is why using `<div>${Object.values(inputs)}</div>` doesn’t just output all 4 inputs. That is cool!

---

<div class="post-metadata">

**Author:** ![mootari](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/mootari/32/581_2.png) [@mootari](https://talk.observablehq.com/u/mootari)\
**Post date:** [March 19, 2023, 2:50pm UTC](https://talk.observablehq.com/t/input-form-template-option-modification-and-examples/7762/4 "2023-03-19T14:50:35Z")

</div>

You can read more about the “rest property” [on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment#rest_property). I recommend to read up about the spread syntax as well, since both are valuable language features to keep your code concise.

When things start to get too convoluted it can also help to skip the short arrow form and instead use a function body:

```javascript
template: (inputs) => {
  const left = Object.values(inputs).slice(0, -2);
  const right = Object.values(inputs).slice(-2);
  return htl.html`<div style="display:flex; gap: 20px; justify-content: space-between; width: 650px">
        <div>${left}</div>
        <div>${right}</div>`;
}

```

CSS has quite a few options to arrange elements (e.g. [grid](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Grid_Layout), [columns](https://developer.mozilla.org/en-US/docs/Web/CSS/Layout_cookbook/Column_layouts)) so that it may not even be necessary to pick out individual inputs.

---

<div class="post-metadata">

**Author:** ![ciscorucinski](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/ciscorucinski/32/4305_2.png) [@ciscorucinski](https://talk.observablehq.com/u/ciscorucinski)\
**Post date:** [March 20, 2023, 12:23pm UTC](https://talk.observablehq.com/t/input-form-template-option-modification-and-examples/7762/5 "2023-03-20T12:23:57Z")

</div>

Ahh, we can rename `inputs` whatever we want to in this instance; it doesn’t have to be called `inputs`. One reason I posted the signature of `Inputs.form` was because I thought it grabbed that content somehow and was using a spread operator, but that is just destructuring + rest property syntax. That makes a lot more sense now.

And again, thanks for the alternative solution and the pointer for CSS.
