Skip to content

Commit f95ba42

Browse files
authored
Fix radio field crash with nested option strings (#12345)
1 parent 17cf6ff commit f95ba42

2 files changed

Lines changed: 35 additions & 1 deletion

File tree

modules/ui/fields/radio.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,12 @@ export function uiFieldRadio(field, context) {
6666

6767
enter
6868
.append('span')
69-
.each(function(d) { strings.t.append('options.' + d, { 'default': d })(d3_select(this)); });
69+
.each(function(d) {
70+
const labelId = strings.hasTextForStringId('options.' + d + '.title')
71+
? 'options.' + d + '.title'
72+
: 'options.' + d;
73+
strings.t.append(labelId, { 'default': d })(d3_select(this));
74+
});
7075

7176
labels = labels
7277
.merge(enter);

test/spec/ui/fields/radio.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,33 @@ describe('iD.uiFieldRadio', () => {
6868
expect(onChange).toHaveBeenNthCalledWith(1, toTags);
6969
});
7070
});
71+
72+
describe('radio with stringsCrossReference', () => {
73+
let context: iD.Context;
74+
let selection: d3.Selection;
75+
76+
beforeEach(() => {
77+
context = iD.coreContext().assetPath('../dist/').init();
78+
selection = d3.select(document.createElement('div'));
79+
});
80+
81+
it('renders option labels from .title when the referenced field has nested title/description option strings', () => {
82+
const accessField = iD.presetField('access', { type: 'combo', key: 'access' });
83+
const field = iD.presetField('access_boolean', {
84+
type: 'radio',
85+
key: 'access',
86+
stringsCrossReference: '{access}',
87+
options: ['yes', 'no'],
88+
}, { access: accessField });
89+
90+
const instance = iD.uiFieldRadio(field, context);
91+
92+
expect(() => selection.call(instance)).not.toThrow();
93+
94+
const labels = selection.selectAll<HTMLLabelElement, unknown>('label').nodes();
95+
expect(labels).toHaveLength(2);
96+
expect(labels[0].querySelector('.localized-text')?.innerHTML).toBe('Allowed');
97+
expect(labels[1].querySelector('.localized-text')?.innerHTML).toBe('Prohibited');
98+
});
99+
});
71100
});

0 commit comments

Comments
 (0)