From b43c28567ae276c2ebd1831a5160c8993fef4f5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mikel=20Mart=C3=ADn?= Date: Mon, 17 Jan 2022 17:26:14 +0100 Subject: [PATCH] MDL-73470 tool_componentlibrary: Documentation for toggle input --- .../content/moodle/components/toggle.md | 81 +++++++++++++++++++ .../examples/toggle/example.mustache | 41 ++++++++++ 2 files changed, 122 insertions(+) create mode 100644 admin/tool/componentlibrary/content/moodle/components/toggle.md create mode 100644 admin/tool/componentlibrary/templates/examples/toggle/example.mustache diff --git a/admin/tool/componentlibrary/content/moodle/components/toggle.md b/admin/tool/componentlibrary/content/moodle/components/toggle.md new file mode 100644 index 00000000000..9e0c9c7bf29 --- /dev/null +++ b/admin/tool/componentlibrary/content/moodle/components/toggle.md @@ -0,0 +1,81 @@ +--- +layout: docs +title: "Toggle input" +date: 2022-01-17T00:00:00+01:00 +draft: false +weight: 60 +tags: +- MDL-73470 +- 4.0 +--- + +## How to use + +Toggle input is rendered using a template found in lib/templates/toggle.mustache. + +The parameters for the template context are: +* id: Unique id for the toggle input. +* extraclasses: Any extra classes added to the toggle input outer container. +* checked: If the initial status is checked. +* disabled: If toggle input is disabled. +* dataattributes: Array of name/value elements added as data-attributes. +* label: Label text. +* labelclasses: Any extra classes added to the label container. + +## Examples + +
+Checked toggle and with "sr-only" label. +
+{{< mustache template="core/toggle" >}} + { + "id": "example-toggle-1", + "checked": true, + "dataattributes": [{ + "name": "action", + "value": "toggle-status" + }], + "label": "Enable/disable status", + "labelclasses": "sr-only" + } +{{< /mustache >}} + +
+Disabled toggle with extra classes. +
+{{< mustache template="core/toggle" >}} + { + "id": "example-toggle-2", + "disabled": true, + "extraclasses": "mt-2 ml-2", + "dataattributes": [{ + "name": "action", + "value": "toggle-status" + }], + "label": "Enable/disable status" + } +{{< /mustache >}} + +## Use toggle as a template block + +It is also possible to include *core/toggle* in any other template using [blocks](https://docs.moodle.org/dev/Templates#Blocks), instead of rendering it with a context. +The parameters that you can define are: +* id: Unique id for the toggle input. +* extraclasses: Any extra classes added to the toggle input outer container. +* attributes: Any attributes added to the toggle input. + * data-attributes + * checked + * disabled +* labelmarkup: Label element code block. + * Should include *class="custom-control-label"*. +* label: Label text. +* labelclasses: Any extra classes added to the label container. + +
+Example of template using toggle as a block. +
+{{< mustache template="tool_componentlibrary/examples/toggle/example" >}} + { + } +{{< /mustache >}} + diff --git a/admin/tool/componentlibrary/templates/examples/toggle/example.mustache b/admin/tool/componentlibrary/templates/examples/toggle/example.mustache new file mode 100644 index 00000000000..4b0b91fa309 --- /dev/null +++ b/admin/tool/componentlibrary/templates/examples/toggle/example.mustache @@ -0,0 +1,41 @@ +{{! + This file is part of Moodle - http://moodle.org/ + + Moodle is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + Moodle is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with Moodle. If not, see . +}} +{{! + @template tool_componentlibrary/examples/toggle/example + + Template for toggle element used as a block example + + Example context (json): + { + } +}} +
+

Toggle block

+ This is a simple example using toggle template as a block. + {{< core/toggle }} + {{$id}}example-toggle-3{{/id}} + {{$extraclasses}}mt-3{{/extraclasses}} + {{$attributes}} + data-action="toggle-status" + data-action-id="3" + checked + disabled + {{/attributes}} + {{$label}}Enable/disable status{{/label}} + {{$labelclasses}}sr-only{{/labelclasses}} + {{/ core/toggle }} +