generated from ibexa/bundle-template
-
Notifications
You must be signed in to change notification settings - Fork 1
IBX-10853: Toggle #65
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+392
−53
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
1625aba
IBX-10853: Toggle
GrabowskiM e8cc5ef
Refactored input field components to use SingleInputFieldTrait for be…
mikadamczyk e68d1c3
use ternary
GrabowskiM 31060e8
Refactored input field components to extend AbstractSingleInputField …
mikadamczyk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
src/bundle/Resources/public/ts/components/toggle_button/index.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| export * from './toggle_button_field'; | ||
| export * from './toggle_button_input'; |
46 changes: 46 additions & 0 deletions
46
src/bundle/Resources/public/ts/components/toggle_button/toggle_button_field.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| import { Base } from '../../partials'; | ||
| import { HelperText } from '../helper_text'; | ||
| import { Label } from '../label'; | ||
| import { ToggleButtonInput } from './toggle_button_input'; | ||
|
|
||
| export class ToggleButtonField extends Base { | ||
| private helperTextInstance: HelperText | null = null; | ||
| private inputInstance: ToggleButtonInput; | ||
| private labelInstance: Label | null = null; | ||
|
|
||
| constructor(container: HTMLDivElement) { | ||
| super(container); | ||
|
|
||
| const inputContainer = container.querySelector<HTMLDivElement>('.ids-toggle'); | ||
|
|
||
| if (!inputContainer) { | ||
| throw new Error('ToggleButtonField: Input container is missing in the container.'); | ||
| } | ||
|
|
||
| const labelContainer = container.querySelector<HTMLDivElement>('.ids-label'); | ||
|
|
||
| if (labelContainer) { | ||
| this.labelInstance = new Label(labelContainer); | ||
| } | ||
|
|
||
| const helperTextContainer = container.querySelector<HTMLDivElement>('.ids-helper-text'); | ||
|
|
||
| if (helperTextContainer) { | ||
| this.helperTextInstance = new HelperText(helperTextContainer); | ||
| } | ||
|
|
||
| this.inputInstance = new ToggleButtonInput(inputContainer); | ||
| } | ||
|
|
||
| initChildren(): void { | ||
| this.labelInstance?.init(); | ||
| this.inputInstance.init(); | ||
| this.helperTextInstance?.init(); | ||
| } | ||
|
|
||
| init(): void { | ||
| super.init(); | ||
|
|
||
| this.initChildren(); | ||
| } | ||
| } |
76 changes: 76 additions & 0 deletions
76
src/bundle/Resources/public/ts/components/toggle_button/toggle_button_input.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| import { BaseChoiceInput } from '../../partials'; | ||
|
|
||
| export class ToggleButtonInput extends BaseChoiceInput { | ||
| private labels: { on: string; off: string }; | ||
| private widgetNode: HTMLDivElement; | ||
| private toggleLabelNode: HTMLLabelElement; | ||
|
|
||
| static EVENTS = { | ||
| ...BaseChoiceInput.EVENTS, | ||
| CHANGE: 'ids:toggle-button-input:change', | ||
| }; | ||
|
|
||
| constructor(container: HTMLDivElement) { | ||
| super(container); | ||
|
|
||
| const widgetNode = this._container.querySelector<HTMLDivElement>('.ids-toggle__widget'); | ||
| const toggleLabelNode = this._container.querySelector<HTMLLabelElement>('.ids-toggle__label'); | ||
|
|
||
| if (!widgetNode || !toggleLabelNode) { | ||
| throw new Error('ToggleButtonInput: Required elements are missing in the container.'); | ||
| } | ||
|
|
||
| const labelOn = toggleLabelNode.getAttribute('data-ids-label-on'); | ||
| const labelOff = toggleLabelNode.getAttribute('data-ids-label-off'); | ||
|
|
||
| if (!labelOn || !labelOff) { | ||
| throw new Error('ToggleButtonInput: Toggle labels are missing in label attributes.'); | ||
| } | ||
|
|
||
| this.labels = { off: labelOff, on: labelOn }; | ||
| this.widgetNode = widgetNode; | ||
| this.toggleLabelNode = toggleLabelNode; | ||
| } | ||
|
|
||
| protected updateLabel(): void { | ||
| const isChecked = this._inputElement.checked; | ||
|
|
||
| this.toggleLabelNode.textContent = isChecked ? this.labels.on : this.labels.off; | ||
| } | ||
|
|
||
| protected initWidgets(): void { | ||
| this.widgetNode.addEventListener('click', () => { | ||
| this._inputElement.focus(); | ||
| this._inputElement.checked = !this._inputElement.checked; | ||
| this._inputElement.dispatchEvent(new Event('change', { bubbles: true })); | ||
| }); | ||
| } | ||
|
|
||
| protected initInputEvents(): void { | ||
| this._inputElement.addEventListener('focus', () => { | ||
| this._container.classList.add('ids-toggle--focused'); | ||
| }); | ||
|
|
||
| this._inputElement.addEventListener('blur', () => { | ||
| this._container.classList.remove('ids-toggle--focused'); | ||
| }); | ||
|
|
||
| this._inputElement.addEventListener('change', () => { | ||
| const changeEvent = new CustomEvent(ToggleButtonInput.EVENTS.CHANGE, { | ||
| bubbles: true, | ||
| detail: this._inputElement.checked, | ||
| }); | ||
|
|
||
| this.updateLabel(); | ||
| this._container.classList.toggle('ids-toggle--checked', this._inputElement.checked); | ||
| this._container.dispatchEvent(changeEvent); | ||
| }); | ||
| } | ||
|
|
||
| public init() { | ||
| super.init(); | ||
|
|
||
| this.initInputEvents(); | ||
| this.initWidgets(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
...le/Resources/views/themes/standard/design_system/components/toggle_button/field.html.twig
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| {% extends '@IbexaDesignSystemTwig/themes/standard/design_system/partials/base_field.html.twig' %} | ||
|
|
||
| {% set class = html_classes('ids-toggle-field', attributes.render('class') ?? '') %} | ||
|
|
||
| {% block content %} | ||
| <twig:ibexa:toggle_button:input {{ ...input }} /> | ||
| {% endblock content %} |
42 changes: 42 additions & 0 deletions
42
...le/Resources/views/themes/standard/design_system/components/toggle_button/input.html.twig
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| {% set component_classes = | ||
| html_cva( | ||
| base: html_classes( | ||
| 'ids-toggle', | ||
| { | ||
| 'ids-toggle--checked': checked, | ||
| 'ids-toggle--disabled': disabled, | ||
| } | ||
| ), | ||
| variants: { | ||
| size: { | ||
| medium: 'ids-toggle--medium', | ||
| small: 'ids-toggle--small' | ||
| } | ||
| } | ||
| ) | ||
| %} | ||
|
|
||
| <div class="{{ component_classes.apply({ size }, attributes.render('class')) }}" {{ attributes }}> | ||
| <div class="ids-toggle__source"> | ||
| <twig:ibexa:checkbox:input | ||
| :id="id" | ||
| :name="name" | ||
| :value="value" | ||
| :checked="checked" | ||
| :disabled="disabled" | ||
| :required="required" | ||
| data-ids-custom-init="1" | ||
| /> | ||
| </div> | ||
| <div class="ids-toggle__widget" role="button"> | ||
| <div class="ids-toggle__indicator"></div> | ||
| </div> | ||
| <twig:ibexa:choice_input_label | ||
| class="ids-toggle__label" | ||
| :for="id" | ||
| :data-ids-label-on="onLabel" | ||
| :data-ids-label-off="offLabel" | ||
| > | ||
| {{ checked ? onLabel : offLabel }} | ||
| </twig:ibexa:choice_input_label> | ||
| </div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
| * @license For full copyright and license information view LICENSE file distributed with this source code. | ||
| */ | ||
| declare(strict_types=1); | ||
|
|
||
| namespace Ibexa\DesignSystemTwig\Twig\Components; | ||
|
|
||
| use Symfony\Component\OptionsResolver\Options; | ||
| use Symfony\Component\OptionsResolver\OptionsResolver; | ||
| use Symfony\Component\PropertyAccess\Exception\InvalidTypeException; | ||
| use Symfony\UX\TwigComponent\Attribute\ExposeInTemplate; | ||
|
|
||
| /** | ||
| * @phpstan-type AttributeMap array<string, scalar> | ||
| */ | ||
| abstract class AbstractSingleInputField extends AbstractField | ||
| { | ||
| /** @var non-empty-string */ | ||
| public string $id; | ||
|
|
||
| /** @var AttributeMap */ | ||
| #[ExposeInTemplate(name: 'input', getter: 'getInput')] | ||
| public array $input = []; | ||
|
|
||
| public string $value = ''; | ||
|
|
||
| /** | ||
| * @return AttributeMap | ||
| */ | ||
| public function getLabelExtra(): array | ||
| { | ||
| return $this->labelExtra + ['for' => $this->id, 'required' => $this->required]; | ||
| } | ||
|
|
||
| /** | ||
| * @return AttributeMap | ||
| */ | ||
| public function getInput(): array | ||
| { | ||
| return $this->input + [ | ||
| 'id' => $this->id, | ||
| 'name' => $this->name, | ||
| 'required' => $this->required, | ||
| 'value' => $this->value, | ||
| 'data-ids-custom-init' => 'true', | ||
| ]; | ||
| } | ||
|
|
||
| protected function configureSingleInputFieldOptions( | ||
| OptionsResolver $resolver, | ||
| ?callable $idFactory, | ||
| string $defaultValue = '' | ||
| ): void { | ||
| $resolver | ||
| ->define('id') | ||
| ->allowedTypes('null', 'string') | ||
| ->default(null) | ||
| ->normalize(static function (Options $options, ?string $id) use ($idFactory): string { | ||
| if (null !== $id) { | ||
| if ('' === trim($id)) { | ||
| throw new InvalidTypeException('non-empty-string', 'string', 'id'); | ||
| } | ||
|
|
||
| return $id; | ||
| } | ||
|
|
||
| if (null === $idFactory) { | ||
| throw new InvalidTypeException('string', 'NULL', 'id'); | ||
| } | ||
|
|
||
| $value = $idFactory(); | ||
|
|
||
| if ('' === trim($value)) { | ||
| throw new InvalidTypeException('non-empty-string', 'string', 'id'); | ||
| } | ||
|
|
||
| return $value; | ||
| }); | ||
|
|
||
| $resolver | ||
| ->define('input') | ||
| ->allowedTypes('array') | ||
| ->default([]) | ||
| ->normalize(static function (Options $options, array $attributes): array { | ||
| return self::assertForbidden($attributes, ['id', 'name', 'required', 'value'], 'input'); | ||
| }); | ||
|
|
||
| $resolver | ||
| ->define('value') | ||
| ->allowedTypes('string') | ||
| ->default($defaultValue); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This call shows that this trait depends on something else. It can only ever be used as part of
AbstractField.