Skip to content

Commit 1ab3e68

Browse files
feat(ui): show country flags in SMS phone selection
1 parent f4cc97e commit 1ab3e68

7 files changed

Lines changed: 85 additions & 10 deletions

File tree

‎packages/swingset/src/stories/select.component.mdx‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,23 @@ const roles = [
9494
storyModule={SelectStories}
9595
/>
9696

97+
### Decorative content
98+
99+
Supply `startAdornment` to place a flag or icon before the text. Adornments are hidden from assistive technology; `label` remains the text used for naming and typeahead. Pass the selected item's adornment to the trigger.
100+
101+
| Prop | Type | Default | Description |
102+
| ------------------------------------------------------------ | ----------- | ------- | --------------------------------------------- |
103+
| `SelectItem.startAdornment` / `Select.Option.startAdornment` | `ReactNode` | — | Decorative content before an option's label. |
104+
| `Select.Trigger.startAdornment` | `ReactNode` | — | Decorative content before the selected value. |
105+
97106
### Options
98107

99108
Every row reserves space for the check, so the labels stay put when the selection moves. A
100109
`disabled` item can still be reached with the keyboard but cannot be selected. The description is
101110
read to assistive technology as the option's description, not as part of its name.
102111

103112
To compose the rows yourself, give `Select.Popup` children: one `Select.Option` per item, taking the
104-
same `value`, `label`, `description`, and `disabled`.
113+
same `value`, `label`, `description`, `startAdornment`, and `disabled`.
105114

106115
```tsx
107116
<Select.Popup>
@@ -179,11 +188,13 @@ with the shared ScrollArea treatment. Nothing to opt into — `Select.Popup` com
179188
| ---------------- | ------------------------------------ | ----------------------------------------------------------------------------------- |
180189
| `Select.Root` | — | State provider; owns the value, the items, open/close, and keyboard nav. |
181190
| `Select.Trigger` | `select-trigger` | Opens the listbox. Defaults to a neutral `md` `Button`; `variant` picks the chrome. |
191+
| ↳ adornment | `select-trigger-adornment` | Decorative content before the value. |
182192
| ↳ value | `select-value` | The selected label, or the placeholder. |
183193
| ↳ icon | `select-trigger-icon` | The trailing chevron. |
184194
| `Select.Popup` | `select-positioner` / `select-popup` | Portals, positions, and renders the listbox surface. |
185195
| ↳ viewport | `select-viewport` | The scrolling box inside the popup. Carries the ScrollArea fade and scrollbar. |
186196
| `Select.Option` | `select-option` | A single choice. `data-selected`, `data-active`, `data-disabled`, `data-described`. |
197+
| ↳ adornment | `select-option-adornment` | Decorative content before the option label. |
187198
| ↳ content | `select-option-content` | Stacks the label over the description. |
188199
| ↳ label | `select-option-label` | The option's text. Truncates to one line. |
189200
| ↳ description | `select-option-description` | The optional second line. |

‎packages/swingset/src/stories/user-profile-mfa-section.mdx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ The caller owns the code, pending state, error feedback, and success closure. Cl
126126

127127
### Set up SMS verification
128128

129-
Choose an existing number with Select or add a new number. New numbers use the same phone-entry and code-verification steps as Add phone number. Back returns to the previous screen; returning from verification to phone entry keeps the entered number.
129+
Choose an existing number with Select or add a new number. Country flags appear beside the full numbers in the options and selected value. New numbers use the same phone-entry and code-verification steps as Add phone number. Back returns to the previous screen; returning from verification to phone entry keeps the entered number.
130130

131131
In this example, the first existing number is already verified and enables SMS directly. The second existing number and newly entered numbers require a code. Enter any six digits to see an initial error, then choose Verify to retry successfully. Resend shows a countdown and pending feedback. Closing and reopening resets the example. All requests are simulated; no text messages are sent or account changes made.
132132

‎packages/swingset/src/stories/user-profile-mfa-section.stories.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ export function AuthenticatorSetup() {
156156
export function SmsSetup() {
157157
const phoneNumbers = [
158158
{ id: 'personal', phoneNumber: '+18015550100', verified: true },
159-
{ id: 'work', phoneNumber: '+18015550200', verified: false },
159+
{ id: 'work', phoneNumber: '+14165550100', verified: false },
160160
];
161161
const [open, setOpen] = useState(false);
162162
const [step, setStep] = useState<UserProfileAddSmsDialogProps['step']>('select');

‎packages/ui/src/mosaic/components/select/select.styles.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@ export const value = stylex.create({
2626
},
2727
});
2828

29+
export const adornment = stylex.create({
30+
base: {
31+
alignItems: 'center',
32+
display: 'flex',
33+
flexShrink: 0,
34+
lineHeight: 1,
35+
},
36+
});
37+
2938
export const positioner = stylex.create({
3039
base: {
3140
outline: 'none',

‎packages/ui/src/mosaic/components/select/select.tsx‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export interface SelectItem {
2929
* description hangs below the trigger unless `alignItemWithTrigger` says otherwise.
3030
*/
3131
description?: string;
32+
startAdornment?: React.ReactNode;
3233
disabled?: boolean;
3334
}
3435

@@ -72,6 +73,7 @@ export type SelectTriggerProps = MosaicComponentProps<'button'> & {
7273
variant?: SelectTriggerVariant;
7374
/** Shown in place of the selected label while nothing is selected. */
7475
placeholder?: React.ReactNode;
76+
startAdornment?: React.ReactNode;
7577
};
7678

7779
/**
@@ -83,6 +85,7 @@ export const SelectTrigger = React.forwardRef<HTMLButtonElement, SelectTriggerPr
8385
{
8486
variant = 'outline',
8587
placeholder,
88+
startAdornment,
8689
render,
8790
xstyle,
8891
children,
@@ -135,6 +138,14 @@ export const SelectTrigger = React.forwardRef<HTMLButtonElement, SelectTriggerPr
135138
aria-required={ariaRequired ?? (fieldProps?.required ? true : undefined)}
136139
{...mergeStyleProps(themeProps('select-trigger', { variant }), stylex.props(xstyle), rest)}
137140
>
141+
{startAdornment != null && (
142+
<span
143+
aria-hidden='true'
144+
{...mergeStyleProps(themeProps('select-trigger-adornment'), stylex.props(reset.base, slots.adornment.base))}
145+
>
146+
{startAdornment}
147+
</span>
148+
)}
138149
{children ?? (
139150
<Primitive.Value
140151
id={valueId}
@@ -212,7 +223,7 @@ export interface SelectOptionProps
212223

213224
/** A single choice: its label, an optional description, and a check when it is the selection. */
214225
export const SelectOption = React.forwardRef<HTMLButtonElement, SelectOptionProps>(function MosaicSelectOption(
215-
{ label, description, xstyle, ...rest },
226+
{ label, description, startAdornment, xstyle, ...rest },
216227
ref,
217228
) {
218229
const id = React.useId();
@@ -239,6 +250,14 @@ export const SelectOption = React.forwardRef<HTMLButtonElement, SelectOptionProp
239250
rest,
240251
)}
241252
>
253+
{startAdornment != null && (
254+
<span
255+
aria-hidden='true'
256+
{...mergeStyleProps(themeProps('select-option-adornment'), stylex.props(reset.base, slots.adornment.base))}
257+
>
258+
{startAdornment}
259+
</span>
260+
)}
242261
<span {...mergeStyleProps(themeProps('select-option-content'), stylex.props(reset.base, slots.content.base))}>
243262
<span
244263
id={labelId}

‎packages/ui/src/mosaic/features/user-profile/__tests__/user-profile-add-sms.dialog.test.tsx‎

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { render, screen, waitFor } from '@testing-library/react';
1+
import { render, screen, waitFor, within } from '@testing-library/react';
22
import userEvent from '@testing-library/user-event';
33
import { useState } from 'react';
44
import { describe, expect, it, vi } from 'vitest';
@@ -39,6 +39,36 @@ function renderView(overrides: Partial<UserProfileAddSmsDialogProps> = {}) {
3939
}
4040

4141
describe('UserProfileAddSmsDialog', () => {
42+
it('shows decorative country flags in the options and updates the selected flag', async () => {
43+
const user = userEvent.setup();
44+
const { props, rerender } = renderView({
45+
phoneNumbers: [
46+
{ id: 'personal', phoneNumber: '+18015550100' },
47+
{ id: 'canada', phoneNumber: '+14165550100' },
48+
],
49+
});
50+
const select = screen.getByRole('combobox', { name: 'Phone number +1 (801) 555-0100' });
51+
expect(within(select).getByText('🇺🇸')).toHaveAttribute('aria-hidden', 'true');
52+
await user.click(select);
53+
const us = screen.getByRole('option', { name: '+1 (801) 555-0100' });
54+
const canada = screen.getByRole('option', { name: '+1 (416) 555-0100' });
55+
expect(within(us).getByText('🇺🇸')).toHaveAttribute('aria-hidden', 'true');
56+
expect(within(canada).getByText('🇨🇦')).toHaveAttribute('aria-hidden', 'true');
57+
await user.click(canada);
58+
expect(props.onSelectedPhoneIdChange).toHaveBeenCalledWith('canada');
59+
rerender(
60+
<MosaicProvider>
61+
<UserProfileAddSmsDialog
62+
{...props}
63+
selectedPhoneId='canada'
64+
/>
65+
</MosaicProvider>,
66+
);
67+
expect(select).toHaveAccessibleName('Phone number +1 (416) 555-0100');
68+
expect(within(select).getByText('🇨🇦')).toHaveAttribute('aria-hidden', 'true');
69+
expect(within(select).queryByText('🇺🇸')).not.toBeInTheDocument();
70+
});
71+
4272
it('chooses an existing number with Select or requests a new number', async () => {
4373
const user = userEvent.setup();
4474
const { props } = renderView();

‎packages/ui/src/mosaic/features/user-profile/user-profile-add-sms.dialog.tsx‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { useMergeRefs } from '@floating-ui/react';
22
import type { Ref } from 'react';
33
import { useId, useRef } from 'react';
44

5-
import { stringToFormattedPhoneString } from '../../../utils/phoneUtils';
5+
import { getFlagEmojiFromCountryIso, parsePhoneString } from '../../../utils/phoneUtils';
66
import { Button, SubmitButton } from '../../components/button';
77
import { Card } from '../../components/card';
88
import type { DialogTriggerProps } from '../../components/dialog';
@@ -118,6 +118,14 @@ export function UserProfileAddSmsDialog(props: UserProfileAddSmsDialogProps) {
118118
function SelectPhoneStep(props: UserProfileAddSmsDialogProps & { inputRef: Ref<HTMLButtonElement> }) {
119119
const formId = useId();
120120
const inputRef = useMergeRefs([props.inputRef, useFlowAutoFocus<HTMLButtonElement>()]);
121+
const items = props.phoneNumbers.map(phone => {
122+
const parsed = parsePhoneString(phone.phoneNumber);
123+
return {
124+
value: phone.id,
125+
label: parsed.formattedNumberWithCode,
126+
startAdornment: getFlagEmojiFromCountryIso(parsed.iso),
127+
};
128+
});
121129
return (
122130
<>
123131
<Card.Header>
@@ -144,16 +152,14 @@ function SelectPhoneStep(props: UserProfileAddSmsDialogProps & { inputRef: Ref<H
144152
>
145153
<Field.Label>{m.phoneLabel}</Field.Label>
146154
<Select.Root
147-
items={props.phoneNumbers.map(phone => ({
148-
value: phone.id,
149-
label: stringToFormattedPhoneString(phone.phoneNumber),
150-
}))}
155+
items={items}
151156
value={props.selectedPhoneId}
152157
onValueChange={props.onSelectedPhoneIdChange}
153158
>
154159
<Select.Trigger
155160
ref={inputRef}
156161
placeholder={m.phonePlaceholder}
162+
startAdornment={items.find(item => item.value === props.selectedPhoneId)?.startAdornment}
157163
/>
158164
<Select.Popup />
159165
</Select.Root>

0 commit comments

Comments
 (0)