Skip to content

Commit 2fe2fbc

Browse files
allisonhorstAllison Horst
and
Allison Horst
authored
updates Inputs individual pages (#540)
Co-authored-by: Allison Horst <[email protected]>
1 parent 54a696f commit 2fe2fbc

16 files changed

+440
-12022
lines changed

docs/inputs/athletes.csv

Lines changed: 0 additions & 11539 deletions
This file was deleted.

docs/inputs/button.md

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Button input
22

3-
A Button emits an *input* event when you click it. Buttons may be used to trigger the evaluation of cells, say to restart an animation.
3+
[API Reference ›](https://github.com/observablehq/inputs/blob/main/README.md#button)
4+
5+
The button input emits an *input* event when you click it. Buttons may be used to trigger the evaluation of cells, say to restart an animation.
46

57
For example, below is an animation (using [yield](../javascript/generators)) that progressively hides a bar.
68

@@ -81,13 +83,13 @@ const counter = view(Inputs.button([
8183
counter
8284
```
8385

84-
The first argument to Button is the contents of the button. It’s not required, but it’s strongly encouraged.
86+
The first argument to `Inputs.button()` is the contents of the button. It’s not required, but it’s strongly encouraged.
8587

8688
```js echo
8789
const x = view(Inputs.button());
8890
```
8991

90-
The contents of the Button can be an HTML element if desired, say for control over typography.
92+
The contents of the button input can be an HTML element if desired, say for control over typography.
9193

9294
```js echo
9395
const y = view(Inputs.button(html`<i>Fancy</i>`));
@@ -100,6 +102,7 @@ const confirm = view(Inputs.button("OK", {label: "Continue?"}));
100102
```
101103

102104
You can change the rendered text in Markdown based on whether a button is clicked. Try clicking the `OK` button with the `Continue?` label.
105+
103106
```md echo run=false
104107
confirm ? "Confirmed!" : "Awaiting confirmation..."
105108
```
@@ -110,4 +113,17 @@ You can also use a button to copy something to the clipboard.
110113

111114
```js echo
112115
Inputs.button("Copy to clipboard", {value: null, reduce: () => navigator.clipboard.writeText(time)})
113-
```
116+
```
117+
118+
## Options
119+
120+
**Inputs.button(*content*, *options*)**
121+
122+
The available button input options are:
123+
124+
* *label* - a label; either a string or an HTML element.
125+
* *required* - if true, the initial value defaults to undefined.
126+
* *value* - the initial value; defaults to 0 or null if *required* is false.
127+
* *reduce* - a function to update the value on click; by default returns *value* + 1.
128+
* *width* - the width of the input (not including the label).
129+
* *disabled* - whether input is disabled; defaults to false.

docs/inputs/checkbox.md

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
# Checkbox input
22

3-
A Checkbox allows the user to choose any of a given set of values. (See [Radio](./radio) for single-choice.) A Checkbox is recommended over a [Select](./select) when the number of values to choose from is small — say, seven or fewer — because all choices will be visible up-front, improving usability. For zero or one choice, see [Toggle](./toggle).
3+
[API Reference ›](https://github.com/observablehq/inputs/blob/main/README.md#checkbox)
44

5-
The initial value of a Checkbox defaults to an empty array. You can override this by specifying the *value* option, which should also be an array (or iterable).
5+
The checkbox input allows the user to choose any of a given set of values. (See the [radio](./radio) input for single-choice.) A checkbox is recommended over a [select](./select) input when the number of values to choose from is small — say, seven or fewer — because all choices will be visible up-front, improving usability. For zero or one choice, see the [toggle](./toggle) input.
6+
7+
The initial value of a checkbox defaults to an empty array. You can override this by specifying the *value* option, which should also be an array (or iterable).
68

79
```js echo
810
const colors = view(Inputs.checkbox(["red", "green", "blue"], {label: "color"}));
@@ -16,7 +18,7 @@ colors
1618
<div style="display: flex;">${colors.map(color => html`<div style="background-color: ${color}; width: 25px; height: 25px;">`)}
1719
```
1820

19-
A Checkbox’s values need not be strings: they can be anything. Specify a *format* function to control how these values are presented to the reader.
21+
A checkbox’s values need not be strings: they can be anything. Specify a *format* function to control how these values are presented to the reader.
2022

2123
```js echo
2224
const teams = [
@@ -36,7 +38,7 @@ const watching = view(Inputs.checkbox(teams, {label: "Watching", format: x => x.
3638
watching
3739
```
3840

39-
A Checkbox can be disabled by setting the *disabled* option to true. Alternatively, specific options can be disabled by passing an array of values to disable.
41+
A checkbox can be disabled by setting the *disabled* option to true. Alternatively, specific options can be disabled by passing an array of values to disable.
4042

4143
```js echo
4244
const vowels = view(Inputs.checkbox([..."AEIOUY"], {label: "Vowel", disabled: ["Y"]}));
@@ -46,7 +48,7 @@ const vowels = view(Inputs.checkbox([..."AEIOUY"], {label: "Vowel", disabled: ["
4648
vowels
4749
```
4850

49-
The *format* function, like the *label*, can return either a text string or an HTML element. This allows extensive control over the appearance of the Checkbox, if desired.
51+
The *format* function, like the *label*, can return either a text string or an HTML element. This allows extensive control over the appearance of the checkbox, if desired.
5052

5153
```js echo
5254
const colors2 = view(Inputs.checkbox(["red", "green", "blue"], {value: ["red"], label: html`<b>Colors</b>`, format: x => html`<span style="text-transform: capitalize; border-bottom: solid 2px ${x}; margin-bottom: -2px;">${x}`}));
@@ -56,7 +58,7 @@ const colors2 = view(Inputs.checkbox(["red", "green", "blue"], {value: ["red"],
5658
colors2
5759
```
5860

59-
If the Checkbox’s data are specified as a Map, the values will be the map’s values while the keys will be the displayed options. (This behavior can be customized by passing *keyof* and *valueof* function options.) Below, the displayed sizes are named, but the value is the corresponding number of fluid ounces.
61+
If the checkbox’s data are specified as a Map, the values will be the map’s values while the keys will be the displayed options. (This behavior can be customized by passing *keyof* and *valueof* function options.) Below, the displayed sizes are named, but the value is the corresponding number of fluid ounces.
6062

6163
```js echo
6264
const sizes = view(Inputs.checkbox(new Map([["Short", 8], ["Tall", 12], ["Grande", 16], ["Venti", 20]]), {value: [12], label: "Size"}));
@@ -79,28 +81,40 @@ const size2 = view(Inputs.checkbox(
7981
size2
8082
```
8183

82-
<!--[TODO] check if okay, removed link to Hello, Inputs. -->
83-
84-
Passing a Map to Checkbox is especially useful in conjunction with [d3.group](https://observablehq.com/@d3/d3-group). For example, given a tabular dataset of Olympic athletes, we can use d3.group to group them by gold medal count, and then Checkbox to select the athletes for the chosen count. Note that the value of the Checkbox will be an array of arrays, since d3.group returns a Map from key to array; use [*array*.flat](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat) to merge these arrays if desired.
85-
86-
```js echo
87-
const athletes = FileAttachment("athletes.csv").csv({typed: true});
88-
```
84+
Passing a Map to checkbox is especially useful in conjunction with [d3.group](https://d3js.org/d3-array/group). For example, given a the sample `olympians` dataset of Olympic athletes, we can use d3.group to group them by gold medal count, and then checkbox to select the athletes for the chosen count. Note that the value of the checkbox will be an array of arrays, since d3.group returns a Map from key to array; use [*array*.flat](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat) to merge these arrays if desired.
8985

9086
```js echo
91-
const goldAthletes = view(Inputs.checkbox(d3.group(athletes, d => d.gold), {label: "Gold medal count", sort: "descending", key: [4, 5]}));
87+
const goldAthletes = view(Inputs.checkbox(d3.group(olympians, d => d.gold), {label: "Gold medal count", sort: "descending", key: [4, 5]}));
9288
```
9389

9490
```js echo
9591
goldAthletes.flat()
9692
```
9793

98-
If the *sort* and *unique* options are specified, the Checkbox’s keys will be sorted and duplicate keys will be discarded, respectively.
94+
If the *sort* and *unique* options are specified, the checkbox’s keys will be sorted and duplicate keys will be discarded, respectively.
9995

10096
```js echo
10197
const bases = view(Inputs.checkbox("GATTACA", {sort: true, unique: true}));
10298
```
10399

104100
```js echo
105101
bases
106-
```
102+
```
103+
104+
## Options
105+
106+
**Inputs.checkbox(*data*, *options*)**
107+
108+
The available checkbox input options are:
109+
110+
* *label* - a label; either a string or an HTML element.
111+
* *sort* - true, “ascending”, “descending”, or a comparator function to sort keys; defaults to false.
112+
* *unique* - true to only show unique keys; defaults to false.
113+
* *locale* - the current locale; defaults to English.
114+
* *format* - a format function; defaults to [formatLocaleAuto](https://github.com/observablehq/inputs?tab=readme-ov-file#formatLocaleAuto) composed with *keyof*.
115+
* *keyof* - a function to return the key for the given element in *data*.
116+
* *valueof* - a function to return the value of the given element in *data*.
117+
* *value* - the initial value, an array; defaults to an empty array (no selection).
118+
* *disabled* - whether input is disabled, or the disabled values; defaults to false.
119+
120+
<!-- TODO check formatLocaleAuto link-->

docs/inputs/color.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Color input
22

3-
The Color input specifies an RGB color as a hexadecimal string `#rrggbb`. The initial value defaults to black (`#000000`) and can be specified with the *value* option.
3+
[API Reference ›](https://github.com/observablehq/inputs/blob/main/README.md#color)
4+
5+
The color input specifies an RGB color as a hexadecimal string `#rrggbb`. The initial value defaults to black (`#000000`) and can be specified with the *value* option.
46

57
```js echo
68
const color = view(Inputs.color({label: "Favorite color", value: "#4682b4"}));
@@ -18,7 +20,7 @@ const fill = view(Inputs.color({label: "Fill", value: d3.color("steelblue").form
1820

1921
If you specify the *datalist* option as an array of hexadecimal color strings, the color picker will show this set of colors for convenient picking. (The user will still be allowed to pick another color, however; if you want to limit the choice to a specific set, then a radio or select input may be more appropriate.)
2022

21-
<!-- [TODO] update to the new Observable color palette? -->
23+
<!-- [TODO] update to the new Observable10 color palette? -->
2224

2325
```js echo
2426
const stroke = view(Inputs.color({label: "Stroke", datalist: d3.schemeTableau10}));
@@ -36,4 +38,10 @@ const disabled = view(Inputs.color({label: "Disabled", value: "#f28e2c", disable
3638

3739
```js echo
3840
disabled
39-
```
41+
```
42+
43+
## Options
44+
45+
**Inputs.color(*options*)**
46+
47+
Like [Inputs.text](./text), but where *type* is color. The color value is represented as an RGB hexadecimal string such as #ff00ff. This type of input does not support the following options: *placeholder*, *pattern*, *spellcheck*, *autocomplete*, *autocapitalize*, *min*, *max*, *minlength*, *maxlength*.

docs/inputs/date.md

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Date input
22

3-
The Date input specifies a date.
3+
[API Reference ›](https://github.com/observablehq/inputs/blob/main/README.md#date)
4+
5+
The date input specifies a date.
46

57
```js echo
68
const date = view(Inputs.date());
@@ -76,4 +78,23 @@ const readonly = view(Inputs.date({label: "Readonly date", value: "2021-01-01",
7678

7779
```js echo
7880
readonly
79-
```
81+
```
82+
83+
## Options
84+
85+
**Inputs.date(*options*)**
86+
87+
The available date input options are:
88+
89+
* *label* - a label; either a string or an HTML element.
90+
* *value* - the initial value, as a JavaScript Date or formatted as an ISO string (yyyy-mm-dd); defaults to null.
91+
* *min* - [minimum value](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/min) attribute.
92+
* *max* - [maximum value](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/max) attribute.
93+
* *required* - if true, the input must be a valid date.
94+
* *validate* - a function to check whether the text input is valid.
95+
* *width* - the width of the input (not including the label).
96+
* *submit* - whether to require explicit submission before updating; defaults to false.
97+
* *readonly* - whether input is readonly; defaults to false.
98+
* *disabled* - whether input is disabled; defaults to false.
99+
100+
The value of the input is a Date instance at UTC midnight of the specified date, or null if no (valid) value has been specified. Note that the displayed date format is [based on the browser’s locale](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/date).

docs/inputs/file.md

Lines changed: 93 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,101 @@
11
# File input
22

3-
<!-- [TODO] check okay, updated to point to /javascript/files rather than FileAttachment info in docs -->
3+
[API Reference ›](https://github.com/observablehq/inputs/blob/main/README.md#file)
44

5-
The File input specifies a local file. The exposed value provides the same interface as an Observable [file attachment](../javascript/files) for convenient parsing in various formats such as text, image, JSON, CSV, ZIP, and XLSX; however, the file is not uploaded and is only available temporarily in memory.
5+
The file input specifies a local file. The exposed value provides the same interface as an Observable [file attachment](../javascript/files) for convenient parsing in various formats such as text, image, JSON, CSV, ZIP, and XLSX; however, the file is not uploaded and is only available temporarily in memory.
66

77
By default, any file is allowed, and the value of the input resolves to null.
88

9-
<!-- [TODO] check error, return to File input after hearing back (Fil has PR submitted) -->
9+
```js echo
10+
const file = view(Inputs.file());
11+
```
12+
13+
```js echo
14+
file
15+
```
16+
17+
We recommend providing a *label* to improve usability.
18+
19+
Specify the *accept* option to limit the allowed file extensions. This is useful when you intend to parse the file as a specific file format, such as CSV. By setting the *required* option to true, the value of the input won’t resolve until the user choses a file.
1020

1121
```js echo
12-
const file = view(Inputs.file())
13-
```
22+
const csvfile = view(Inputs.file({label: "CSV file", accept: ".csv", required: true}));
23+
```
24+
25+
Once a file has been selected, you can read its contents like so:
26+
27+
28+
```js echo
29+
const data = display(await csvfile.csv({typed: true}));
30+
```
31+
32+
Here are examples of other supported file types.
33+
34+
```js echo
35+
const jsonfile = view(Inputs.file({label: "JSON file", accept: ".json", required: true}));
36+
```
37+
38+
```js echo
39+
const data = display(await jsonfile.json());
40+
```
41+
42+
```js echo
43+
const textfile = view(Inputs.file({label: "Text file", accept: ".txt", required: true}));
44+
```
45+
46+
```js echo
47+
const data = display(await textfile.text());
48+
```
49+
50+
```js echo
51+
const imgfile = view(Inputs.file({label: "Image file", accept: ".png,.jpg", required: true}));
52+
```
53+
54+
```js echo
55+
const image = display(await imgfile.image());
56+
```
57+
58+
```js echo
59+
const xlsxfile = view(Inputs.file({label: "Excel file", accept: ".xlsx", required: true}));
60+
```
61+
62+
```js echo
63+
const workbook = display(await xlsxfile.xlsx());
64+
```
65+
66+
```js echo
67+
const zipfile = view(Inputs.file({label: "ZIP archive", accept: ".zip", required: true}));
68+
```
69+
70+
```js echo
71+
const archive = display(await zipfile.zip())
72+
```
73+
74+
The *multiple* option allows the user to pick multiple files. In this mode, the exposed value is an array of files instead of a single file.
75+
76+
```js echo
77+
const files = view(Inputs.file({label: "Files", multiple: true}));
78+
```
79+
80+
```js echo
81+
files
82+
```
83+
84+
## Options
85+
86+
**Inputs.file(*options*)**
87+
88+
The available file input options are:
89+
90+
* *label* - a label; either a string or an HTML element.
91+
* *required* - if true, a valid file must be selected.
92+
* *validate* - a function to check whether the file input is valid.
93+
* *accept* - the [acceptable file types](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/file#accept).
94+
* *capture* - for [capturing image or video data](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/file#capture).
95+
* *multiple* - whether to allow multiple files to be selected; defaults to false.
96+
* *width* - the width of the input (not including the label).
97+
* *disabled* - whether input is disabled; defaults to false.
98+
99+
Note that the value of file input cannot be set programmatically; it can only be changed by the user.
100+
101+
<!-- TODO check: Delete? (In vanilla JavaScript, the Inputs.file method is not exposed directly. Instead, an Inputs.fileOf method is exposed which takes an AbstractFile implementation and returns the Inputs.file method. This avoids a circular dependency between Observable Inputs and the Observable standard library.)-->

docs/inputs/form.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Form input
22

3-
The Form input combines a number of inputs into a single compound input. It’s intended for a more compact display of closely-related inputs, say for a color’s red, green, and blue channels.
3+
[API Reference ›](https://github.com/observablehq/inputs/blob/main/README.md#inputsforminputs-options)
4+
5+
The form input combines a number of inputs into a single compound input. It’s intended for a more compact display of closely-related inputs, say for a color’s red, green, and blue channels.
46

57
```js echo
68
const rgb = view(Inputs.form([
@@ -26,4 +28,14 @@ const rgb2 = view(Inputs.form({
2628

2729
```js echo
2830
rgb2
29-
```
31+
```
32+
33+
## Options
34+
35+
**Inputs.form(*inputs*, *options*)**
36+
37+
The available form input options are:
38+
39+
* *template* - a function that takes the given *inputs* and returns an HTML element to display.
40+
41+
If the *template* object is not specified, the given inputs are wrapped in a DIV.

0 commit comments

Comments
 (0)