UNPKG

3.55 kBMarkdownView Raw
1# Flourish controls
2
3Add interchangeable controls (dropdown, buttons or slider) to a page.
4
5## How to install
6
7`npm install -s @flourish/controls`
8
9Add one or more control objects to your template state:
10
11``` js
12export var state = {
13 controls: {},
14 ...
15}
16```
17
18Import `@flourish/controls` into your `template.yml` settings.
19
20
21## Basics
22
23Initialise the control outside any function:
24
25``` js
26import initControls from "@flourish/controls";
27var control = initControls(state.controls);
28```
29
30You can also pass in optional functions for getting parsing and formatting functions. The former is required if you want to use user input and the user is using "," as the decimal separator. The latter is required if you want to format numbers to use "," as a decimal separator. For example:
31
32``` js
33import initLocalization from "@flourish/number-localization";
34import initFormatter from "@flourish/number-formatter";
35import state from "../core/state";
36
37localization = initLocalization(state.localization);
38formatter = initFormatter(state.formatting);
39
40function getParser() {
41 return localization.getParser();
42}
43
44function getFormatter() {
45 return formatter(localization.getFormatterFunction());
46}
47
48var control = init(state.controls, getParser, getFormatter);
49```
50
51Append the control to a container and add an on change handler: `controls.appendTo(container).on("change", update);`. This is usually done in the `draw` function.
52
53In `update` you typically want to update the set of `options` and then call the controls own `update` method: `controls.options(options).update();`.
54
55## Methods
56
57### `appendTo(container, [bounding_container])`
58
59Appends the control to the `container` DOM node and injects CSS into the `head` if necessary. The optional `bounding_container` makes sure the dropdown list will never overflow outside that container.
60
61Returns the control object.
62
63### `getSortedIndex()`
64
65Returns the index of the currently chosen value in a sorted version of the options array.
66
67### `index([i])`
68
69With no argument returns the (unsorted) index of the currently selected option. If `i` is specified and corresponds to an index into the `options` array, sets the currently selected option to `i`. Returns the control object.
70
71### `n_options()`
72
73Returns the current number of options.
74
75### `on(event, callback)`
76
77Add `callback` to the list of `event` handlers. Currently the only supported `event` is "change". Returns the control object.
78
79### `options([arr])`
80
81With no argument returns a copy of the current list of options displayed by the current control. With an array, replaces the current options with a copy of `arr` and returns the control object.
82
83### `remove()`
84
85Removes the control (and its resize event handler) from the DOM. Returns the control object.
86
87### `trigger(event)`
88
89Calls all the handlers assigned to `event` and returns the control object. Currently the only supported `event` type is "change".
90
91### `update()`
92
93Rebuilds the control with latest settings. Returns the control object.
94
95### `value([val])`
96
97With no argument returns the string value of the currently selected option. With `val` present changes the current index to match that of `val` in the options array and returns the control object. If `val` is not in the options array then nothing is changed but the control object is still returned.
98
99## Styling the controls
100
101The controls have very basic styling. You can overwrite these styles with your own css and styling. We recommend using [@flourish/ui-styles](https://github.com/kiln/flourish-ui-styles) to style the controls.
102
103
104
105