import type { Meta, StoryObj } from '@storybook/react-webpack5'; import { userEvent, within } from 'storybook/test'; import { Flag } from '@wise/art'; import { useRtlDiffI18n, withRTLDiff } from '../../../../.storybook/decorators/RtlDiffDecorator'; import { SelectInput, SelectInputOptionContent } from '..'; export default { component: SelectInput, title: 'Forms/SelectInput/Tests/RTL Diff', tags: ['!autodocs', '!manifest'], } satisfies Meta; type Story = StoryObj; interface RtlCurrency { code: string; name: string; } /** Currency codes stay Latin — only the full names are localised. */ const getRtlCurrencies = (i18n: Record): RtlCurrency[] => [ { code: 'USD', name: i18n.usDollar }, { code: 'EUR', name: i18n.euro }, { code: 'GBP', name: i18n.britishPound }, ]; const currencyNames = { usDollar: { en: 'United States Dollar', ar: 'الدولار الأمريكي' }, euro: { en: 'Euro', ar: 'اليورو' }, britishPound: { en: 'British pound', ar: 'الجنيه الإسترليني' }, }; export const TriggerVariants: Story = { render: () => { const i18n = useRtlDiffI18n(); const rtlCurrencies = getRtlCurrencies(i18n); return ( <> {/* sm — chevron position flips in RTL */} ({ type: 'option', value: currency }))} renderValue={(currency) => } /> {/* md — chevron position flips in RTL */} ({ type: 'option', value: currency }))} renderValue={(currency) => } /> {/* lg with value and leading icon — icon side and chevron flip in RTL */} ({ type: 'option', value: currency }))} renderValue={(currency) => ( } /> )} /> {/* multiple — selected values on leading side, chevron on trailing side, both flip in RTL */} ({ type: 'option', value: currency }))} renderValue={(currency, withinTrigger) => withinTrigger ? ( currency.code ) : ( ) } /> ); }, ...withRTLDiff({ fixedRows: { heights: 'auto' }, i18n: { ...currencyNames, selectCurrency: { en: 'Select currency', ar: 'اختر العملة' }, chooseCurrencies: { en: 'Choose currencies', ar: 'اختر العملات' }, }, }), }; const getOtherCurrencies = (i18n: Record): RtlCurrency[] => [ { code: 'CAD', name: i18n.canadianDollar }, { code: 'AUD', name: i18n.australianDollar }, { code: 'JPY', name: i18n.japaneseYen }, ]; const getGroupedItems = (i18n: Record) => { const rtlCurrencies = getRtlCurrencies(i18n); return [ { type: 'group' as const, label: i18n.popularCurrencies, options: rtlCurrencies.map((currency) => ({ type: 'option' as const, value: currency, filterMatchers: [currency.code, currency.name], })), }, { type: 'group' as const, label: i18n.allCurrencies, options: [...rtlCurrencies, ...getOtherCurrencies(i18n)] .sort((a, b) => a.code.localeCompare(b.code)) .map((currency) => ({ type: 'option' as const, value: currency, filterMatchers: [currency.code, currency.name], })), }, ]; }; export const OpenDropdown: Story = { render: () => { const i18n = useRtlDiffI18n(); return ( ( } /> )} /> ); }, play: async ({ canvasElement }) => { const canvas = within(canvasElement); const trigger = canvas.queryByRole('combobox'); if (!trigger) return; await userEvent.click(trigger); }, ...withRTLDiff({ renderAs: 'iframe', width: '80%', i18n: { ...currencyNames, filterPlaceholder: { en: 'Type a currency / country', ar: 'اكتب عملة / دولة', }, selectCurrency: { en: 'Select currency', ar: 'اختر العملة' }, popularCurrencies: { en: 'Popular currencies', ar: 'العملات الشائعة' }, allCurrencies: { en: 'All currencies', ar: 'جميع العملات' }, canadianDollar: { en: 'Canadian dollar', ar: 'الدولار الكندي' }, australianDollar: { en: 'Australian dollar', ar: 'الدولار الأسترالي' }, japaneseYen: { en: 'Japanese yen', ar: 'الين الياباني' }, }, }), };