Inherit Field¶
Wraps any field with an Inherit state. While it inherits, the field shows the inherited value, dimmed and disabled, and the toggle reads "Inherit (value)". Override turns the field back on with its own value. The toggle is two pills on the pressed group core, or a checkbox.
Decision¶
| When | Use |
|---|---|
| A setting that falls back to a parent's value unless the reader overrides it, such as a per library value over a global one | Inherit Field |
| A setting with no parent value | The field alone |
Usage¶
import { createInheritField } from './configurationpage?name=myplugin_stolon.js';
var language = createInheritField({
field: view.querySelector('#libraryLanguage'),
inheritedValue: config.Language,
value: { inherit: library.Language === null, value: library.Language }
});
// On save: library.Language = language.getValue().inherit ? null : language.getValue().value;
Classes: stolon-inherit, stolon-inherit-field, stolon-inherit-toggle, and stolon-inheriting while it inherits. The handle is { element, getValue, setValue, setInherited, destroy }, and getValue() returns { inherit, value }, where value is the field's own value even while it inherits.
States and Modifiers¶
fieldis a select, text, number, or checkbox input, or a Stolon field handle{ element, getValue, setValue }. The field is moved into the wrapper where it stood.valueis{ inherit, value }to start with, inheriting by default. The field's own value is kept while it inherits and restored on Override.- The inherited value reads as a select's option text, On or Off for a checkbox, else the value.
inheritedTextreplaces it, andsetInherited(value, text)changes it. toggle: 'checkbox'shows an Override checkbox in place of the pills.inheritText("Inherit ({value})"),overrideText("Override"),onText("On"), andoffText("Off") replace the defaults.labelis the aria-label of the toggle group.- A field that inherits is disabled, so the validation core skips it.
Reference¶
Generated from the code by scripts/build-docs.mjs. Edit the code, not this section.
createInheritField¶
function createInheritField<V = unknown>(options: InheritFieldOptions<V>): InheritFieldHandle<V>
Wraps any field with an Inherit state. While it inherits, the field shows the inherited value, dimmed and disabled, and the toggle reads 'Inherit (value)'. The Override choice turns the field back on with its own value, which is kept while it inherits. The toggle is two pills on the pressed group, or a checkbox.
| Option | Type | What it does |
|---|---|---|
field (required) |
HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement | InheritableHandle<V> |
the select, text, number, or checkbox input, or a Stolon field handle { element, getValue, setValue } [required] |
inheritedValue |
V |
the value shown while inheriting [optional] |
inheritedText |
string |
how the inherited value reads in the toggle (default the chosen option's text for a select, onText or offText for a checkbox, else the value) |
value |
Partial<InheritValue<V>> | null |
{ inherit, value } to start with (default inheriting, with the field's own value) |
toggle |
'pressed' | 'checkbox' |
'pressed' for Inherit and Override pills, or 'checkbox' for an Override checkbox (default 'pressed') |
inheritText |
string |
the Inherit pill, with {value} as the inherited text (default 'Inherit ({value})') |
overrideText |
string |
the Override pill or checkbox label (default 'Override') |
onText |
string |
how a checked checkbox reads (default 'On') |
offText |
string |
how a cleared checkbox reads (default 'Off') |
label |
string |
aria-label of the toggle group [optional] |
onChange |
(value: InheritValue<V>): void |
function({ inherit, value }) after each change [optional] |
| Handle member | Type |
|---|---|
getValue |
(): InheritValue<V> |
setValue |
(next?: Partial<InheritValue<V>> | null): void |
setInherited |
(value: V, text?: string): void |
destroy |
(): void |
| Type | What it is |
|---|---|
InheritValue |
What an inherit field reports: whether it inherits, and its own value, kept while it inherits. |
InheritableHandle |
A Stolon field handle an inherit field can wrap, such as a Number Unit Field or a Color Picker. |
| Class | What it looks like |
|---|---|
stolon-inherit |
A field with an Inherit and Override toggle beside it, which wraps under the field where the line is too narrow |
stolon-inherit.stolon-check |
A checkbox field: its box and text a field tall and centered, on one line with the Override toggle beside it |
stolon-inherit.stolon-inheriting |
The field while it inherits, dimmed and disabled with the inherited value in it |
Example¶
Example Code¶
<!-- title: Inherit Field -->
<div class="stolon-field"><label for="lang">Metadata Language</label><select id="lang"><option value="en">English</option><option value="fr">French</option><option value="de">German</option></select></div>
<div class="stolon-field" style="margin-top: 16px;"><label for="workers">Workers</label><input id="workers" type="number" min="1" max="16"></div>
<div class="stolon-field" style="margin-top: 16px;"><label><input id="art" type="checkbox"><span>Save Artwork</span></label></div>
<script type="module">
const base = await window.__baseReady;
base.createInheritField({ field: document.getElementById('lang'), inheritedValue: 'en', value: { inherit: true, value: 'fr' } });
base.createInheritField({ field: document.getElementById('workers'), inheritedValue: 4, value: { inherit: false, value: 8 } });
base.createInheritField({ field: document.getElementById('art'), inheritedValue: true, toggle: 'checkbox' });
</script>