|
1 | 1 | # Inputs
|
2 | 2 |
|
3 |
| -As described above, this function displays the given `input` and then returns its corresponding generator via `Generators.input`. Use this to display an input element while also declaring the input’s current value as a reactive top-level variable. |
| 3 | +You can use the built-in [`view` function](#view(input)) and an HTML input element to create a reactive input. For example, here is a slider: |
| 4 | + |
| 5 | +```js show |
| 6 | +const gain = view(Inputs.range([0, 11], {value: 5, step: 0.1, label: "Gain"})); |
| 7 | +``` |
| 8 | + |
| 9 | +Now you can reference the input’s value (here `gain`) anywhere. The code will run whenever the input changes; no event listeners required! |
| 10 | + |
| 11 | +```md |
| 12 | +The current gain is ${gain.toFixed(1)}! |
| 13 | +``` |
| 14 | + |
| 15 | +The current gain is ${gain.toFixed(1)}! |
4 | 16 |
|
5 | 17 | ## view(*input*)
|
| 18 | + |
| 19 | +As described above, this function [displays](./display) the given input element and then returns its corresponding [generator](./generators) via `Generators.input`. Use this to display an input element while also exposing the input’s value as a [reactive top-level variable](./reactivity). |
| 20 | + |
| 21 | +## Built-in inputs |
| 22 | + |
| 23 | +These basic inputs will get you started. |
| 24 | + |
| 25 | +* [Button](#button) - do something when a button is clicked |
| 26 | +* [Toggle](#toggle) - toggle between two values (on or off) |
| 27 | +* [Checkbox](#checkbox) - choose any from a set |
| 28 | +* [Radio](#radio) - choose one from a set |
| 29 | +* [Range](#range) or [Number](https://observablehq.com/@observablehq/input-range) - choose a number in a range (slider) |
| 30 | +* [Select](#select) - choose one or any from a set (drop-down menu) |
| 31 | +* [Text](#text) - enter freeform single-line text |
| 32 | +* [Textarea](#textarea) - enter freeform multi-line text |
| 33 | +* [Date](#date) or [Datetime](https://observablehq.com/@observablehq/input-date) - choose a date |
| 34 | +* [Color](#color) - choose a color |
| 35 | +* [File](#file) - choose a local file |
| 36 | + |
| 37 | +These fancy inputs are designed to work with tabular data such as CSV or TSV [file attachments](./files). |
| 38 | + |
| 39 | +* [Search](#search) - query a tabular dataset |
| 40 | +* [Table](#table) - browse a tabular dataset |
| 41 | + |
| 42 | +--- |
| 43 | + |
| 44 | +### Button |
| 45 | + |
| 46 | +Do something when a button is clicked. [Examples ›](https://observablehq.com/@observablehq/input-button) [API Reference ›](https://github.com/observablehq/inputs/blob/main/README.md#button) |
| 47 | + |
| 48 | +```js show |
| 49 | +const clicks = view(Inputs.button("Click me")); |
| 50 | +``` |
| 51 | + |
| 52 | +```js |
| 53 | +clicks |
| 54 | +``` |
| 55 | + |
| 56 | +--- |
| 57 | + |
| 58 | +### Toggle |
| 59 | + |
| 60 | +Toggle between two values (on or off). [Examples ›](https://observablehq.com/@observablehq/input-toggle) [API Reference ›](https://github.com/observablehq/inputs/blob/main/README.md#toggle) |
| 61 | + |
| 62 | +```js show |
| 63 | +const mute = view(Inputs.toggle({label: "Mute"})); |
| 64 | +``` |
| 65 | + |
| 66 | +```js |
| 67 | +mute |
| 68 | +``` |
| 69 | + |
| 70 | +--- |
| 71 | + |
| 72 | +### Checkbox |
| 73 | + |
| 74 | +Choose any from a set. [Examples ›](https://observablehq.com/@observablehq/input-checkbox) [API Reference ›](https://github.com/observablehq/inputs/blob/main/README.md#checkbox) |
| 75 | + |
| 76 | +```js show |
| 77 | +const flavors = view(Inputs.checkbox(["salty", "sweet", "bitter", "sour", "umami"], {label: "Flavors"})); |
| 78 | +``` |
| 79 | + |
| 80 | +```js |
| 81 | +flavors |
| 82 | +``` |
| 83 | + |
| 84 | +--- |
| 85 | + |
| 86 | +### Radio |
| 87 | + |
| 88 | +Choose one from a set. [Examples ›](https://observablehq.com/@observablehq/input-radio) [API Reference ›](https://github.com/observablehq/inputs/blob/main/README.md#radio) |
| 89 | + |
| 90 | +```js show |
| 91 | +const flavor = view(Inputs.radio(["salty", "sweet", "bitter", "sour", "umami"], {label: "Flavor"})); |
| 92 | +``` |
| 93 | + |
| 94 | +```js |
| 95 | +flavor |
| 96 | +``` |
| 97 | + |
| 98 | +--- |
| 99 | + |
| 100 | +### Range |
| 101 | + |
| 102 | +Pick a number. [Examples ›](https://observablehq.com/@observablehq/input-range) [API Reference ›](https://github.com/observablehq/inputs/blob/main/README.md#range) |
| 103 | + |
| 104 | +```js show |
| 105 | +const n = view(Inputs.range([0, 255], {step: 1, label: "Favorite number"})); |
| 106 | +``` |
| 107 | + |
| 108 | +```js |
| 109 | +n |
| 110 | +``` |
| 111 | + |
| 112 | +--- |
| 113 | + |
| 114 | +### Select |
| 115 | + |
| 116 | +Choose one, or any, from a menu. [Examples ›](https://observablehq.com/@observablehq/input-select) [API Reference ›](https://github.com/observablehq/inputs/blob/main/README.md#select) |
| 117 | + |
| 118 | +```js |
| 119 | +const capitals = FileAttachment("us-state-capitals.tsv").tsv({typed: true}); |
| 120 | +const stateNames = capitals.then((capitals) => capitals.map(d => d.State)); |
| 121 | +``` |
| 122 | + |
| 123 | +```js show |
| 124 | +const homeState = view(Inputs.select([null].concat(stateNames), {label: "Home state"})); |
| 125 | +``` |
| 126 | + |
| 127 | +```js |
| 128 | +homeState |
| 129 | +``` |
| 130 | + |
| 131 | +```js show |
| 132 | +const visitedStates = view(Inputs.select(stateNames, {label: "Visited states", multiple: true})); |
| 133 | +``` |
| 134 | + |
| 135 | +```js |
| 136 | +visitedStates |
| 137 | +``` |
| 138 | + |
| 139 | +--- |
| 140 | + |
| 141 | +### Text |
| 142 | + |
| 143 | +Enter freeform single-line text. [Examples ›](https://observablehq.com/@observablehq/input-text) [API Reference ›](https://github.com/observablehq/inputs/blob/main/README.md#text) |
| 144 | + |
| 145 | +```js show |
| 146 | +const name = view(Inputs.text({label: "Name", placeholder: "What’s your name?"})); |
| 147 | +``` |
| 148 | + |
| 149 | +```js |
| 150 | +name |
| 151 | +``` |
| 152 | + |
| 153 | +--- |
| 154 | + |
| 155 | +### Textarea |
| 156 | + |
| 157 | +Enter freeform multi-line text. [Examples ›](https://observablehq.com/@observablehq/input-textarea) [API Reference ›](https://github.com/observablehq/inputs/blob/main/README.md#textarea) |
| 158 | + |
| 159 | +```js show |
| 160 | +const bio = view(Inputs.textarea({label: "Biography", placeholder: "What’s your story?"})); |
| 161 | +``` |
| 162 | + |
| 163 | +```js |
| 164 | +bio |
| 165 | +``` |
| 166 | + |
| 167 | +--- |
| 168 | + |
| 169 | +### Date |
| 170 | + |
| 171 | +Choose a date, or a date and time. [Examples ›](https://observablehq.com/@observablehq/input-date) [API Reference ›](https://github.com/observablehq/inputs/blob/main/README.md#date) |
| 172 | + |
| 173 | +```js show |
| 174 | +const birthday = view(Inputs.date({label: "Birthday"})); |
| 175 | +``` |
| 176 | + |
| 177 | +```js |
| 178 | +birthday |
| 179 | +``` |
| 180 | + |
| 181 | +--- |
| 182 | + |
| 183 | +### Color |
| 184 | + |
| 185 | +Choose a color. [Examples ›](https://observablehq.com/@observablehq/input-color) [API Reference ›](https://github.com/observablehq/inputs/blob/main/README.md#color) |
| 186 | + |
| 187 | +```js show |
| 188 | +const color = view(Inputs.color({label: "Favorite color", value: "#4682b4"})); |
| 189 | +``` |
| 190 | + |
| 191 | +```js |
| 192 | +color |
| 193 | +``` |
| 194 | + |
| 195 | +--- |
| 196 | + |
| 197 | +### File |
| 198 | + |
| 199 | +Choose a local file. [Examples ›](https://observablehq.com/@observablehq/input-file) [API Reference ›](https://github.com/observablehq/inputs/blob/main/README.md#file) |
| 200 | + |
| 201 | +```js show |
| 202 | +const file = view(Inputs.file({label: "CSV file", accept: ".csv", required: true})); |
| 203 | +``` |
| 204 | + |
| 205 | +```js |
| 206 | +data = file.csv({typed: true}) |
| 207 | +``` |
| 208 | + |
| 209 | +--- |
| 210 | + |
| 211 | +### Search |
| 212 | + |
| 213 | +Query a tabular dataset. [Examples ›](https://observablehq.com/@observablehq/input-search) [API Reference ›](https://github.com/observablehq/inputs/blob/main/README.md#search) |
| 214 | + |
| 215 | +```js show |
| 216 | +const search = view(Inputs.search(capitals, {placeholder: "Search U.S. capitals"})); |
| 217 | +``` |
| 218 | + |
| 219 | +```js |
| 220 | +search // see table below! |
| 221 | +``` |
| 222 | + |
| 223 | +--- |
| 224 | + |
| 225 | +### Table |
| 226 | + |
| 227 | +Browse a tabular dataset. [Examples ›](https://observablehq.com/@observablehq/input-table) [API Reference ›](https://github.com/observablehq/inputs/blob/main/README.md#table) |
| 228 | + |
| 229 | +```js show |
| 230 | +const rows = view(Inputs.table(search)); |
| 231 | +``` |
| 232 | + |
| 233 | +```js |
| 234 | +rows // click a checkbox in the leftmost column |
| 235 | +``` |
| 236 | + |
| 237 | +--- |
| 238 | + |
| 239 | +* [Form](https://observablehq.com/@observablehq/input-form?collection=@observablehq/inputs) - combine multiple inputs for a compact display |
0 commit comments