Skip to content
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

Controls: Fix select / multiselect when value contains multiple spaces #22334

Merged
merged 7 commits into from
Oct 9, 2023
4 changes: 2 additions & 2 deletions code/addons/controls/template/stories/basics.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ export default {
control: { type: 'radio', options: ['a', 'b', 'c'], labels: ['alpha', 'beta', 'gamma'] },
},
inlineRadio: { control: { type: 'inline-radio', options: ['a', 'b', 'c'] } },
select: { control: { type: 'select', options: ['a', 'b', 'c'] } },
multiSelect: { control: { type: 'multi-select', options: ['a', 'b', 'c'] } },
select: { control: 'select', options: ['a', 'b', 'c', 'double space'] },
multiSelect: { control: { type: 'multi-select' }, options: ['a', 'b', 'c', 'double space'] },
range: { control: 'range' },
rangeCustom: { control: { type: 'range', min: 0, max: 1000, step: 100 } },
text: { control: 'text' },
Expand Down
27 changes: 27 additions & 0 deletions code/e2e-tests/addon-controls.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,31 @@ test.describe('addon-controls', () => {
const label = await sbPage.panelContent().locator('textarea[name=label]').inputValue();
await expect(label).toEqual('Hello world');
});

test('should set select option when value contains double spaces', async ({ page }) => {
await page.goto(`${storybookUrl}?path=/story/addons-controls-basics--undefined`);

const sbPage = new SbPage(page);
await sbPage.waitUntilLoaded();
await sbPage.viewAddonPanel('Controls');
await sbPage.panelContent().locator('#control-select').selectOption('double space');

await expect(sbPage.panelContent().locator('#control-select')).toHaveValue('double space');
await expect(page).toHaveURL(/.*select:double\+\+space.*/);
});

test('should set multiselect option when value contains double spaces', async ({ page }) => {
await page.goto(`${storybookUrl}?path=/story/addons-controls-basics--undefined`);

const sbPage = new SbPage(page);
await sbPage.waitUntilLoaded();
await sbPage.viewAddonPanel('Controls');
await sbPage.panelContent().locator('#control-multiSelect').selectOption('double space');

await expect(sbPage.panelContent().locator('#control-multiSelect')).toHaveValue(
'double space'
);

await expect(page).toHaveURL(/.*multiSelect\[0]:double\+\+space.*/);
});
});
8 changes: 6 additions & 2 deletions code/ui/blocks/src/controls/options/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,9 @@ const SingleSelect: FC<SelectProps> = ({ name, value, options, onChange }) => {
{NO_SELECTION}
</option>
{Object.keys(options).map((key) => (
<option key={key}>{key}</option>
<option key={key} value={key}>
{key}
</option>
))}
</OptionsSelect>
</SelectWrapper>
Expand All @@ -131,7 +133,9 @@ const MultiSelect: FC<SelectProps> = ({ name, value, options, onChange }) => {
<SelectWrapper>
<OptionsSelect id={controlId} multiple value={selection} onChange={handleChange}>
{Object.keys(options).map((key) => (
<option key={key}>{key}</option>
<option key={key} value={key}>
{key}
</option>
))}
</OptionsSelect>
</SelectWrapper>
Expand Down