generated from ibexa/bundle-template
-
Notifications
You must be signed in to change notification settings - Fork 1
IBX-10621: Checkboxes List #49
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
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
3f93aad
temp
GrabowskiM b17f97c
temp
GrabowskiM 3698cc0
fixed events
GrabowskiM b6c0767
cleanup
GrabowskiM 96babf1
after copilot
GrabowskiM ec813b9
fix cs
GrabowskiM a75bbb3
revert force
GrabowskiM ce1318e
Refactored ListField and ListFieldTrait for improved type hinting and…
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,11 @@ | ||
| import getIbexaConfig from '@ibexa/eslint-config/eslint'; | ||
|
|
||
| export default getIbexaConfig({ react: false }); | ||
| export default [ | ||
| ...getIbexaConfig({ react: false }), | ||
| { | ||
| files: ['**/*.ts'], | ||
| rules: { | ||
| '@typescript-eslint/unbound-method': 'off', | ||
| }, | ||
| }, | ||
| ]; | ||
100 changes: 100 additions & 0 deletions
100
src/bundle/Resources/public/ts/components/checkbox/checkboxes_list_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,100 @@ | ||
| import { BaseInputsList } from '../../partials'; | ||
|
|
||
| export enum CheckboxesListFieldAction { | ||
| Check = 'check', | ||
| Uncheck = 'uncheck', | ||
| } | ||
|
|
||
| export class CheckboxesListField extends BaseInputsList<string[]> { | ||
| private _itemsContainer: HTMLDivElement; | ||
|
|
||
| static EVENTS = { | ||
| ...BaseInputsList.EVENTS, | ||
| CHANGE: 'ids:checkboxes-list-field:change', | ||
| }; | ||
|
|
||
| constructor(container: HTMLDivElement) { | ||
| super(container); | ||
|
|
||
| const itemsContainer = container.querySelector<HTMLDivElement>('.ids-choice-inputs-list__items'); | ||
|
|
||
| if (!itemsContainer) { | ||
| throw new Error('CheckboxesListField: Required elements are missing in the container.'); | ||
| } | ||
|
|
||
| this._itemsContainer = itemsContainer; | ||
|
|
||
| this.onItemChange = this.onItemChange.bind(this); | ||
| } | ||
|
|
||
| getItemsCheckboxes() { | ||
| const itemsCheckboxes = [ | ||
| ...this._itemsContainer.querySelectorAll<HTMLInputElement>('.ids-choice-input-field .ids-input--checkbox'), | ||
| ]; | ||
|
|
||
| return itemsCheckboxes; | ||
| } | ||
|
|
||
| getValue(): string[] { | ||
| const itemsCheckboxes = this.getItemsCheckboxes(); | ||
| const checkedValues = itemsCheckboxes.reduce((acc: string[], checkbox) => { | ||
| if (checkbox.checked) { | ||
| acc.push(checkbox.value); | ||
| } | ||
|
|
||
| return acc; | ||
| }, []); | ||
|
|
||
| return checkedValues; | ||
| } | ||
|
|
||
| onItemChange(event: Event) { | ||
| if (!(event.target instanceof HTMLInputElement)) { | ||
| return; | ||
| } | ||
|
|
||
| const item = event.target; | ||
| const nextValue = this.getValue(); | ||
| const actionPerformed = item.checked ? CheckboxesListFieldAction.Check : CheckboxesListFieldAction.Uncheck; | ||
|
|
||
| this.onChange(nextValue, item.value, actionPerformed); | ||
| } | ||
|
|
||
| onChange(nextValue: string[], itemValue: string, actionPerformed: CheckboxesListFieldAction) { | ||
| const changeEvent = new CustomEvent(CheckboxesListField.EVENTS.CHANGE, { | ||
| bubbles: true, | ||
| detail: [nextValue, itemValue, actionPerformed], | ||
| }); | ||
|
|
||
| this._container.dispatchEvent(changeEvent); | ||
| } | ||
|
|
||
| initCheckboxes() { | ||
| const itemsCheckboxes = this.getItemsCheckboxes(); | ||
|
|
||
| itemsCheckboxes.forEach((checkbox) => { | ||
| checkbox.addEventListener('change', this.onItemChange, false); | ||
| }); | ||
| } | ||
|
|
||
| unbindCheckboxes() { | ||
| const itemsCheckboxes = this.getItemsCheckboxes(); | ||
|
|
||
| itemsCheckboxes.forEach((checkbox) => { | ||
| checkbox.removeEventListener('change', this.onItemChange, false); | ||
| }); | ||
| } | ||
|
|
||
| reinit() { | ||
| super.reinit(); | ||
|
|
||
| this.unbindCheckboxes(); | ||
| this.initCheckboxes(); | ||
| } | ||
|
|
||
| init() { | ||
| super.init(); | ||
|
|
||
| this.initCheckboxes(); | ||
| } | ||
| } |
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 |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| export * from './checkbox_input'; | ||
| export * from './checkboxes_list_field'; |
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
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 |
|---|---|---|
|
|
@@ -21,6 +21,7 @@ | |
| name, | ||
| required, | ||
| type, | ||
| value, | ||
| }) | ||
| }} | ||
| /> | ||
|
|
||
9 changes: 9 additions & 0 deletions
9
...le/Resources/views/themes/standard/design_system/components/checkbox/list_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,9 @@ | ||
| {% extends '@IbexaDesignSystemTwig/themes/standard/design_system/partials/base_inputs_list.html.twig' %} | ||
|
|
||
| {% set class = html_classes('ids-checkboxes-list-field', attributes.render('class') ?? '') %} | ||
|
|
||
| {% block item %} | ||
| <twig:ibexa:checkbox:field {{ ...item }}> | ||
| {{ item.label }} | ||
| </twig:ibexa:checkbox:field> | ||
| {% endblock item %} |
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
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,53 @@ | ||
| <?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\Checkbox; | ||
|
|
||
| use Ibexa\DesignSystemTwig\Twig\Components\AbstractField; | ||
| use Ibexa\DesignSystemTwig\Twig\Components\ListFieldTrait; | ||
| use Symfony\Component\OptionsResolver\OptionsResolver; | ||
| use Symfony\UX\TwigComponent\Attribute\AsTwigComponent; | ||
|
|
||
| /** | ||
| * @phpstan-type CheckboxItem array{ | ||
| * value: string|int, | ||
| * label: string, | ||
| * disabled?: bool | ||
| * } | ||
| * @phpstan-type CheckboxItems list<CheckboxItem> | ||
| */ | ||
| #[AsTwigComponent('ibexa:checkbox:list_field')] | ||
| final class ListField extends AbstractField | ||
| { | ||
| use ListFieldTrait; | ||
|
|
||
| /** @var array<string|int> */ | ||
| public array $value = []; | ||
|
|
||
| /** | ||
| * @param CheckboxItem $item | ||
| * | ||
| * @return CheckboxItem | ||
| */ | ||
| protected function modifyListItem(array $item): array | ||
| { | ||
| $item['checked'] = in_array($item['value'], $this->value, true); | ||
|
|
||
| return $item; | ||
| } | ||
|
|
||
| protected function configurePropsResolver(OptionsResolver $resolver): void | ||
| { | ||
| $this->validateListFieldProps($resolver); | ||
|
|
||
| // TODO: check if items are valid according to Checkbox/Field component | ||
|
|
||
mikadamczyk marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| $resolver->setDefaults(['value' => []]); | ||
| $resolver->setAllowedTypes('value', 'array'); | ||
| } | ||
| } | ||
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
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,66 @@ | ||
| <?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\OptionsResolver; | ||
| use Symfony\UX\TwigComponent\Attribute\ExposeInTemplate; | ||
|
|
||
| /** | ||
| * @phpstan-type ListItem array{ | ||
| * value: string|int, | ||
| * label: string, | ||
| * } | ||
| * @phpstan-type ListItems list<ListItem> | ||
| */ | ||
| trait ListFieldTrait | ||
| { | ||
| public const string VERTICAL = 'vertical'; | ||
| public const string HORIZONTAL = 'horizontal'; | ||
|
|
||
| public string $direction = 'vertical'; | ||
|
|
||
| /** @var ListItems */ | ||
| #[ExposeInTemplate(name: 'items', getter: 'getItems')] | ||
| public array $items = []; | ||
|
|
||
| /** | ||
| * @return ListItems | ||
| */ | ||
| public function getItems(): array | ||
| { | ||
| return array_map(function (array $item) { | ||
mikadamczyk marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| $listItem = $item + ['name' => $this->name, 'required' => $this->required]; | ||
|
|
||
| return $this->modifyListItem($listItem); | ||
| }, $this->items); | ||
| } | ||
|
|
||
| /** | ||
| * @param ListItem $item | ||
| * | ||
| * @return ListItem | ||
| */ | ||
| protected function modifyListItem(array $item): array | ||
| { | ||
| return $item; | ||
| } | ||
|
|
||
| protected function validateListFieldProps(OptionsResolver $resolver): void | ||
| { | ||
| $resolver->setDefaults([ | ||
| 'items' => [], | ||
| ]); | ||
| $resolver->setAllowedTypes('items', 'array'); | ||
|
|
||
| $resolver | ||
| ->define('direction') | ||
| ->allowedValues(self::VERTICAL, self::HORIZONTAL) | ||
| ->default(self::VERTICAL); | ||
| } | ||
| } | ||
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.
I've switched it off because it returns false-positive on https://github.com/ibexa/design-system-twig/pull/49/files#diff-914f9d057f37b814c2bb03196746b89ce20143f6ece03374618eb28d2472d225R76
Basically linter for TS is not able to recognize if method was bound in constructor (typescript-eslint/typescript-eslint#636), as it's our common way for binding I think switching it off globally is better than leave it globally and switching it off per usecase