Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import { Base } from '../../partials';

export interface AltRadioInputOptions {
onTileClick?: (event: MouseEvent, inputId: string) => void;
}

export class AltRadioInput extends Base {
private _inputElement: HTMLInputElement;
private _tileElement: HTMLDivElement;
private inputElement: HTMLInputElement;
private tileElement: HTMLDivElement;
private onTileClick?: (event: MouseEvent, inputId: string) => void;

private _isFocused = false;
private isChecked = false;
private isFocused = false;

constructor(container: HTMLDivElement) {
constructor(container: HTMLDivElement, { onTileClick }: AltRadioInputOptions = {}) {
super(container);

const inputElement = this._container.querySelector<HTMLInputElement>('.ids-alt-radio__source .ids-input');
Expand All @@ -16,55 +22,67 @@ export class AltRadioInput extends Base {
throw new Error('AltRadio: Required elements are missing in the container.');
}

this._inputElement = inputElement;
this._tileElement = tileElement;
this.inputElement = inputElement;
this.tileElement = tileElement;

this.onTileClick = onTileClick;
}

setFocus(nextIsFocused: boolean) {
if (this._isFocused === nextIsFocused) {
if (this.isFocused === nextIsFocused) {
return;
}

this._isFocused = nextIsFocused;
this.isFocused = nextIsFocused;

this._tileElement.classList.toggle('ids-alt-radio__tile--focused', nextIsFocused);
this.tileElement.classList.toggle('ids-alt-radio__tile--focused', nextIsFocused);

if (nextIsFocused) {
this._inputElement.focus();
this.inputElement.focus();
} else {
this._inputElement.blur();
this.inputElement.blur();
}
}

setError(value: boolean) {
this._tileElement.classList.toggle('ids-alt-radio__tile--error', value);
this.tileElement.classList.toggle('ids-alt-radio__tile--error', value);
}

getInputElement(): HTMLInputElement {
return this._inputElement;
return this.inputElement;
}

toggleChecked(value?: boolean) {
const isChecked = value ?? !this.isChecked;

this.isChecked = isChecked;
this.inputElement.checked = isChecked;
this.tileElement.classList.toggle('ids-alt-radio__tile--checked', isChecked);
}

initInputListeners() {
this._inputElement.addEventListener('focus', () => {
this.inputElement.addEventListener('focus', () => {
this.setFocus(true);
});

this._inputElement.addEventListener('blur', () => {
this.inputElement.addEventListener('blur', () => {
this.setFocus(false);
});

this._inputElement.addEventListener('input', () => {
this._tileElement.classList.toggle('ids-alt-radio__tile--checked', this._inputElement.checked);
this.inputElement.addEventListener('input', () => {
this.toggleChecked();
});
}

initTileBtn() {
this._tileElement.addEventListener('click', (event) => {
this.tileElement.addEventListener('click', (event) => {
event.preventDefault();
event.stopPropagation();

this._inputElement.focus();
this._inputElement.click();
this.inputElement.focus();
this.inputElement.click();

this.onTileClick?.(event, this.inputElement.id);
});
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import { AltRadioInput } from './alt_radio_input';
import { BaseInputsList } from '../../partials';

export enum AltRadiosListFieldAction {
Check = 'check',
Uncheck = 'uncheck',
}

export class AltRadiosListField extends BaseInputsList<string> {
private itemsContainer: HTMLDivElement;
private itemsMap = new Map<string, AltRadioInput>();
protected value?: string;

static EVENTS = {
...BaseInputsList.EVENTS,
CHANGE: 'ids:alt-radio-list-field:change',
};

constructor(container: HTMLDivElement) {
super(container);

const itemsContainer = container.querySelector<HTMLDivElement>('.ids-choice-inputs-list__items');

if (!itemsContainer) {
throw new Error('AltRadiosListField: Required elements are missing in the container.');
}

this.itemsContainer = itemsContainer;

this.onItemClick = this.onItemClick.bind(this);

this.saveItemsInstancesToMap();
}

protected saveItemsInstancesToMap() {
const itemsButtons = this.getItemsButtons();

this.itemsMap.clear();

itemsButtons.forEach((button) => {
const buttonInstance = new AltRadioInput(button, { onTileClick: this.onItemClick });
const buttonId = buttonInstance.getInputElement().id;

this.itemsMap.set(buttonId, buttonInstance);
});
}

getItemsButtons() {
const itemsButtons = [...this.itemsContainer.querySelectorAll<HTMLDivElement>('.ids-alt-radio')];

return itemsButtons;
}

protected onItemClick(_event: MouseEvent, itemValue: string) {
if (this.value === itemValue) {
return;
}

const changeEvent = new CustomEvent(AltRadiosListField.EVENTS.CHANGE, {
bubbles: true,
detail: itemValue,
});

if (this.value) {
const currentValueInstance = this.itemsMap.get(this.value);

if (currentValueInstance) {
currentValueInstance.toggleChecked(false);
}
}

this.value = itemValue;
this._container.dispatchEvent(changeEvent);
}

protected initButtons() {
this.itemsMap.forEach((itemInstance) => {
itemInstance.init();
});
}

public init() {
super.init();

this.initButtons();
}
}
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './alt_radio_input';
export * from './alt_radios_list_field';
12 changes: 11 additions & 1 deletion src/bundle/Resources/public/ts/init_components.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { AltRadioInput, AltRadiosListField } from './components/alt_radio';
import { CheckboxInput, CheckboxesListField } from './components/checkbox';
import { DropdownMultiInput, DropdownSingleInput } from './components/dropdown';
import { InputTextField, InputTextInput } from './components/input_text';
import { ToggleButtonField, ToggleButtonInput } from './components/toggle_button';
import { Accordion } from './components/accordion';
import { AltRadioInput } from './components/alt_radio/alt_radio_input';
import { DropdownSingleInput } from './components/dropdown/dropdown_single_input';
import { OverflowList } from './components/overflow_list';

const accordionContainers = document.querySelectorAll<HTMLDivElement>('.ids-accordion:not([data-ids-custom-init])');
Expand All @@ -22,6 +24,14 @@ altRadioContainers.forEach((altRadioContainer: HTMLDivElement) => {
altRadioInstance.init();
});

const altRadiosListContainers = document.querySelectorAll<HTMLDivElement>('.ids-alt-radio-list-field:not([data-ids-custom-init])');

altRadiosListContainers.forEach((altRadiosListContainer: HTMLDivElement) => {
const altRadiosListInstance = new AltRadiosListField(altRadiosListContainer);

altRadiosListInstance.init();
});

const checkboxContainers = document.querySelectorAll<HTMLDivElement>('.ids-checkbox:not([data-ids-custom-init])');

checkboxContainers.forEach((checkboxContainer: HTMLDivElement) => {
Expand All @@ -30,7 +40,7 @@ checkboxContainers.forEach((checkboxContainer: HTMLDivElement) => {
checkboxInstance.init();
});

const checkboxesFieldContainers = document.querySelectorAll<HTMLDivElement>('.ids-field.ids-field--list:not([data-ids-custom-init])');
const checkboxesFieldContainers = document.querySelectorAll<HTMLDivElement>('.ids-checkboxes-list-field:not([data-ids-custom-init])');

checkboxesFieldContainers.forEach((checkboxesFieldContainer: HTMLDivElement) => {
const checkboxesFieldInstance = new CheckboxesListField(checkboxesFieldContainer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@
tile_class,
)
%}
{% set custom_init = attributes.render('data-ids-custom-init') is not null %}

<div class="{{ component_classes }}">
<div class="{{ component_classes }}"{{ custom_init ? ' data-ids-custom-init' : '' }}>
<div class="ids-alt-radio__source">
<input
class="ids-input ids-input--radio"
type="radio"
name="{{ name }}"
{{ attributes.defaults({ checked, disabled, required }) }}
/>
</div>
Expand Down
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-alt-radio-list-field', attributes.render('class') ?? '') %}

{% block item %}
<twig:ibexa:alt_radio:input {{ ...item }} data-ids-custom-init="true">
{{ item.label }}
</twig:ibexa:alt_radio:input>
{% endblock item %}
58 changes: 58 additions & 0 deletions src/lib/Twig/Components/AltRadio/ListField.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?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\AltRadio;

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 AltRadioItem array{
* id: non-empty-string,
* value: string|int,
* label: string,
* disabled?: bool,
* tileClass?: string,
* attributes?: array<string, mixed>,
* label_attributes?: array<string, mixed>,
* inputWrapperClassName?: string,
* labelClassName?: string,
* name?: string,
* required?: bool
* }
* @phpstan-type AltRadioItems list<AltRadioItem>
*/
#[AsTwigComponent('ibexa:alt_radio:list_field')]
final class ListField extends AbstractField
{
use ListFieldTrait;

public string $value = '';

protected function configurePropsResolver(OptionsResolver $resolver): void
{
$this->validateListFieldProps($resolver);

$resolver->setDefault('direction', self::HORIZONTAL);
$resolver->setDefault('value', '');
$resolver->setAllowedTypes('value', 'string');
}

protected function configureListFieldItemOptions(OptionsResolver $itemsResolver): void
{
$itemsResolver
->define('tileClass')
->allowedTypes('string')
->default('');

$itemsResolver->setDefault('disabled', false);
$itemsResolver->setDefault('attributes', []);
}
}
9 changes: 8 additions & 1 deletion src/lib/Twig/Components/ListFieldTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ protected function validateListFieldProps(OptionsResolver $resolver): void
->default([])
->allowedTypes('array');

$resolver->setOptions('items', static function (OptionsResolver $itemsResolver): void {
$resolver->setOptions('items', function (OptionsResolver $itemsResolver): void {
$itemsResolver->setPrototype(true);
$itemsResolver
->define('id')
Expand Down Expand Up @@ -111,11 +111,18 @@ protected function validateListFieldProps(OptionsResolver $resolver): void
$itemsResolver
->define('required')
->allowedTypes('bool');

$this->configureListFieldItemOptions($itemsResolver);
});

$resolver
->define('direction')
->allowedValues(self::VERTICAL, self::HORIZONTAL)
->default(self::VERTICAL);
}

protected function configureListFieldItemOptions(OptionsResolver $itemsResolver): void
{
// Intentionally left blank; consuming components override to extend item option definitions.
}
}
Loading