Skip to content

Commit 7c1d9db

Browse files
author
marker dao ®
committed
refactor && fix(radio && tests)
1 parent ade8d40 commit 7c1d9db

4 files changed

Lines changed: 39 additions & 41 deletions

File tree

packages/devextreme/js/__internal/ui/collection/collection_widget.base.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,7 @@ class CollectionWidget<
604604
this.setAria('activedescendant', null, $target);
605605
}
606606

607-
_getIdTarget($target: dxElementWrapper): dxElementWrapper {
607+
_getItemIdTarget($target: dxElementWrapper): dxElementWrapper {
608608
return $target;
609609
}
610610

@@ -619,7 +619,7 @@ class CollectionWidget<
619619
return;
620620
}
621621

622-
const $idTarget = this._getIdTarget($target);
622+
const $idTarget = this._getItemIdTarget($target);
623623

624624
if (!needCleanItemId && focusedElement) {
625625
this.setAria('id', this.getFocusedItemId(), $idTarget);

packages/devextreme/js/__internal/ui/radio_group/m_radio_collection.ts

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class RadioCollection extends CollectionWidget<Properties> {
4949
}
5050

5151
// eslint-disable-next-line class-methods-use-this
52-
_getIdTarget($target: dxElementWrapper): dxElementWrapper {
52+
_getItemIdTarget($target: dxElementWrapper): dxElementWrapper {
5353
const $radioContainer = $target.find(`.${RADIO_VALUE_CONTAINER_CLASS}`);
5454

5555
if ($radioContainer.length) {
@@ -64,41 +64,39 @@ class RadioCollection extends CollectionWidget<Properties> {
6464
const { html } = itemData;
6565

6666
const $itemElement = $(itemElement);
67-
const contentId = `dx-${new Guid()}`;
68-
69-
let $radioContainer = $itemElement.find(`.${RADIO_VALUE_CONTAINER_CLASS}`);
7067

7168
if (!html) {
7269
const $radio = $('<div>').addClass(RADIO_BUTTON_ICON_CLASS);
7370

7471
$('<div>').addClass(RADIO_BUTTON_ICON_DOT_CLASS).appendTo($radio);
7572

76-
$radioContainer = $('<div>').append($radio).addClass(RADIO_VALUE_CONTAINER_CLASS);
73+
const $radioContainer = $('<div>').append($radio).addClass(RADIO_VALUE_CONTAINER_CLASS);
7774
$itemElement.prepend($radioContainer);
7875
}
7976

77+
super._postprocessRenderItem(args);
78+
8079
// eslint-disable-next-line spellcheck/spell-checker
8180
const aria: { role: string; labelledby?: string } = {
8281
role: 'radio',
8382
};
8483

8584
if (!html) {
85+
const $itemContent = $itemElement.find(`.${ITEM_CONTENT_CLASS}`);
86+
const $contentIdTarget = $itemContent.length ? $itemContent : $itemElement;
87+
88+
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
89+
const contentId = $contentIdTarget.attr('id') || `dx-${new Guid()}`;
90+
91+
$contentIdTarget.attr('id', contentId);
92+
8693
// eslint-disable-next-line spellcheck/spell-checker
8794
aria.labelledby = contentId;
8895
}
8996

90-
const $ariaTarget = $radioContainer.length ? $radioContainer : $itemElement;
97+
const $ariaTarget = this._getItemIdTarget($itemElement);
9198

9299
this.setAria(aria, $ariaTarget);
93-
94-
super._postprocessRenderItem(args);
95-
96-
if (!html) {
97-
const $itemContent = $itemElement.find(`.${ITEM_CONTENT_CLASS}`);
98-
const $idTarget = $itemContent.length ? $itemContent : $itemElement;
99-
100-
$idTarget.attr('id', contentId);
101-
}
102100
}
103101

104102
_processSelectableItem(

packages/devextreme/testing/tests/DevExpress.ui.widgets.editors/radioGroup.markup.tests.js

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -437,30 +437,6 @@ QUnit.module('Aria accessibility', {
437437
assert.strictEqual($item.attr('aria-labelledby'), undefined, `item[${index}] element has no aria-labelledby when html is provided`);
438438
});
439439
});
440-
441-
QUnit.test('Item with html: aria-checked on item element is updated on value change', function(assert) {
442-
const items = [
443-
{ html: '<span>Option A</span>', value: 'a' },
444-
{ html: '<span>Option B</span>', value: 'b' },
445-
];
446-
helper.createWidget({ items, valueExpr: 'value', value: 'a' });
447-
448-
helper.getItems().each((index, item) => {
449-
const $item = $(item);
450-
const expected = index === 0 ? 'true' : 'false';
451-
452-
assert.strictEqual($item.attr('aria-checked'), expected, `item[${index}] has correct aria-checked after initial render`);
453-
});
454-
455-
helper.widget.option('value', 'b');
456-
457-
helper.getItems().each((index, item) => {
458-
const $item = $(item);
459-
const expected = index === 1 ? 'true' : 'false';
460-
461-
assert.strictEqual($item.attr('aria-checked'), expected, `item[${index}] has correct aria-checked after value change`);
462-
});
463-
});
464440
});
465441

466442
module('layout', moduleConfig, () => {

packages/devextreme/testing/tests/DevExpress.ui.widgets.editors/radioGroup.tests.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,30 @@ module('accessibility', moduleConfig, () => {
370370
assert.strictEqual($(this).attr('aria-checked'), undefined, `item[${index}] element does not have aria-checked`);
371371
});
372372
});
373+
374+
test('item with html: aria-checked on item element is updated on value change', function(assert) {
375+
const items = [
376+
{ html: '<span>Option A</span>', value: 'a' },
377+
{ html: '<span>Option B</span>', value: 'b' },
378+
];
379+
const $radioGroup = createRadioGroup({ items, valueExpr: 'value', value: 'a' });
380+
const instance = getInstance($radioGroup);
381+
const $itemElements = $(instance.itemElements());
382+
383+
$itemElements.each(function(index) {
384+
const expected = index === 0 ? 'true' : 'false';
385+
386+
assert.strictEqual($(this).attr('aria-checked'), expected, `item[${index}] has correct aria-checked after initial render`);
387+
});
388+
389+
instance.option('value', 'b');
390+
391+
$itemElements.each(function(index) {
392+
const expected = index === 1 ? 'true' : 'false';
393+
394+
assert.strictEqual($(this).attr('aria-checked'), expected, `item[${index}] has correct aria-checked after value change`);
395+
});
396+
});
373397
});
374398

375399
module('hidden input', () => {

0 commit comments

Comments
 (0)