Tabs
<et2-tabbox> | Et2Tabs
et2-tabbox shows one of several panels at a time, with a row of tabs to switch between them. It
is what every “Edit” dialog in EGroupware uses to keep General / Links / History apart.
A tabbox is made of three widgets: the tabbox itself, one
et2-tab per tab in its nav slot, and one
et2-tab-panel per panel. A tab is tied to its panel by
panel="…" matching the panel’s name="…".
In a template you do not write those two yourself. You list the tabs and the panel contents in
<tabs> and <tabpanels> child tags and the tabbox creates the widgets,
pairing them up by position:
<et2-tabbox id="tabs">
<tabs>
<tab id="general" label="General"/>
<tab id="links" label="Links" statustext="Attachments and related entries"/>
<tab id="history" label="History"/>
</tabs>
<tabpanels>
<et2-template id="myapp.edit.general"/>
<et2-template id="myapp.edit.links"/>
<et2-template id="myapp.edit.history"/>
</tabpanels>
</et2-tabbox>
The id on each <tab> is the panel name, the tabbox’s own value, and the key
the server uses to hide a tab through readonlys. Give every tab one.
Examples
Tabs in plain HTML
Outside a template - in a test, or a hand-written page like this one - write the tabs and panels directly.
slot="nav" is what makes an et2-tab part of the tab row.
<et2-tabbox>
<et2-tab slot="nav" panel="general" active>General</et2-tab>
<et2-tab slot="nav" panel="links">Links</et2-tab>
<et2-tab-panel name="general" active>
<et2-textbox label="Subject"></et2-textbox>
</et2-tab-panel>
<et2-tab-panel name="links">
<et2-description value="Nothing linked yet."></et2-description>
</et2-tab-panel>
</et2-tabbox>
The value is the open tab
A tabbox is an input widget whose value is the name of the panel currently showing. Setting
value switches tabs, so a template can open on a particular tab just by putting its id in the
content array.
value:
<et2-tabbox id="tabs-value-example">
<et2-tab slot="nav" panel="one" active>One</et2-tab>
<et2-tab slot="nav" panel="two">Two</et2-tab>
<et2-tab slot="nav" panel="three">Three</et2-tab>
<et2-tab-panel name="one" active><et2-description value="First panel"></et2-description></et2-tab-panel>
<et2-tab-panel name="two"><et2-description value="Second panel"></et2-description></et2-tab-panel>
<et2-tab-panel name="three"><et2-description value="Third panel"></et2-description></et2-tab-panel>
</et2-tabbox>
<p>value: <span id="tabs-value-output"></span></p>
<et2-button id="tabs-value-button" label="Show the third tab"></et2-button>
<script>
const tabs = document.getElementById("tabs-value-example");
const out = document.getElementById("tabs-value-output");
const show = () => {out.textContent = tabs.value;};
tabs.addEventListener("sl-tab-show", show);
document.getElementById("tabs-value-button").addEventListener("click", () => {tabs.value = "three";});
customElements.whenDefined("et2-tabbox").then(() => tabs.updateComplete).then(show);
</script>
Where the tabs sit
placement moves the tab row. top is the default; start and
end put it down the side, which is what the mobile framework switches to by default.
<et2-tabbox placement="start">
<et2-tab slot="nav" panel="a" active>First</et2-tab>
<et2-tab slot="nav" panel="b">Second</et2-tab>
<et2-tab-panel name="a" active><et2-description value="Tabs on the left"></et2-description></et2-tab-panel>
<et2-tab-panel name="b"><et2-description value="Second panel"></et2-description></et2-tab-panel>
</et2-tabbox>
The older alignTabs attribute did the same thing and is deprecated - use
placement.
Panel height
Without tabHeight the tabbox measures its first panel once and pins every panel to that height,
so the dialog does not jump around when you switch tabs. Give tabHeight a value to set it
yourself, or auto to let the panels flex to whatever their parent gives them.
<et2-tabbox tabHeight="8em">
<et2-tab slot="nav" panel="short" active>Short</et2-tab>
<et2-tab slot="nav" panel="long">Long</et2-tab>
<et2-tab-panel name="short" active><et2-description value="One line."></et2-description></et2-tab-panel>
<et2-tab-panel name="long">
<et2-vbox>
<et2-description value="Rather"></et2-description>
<et2-description value="a lot"></et2-description>
<et2-description value="more"></et2-description>
<et2-description value="content"></et2-description>
</et2-vbox>
</et2-tab-panel>
</et2-tabbox>
Reacting to a tab change
sl-tab-show and sl-tab-hide carry the panel name in
event.detail.name. This is the hook for loading a tab’s data the first time it is opened.
last: none
<et2-tabbox id="tabs-event-example">
<et2-tab slot="nav" panel="first" active>First</et2-tab>
<et2-tab slot="nav" panel="second">Second</et2-tab>
<et2-tab-panel name="first" active><et2-description value="Switch tabs"></et2-description></et2-tab-panel>
<et2-tab-panel name="second"><et2-description value="and watch the log"></et2-description></et2-tab-panel>
</et2-tabbox>
<p>last: <span id="tabs-event-output">none</span></p>
<script>
const out = document.getElementById("tabs-event-output");
document.getElementById("tabs-event-example").addEventListener("sl-tab-show", (e) =>
{
out.textContent = "sl-tab-show " + e.detail.name;
});
</script>
Finding and opening the tab a widget is on
Validation has to be able to show the field it is complaining about even when the field is on a closed tab.
activateTab(widget) opens the tab containing a widget, and the static
Et2Tabs.getTabPanel(widget) tells you which panel a widget is on without opening anything -
pass true as the second argument for the tab’s label instead of its id.
// somewhere with a reference to the tabbox and the offending widget
tabbox.activateTab(widget);
Adding tabs from the server
extraTabs takes an array of {label, template} objects and appends them to the tabs
from the template - or replaces them, if addTabs is not set. Each entry may also carry
id, hidden, prepend (true for the front, or the id of
the tab to insert before) and statustext. This is how custom fields and app plugins get their
own tab.
$tpl->setElementAttribute('tabs', 'addTabs', true);
$tpl->setElementAttribute('tabs', 'extraTabs', [
['label' => 'Notes', 'template' => 'myapp.edit.notes', 'id' => 'notes'],
]);
Properties
| Name | Description | Reflects | Type | Default |
|---|---|---|---|---|
addTabs
|
Add tabs to template Set to true if tabs specified in tabs property should be added to tabs read from template, default false if not which replaces what’s in template |
boolean
|
false
|
|
extraTabs
|
Array of [extra] tabs. Each tab needs {label:…, template:…}. Additional optional keys are prepend, hidden and id, for access into content array |
any[]
|
[]
|
|
tabHeight
|
Set the height for tabs Leave unset to size automatically from either parent height attribute, or height of first tabpanel Set to ‘auto’ to allow tab height to flex to fill parent element |
|
string
|
- |
value
|
active tab is the value | - | - |
Learn more about attributes and properties.
Inherited properties (25)
Et2Widget
| Name | Description |
|---|---|
accesskey |
Accesskey provides a hint for generating a keyboard shortcut for the current element. The attribute value must consist of a single printable character. |
actions |
Set Actions on the widget Each action is defined as an object: move: { type: “drop”, acceptedTypes: “mail”, icon: “move”, caption: “Move to” onExecute: javascript:mail_move” } This will turn the widget into a drop target for “mail” drag types. When “mail” drag types are dropped, the global function mail_move(egwAction action, egwActionObject sender) will be called. The ID of the dragged “mail” will be in sender.id, some information about the sender will be in sender.context. The etemplate2 widget involved can typically be found in action.parent.data.widget, so your handler can operate in the widget context easily. The location varies depending on your action though. It might be action.parent.parent.data.widget To customise how the actions are handled for a particular widget, override _link_actions(). It handles the more widget-specific parts. |
align |
Used by Et2Box to determine alignment. Allowed values are left, right |
class |
CSS Class. This class is applied to the outside, on the web component itself. Due to how WebComponents work, this might not change anything inside the component. |
data |
Set the dataset from a CSV |
deferredProperties |
Any attribute that refers to row content cannot be resolved immediately, but some like booleans cannot stay a string because it’s a boolean attribute. We store them for later, and parse when they’re fully in their row. If you are creating a widget that can go in a nextmatch row, and it has boolean attributes that can change for each row, add those attributes into deferredProperties |
disabled |
Defines whether this widget is visibly disabled. The widget is still visible, but clearly cannot be interacted with. Widgets disabled in the template will not return a value to the application code, even if re-enabled via javascript before submitting. To allow a disabled widget to be re-enabled and return a value, disable via javascript in the app’s et2_ready() instead of an attribute in the template file. |
dom_id |
Get the actual DOM ID, which has been prefixed to make sure it’s unique. |
hidden |
The widget is not visible. As far as the user is concerned, the widget does not exist. Widgets hidden with an attribute in the template may not be created in the DOM, and will not return a value. Widgets can be hidden after creation, and they may return a value if hidden this way. |
id |
Get the ID of the widget |
label |
The label of the widget Legacy support for labels with %s that get wrapped around the widget Not the best way go with webComponents - shouldn’t modify their DOM like this |
noLang |
Disable any translations for the widget |
parentId |
Parent is different than what is specified in the template / hierarchy. Widget ID of another node to insert this node into instead of the normal location |
statustext |
Tooltip which is shown for this element on hover |
styles |
WebComponent * |
options |
Get property-values as object |
supportedWidgetClasses |
et2_widget compatability |
SlTabGroup (Shoelace)
| Name | Description |
|---|---|
activation |
When set to auto, navigating tabs with the arrow keys will instantly show the corresponding tab panel. When set to manual, the tab will receive focus but will not show until the user presses spacebar or enter. |
noScrollControls |
Disables the scroll arrows that appear when tabs overflow. |
placement |
The placement of the tabs. |
Et2InputWidget
| Name | Description |
|---|---|
autofocus |
Have browser focus this input on load. Overrides etemplate2.focusOnFirstInput(), use only once per page https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#attributes |
hasFeedbackFor |
Get a list of feedback types |
translate |
List of properties that get translated Done separately to not interfere with properties - if we re-define label property, labels go missing. |
needed |
Compatibility for deprecated name “needed” |
LitElement
| Name | Description |
|---|---|
updateComplete |
A read-only promise that resolves when the component has finished updating. |
Events
| Name | Description | Event Detail |
|---|---|---|
change |
Missing description |
Event
|
Learn more about events.
Methods
| Name | Description | Arguments |
|---|---|---|
activateTab()
|
Activate the tab containing the given widget |
widget: et2_widget
|
afterPrint()
|
Reset after printing | - |
beforePrint()
|
Set up for printing | - |
getAllPanels()
|
Overridden to allow et2-tab-panel as well as sl-tab-panel | - |
getTabPanel()
|
get tab panel-name or label the given widget is in |
widget: , label: boolean
|
handleKeyDown()
|
Keyboard navigation Copy + Paste from parent due to tagname differences (sl-tab vs et2-tab) |
event:
|
setActiveTab()
|
Reimplement to allow our existing function signatures too
use this.show(name : string) |
tab: SlTab|String|Number, options: { emitEvents?: boolean; scrollBehavior?: 'auto' | 'smooth'; }
|
Learn more about methods.
Inherited methods (30)
Et2Widget
| Name | Description |
|---|---|
checkCreateNamespace() |
Checks whether a namespace exists for this element in the content array. If yes, an own perspective of the content array is created. If not, the parent content manager is used. Constructor attributes are passed in case a child needs to make decisions |
clone() |
Creates a copy of this widget. |
createElementFromNode() |
Create a et2_widget from an XML node. First the type and attributes are read from the node. Then the readonly & modifications arrays are checked for changes specific to the loaded data. Then the appropriate constructor is called. After the constructor returns, the widget has a chance to further initialize itself from the XML node when the widget’s loadFromXML() method is called with the node. |
getArrayMgr() |
Returns the array manager object for the given part |
getArrayMgrs() |
Returns an associative array containing the top-most array managers. |
getChildren() |
Get child widgets Use |
getInstanceManager() |
Returns the instance manager |
getPath() |
Returns the path into the data array. By default, array manager takes care of this, but some extensions need to override this |
getRoot() |
Returns the base widget Usually this is the same as getInstanceManager().widgetContainer |
loadFromXML() |
Loads the widget tree from an XML node |
loadingFinished() |
Needed for legacy compatability. |
parseXMLAttrs() |
The parseXMLAttrs function takes an XML DOM attributes object and adds the given attributes to the _target associative array. This function also parses the legacyOptions. N.B. This is only used for legacy widgets. WebComponents use transformAttributes() and do their own handling of attributes. |
set_label() |
NOT the setter, since we cannot add to the DOM before connectedCallback() TODO: This is not best practice. Should just set property, DOM modification should be done in render https://lit-element.polymer-project.org/guide/templates#design-a-performant-template |
setArrayMgr() |
Sets the array manager for the given part |
setArrayMgrs() |
Sets all array manager objects - this function can be used to set the root array managers of the container object. |
setInstanceManager() |
Set the instance manager Normally this is not needed as it’s set on the top-level container, and we just return that reference |
_handleClick() |
Click handler calling custom handler set via onclick attribute to this.onclick |
destroy() |
et2_widget compatability |
set_class() |
Set the widget class |
set_disabled() |
Wrapper on this.disabled because legacy had it. |
set_statustext() |
supports legacy set_statustext |
Et2InputWidget
| Name | Description |
|---|---|
et2HandleBlur() |
If the value is unchanged, put any held validation messages back Named et2HandleBlur to avoid overwriting handleBlur() in Shoelace components |
et2HandleFocus() |
When input receives focus, clear any validation errors. If the value is the same on blur, we’ll put them back The ones from the server (ManualMessage) can interfere with submitting. Named et2HandleFocus to avoid overwriting handleFocus() in Shoelace components |
getInputNode() |
Get input to e.g. set aria-attributes |
handleSlChange() |
Handle sl-change event from Shoelace components and dispatch a change event so anything listening for change events can react to it instead of having to listen for both sl-change and change. |
isValid() |
Used by etemplate2 to determine if we can submit or not |
submit() |
Called whenever the template gets submitted. We return false if the widget is not valid, which cancels the submission. |
validate() |
Massively simplified validate, as compared to what ValidatorMixin gives us, since ValidatorMixin extends FormControlMixin which breaks SlSelect’s render() We take all validators for the widget, and if there’s a value (or field is required) we check the value with each validator. For array values we check each element with each validator. If the value does not pass the validator, we collect the message and display feedback to the user. We handle validation errors from the server with ManualMessages, which always “fail”. If the value is empty, we only validate if the field is required. |
_oldChange() |
Change handler calling custom handler set via onchange attribute |
SlTabGroup (Shoelace)
| Name | Description |
|---|---|
show() |
Shows the specified tab panel. |