diff --git a/index.ts b/index.ts index 703488c..10e070b 100644 --- a/index.ts +++ b/index.ts @@ -70,12 +70,7 @@ export {renewSession, onSessionRenewed, logout} from './src/sessions'; export {showAppRating} from './src/app-rating'; -export { - bottomSheet, - bottomSheetSingleSelector, - bottomSheetActionSelector, - bottomSheetInfo, -} from './src/bottom-sheet'; +export {bottomSheet} from './src/bottom-sheet'; export type { SheetRowItem, SheetActionItem, diff --git a/src/__tests__/bottom-sheet-test.ts b/src/__tests__/bottom-sheet-test.ts index 341ef5c..557e114 100644 --- a/src/__tests__/bottom-sheet-test.ts +++ b/src/__tests__/bottom-sheet-test.ts @@ -1,186 +1,39 @@ -import { - bottomSheetActions, - bottomSheetActionSelector, - bottomSheetInfo, - bottomSheetSingleSelector, - SheetActionItem, - SheetInfoItem, -} from '../bottom-sheet'; +import {bottomSheet} from '../bottom-sheet'; import {createFakeAndroidPostMessage} from './fake-post-message'; -import type {SheetRowItem} from '../bottom-sheet'; - -test('single selection', (done) => { +test('bottomSheet', (done) => { const title = 'any title'; const subtitle = 'any subtitle'; const description = 'any description'; const selectedId = 'item-0'; - const items: Array = [ - { - id: 'item-0', - title: 'item 0 title', - description: 'item 0 description', - icon: { - url: 'https://example.com/icon.png', - urlDark: 'https://example.com/dark-icon.png', - }, - }, - { - id: 'item-1', - title: 'item 1 title', - description: 'item 1 description', - }, - { - id: 'item-2', - title: 'item 2 title', - description: 'item 2 description', - }, - ]; - - createFakeAndroidPostMessage({ - checkMessage: (message) => { - expect(message.type).toBe('SHEET'); - expect(message.payload).toEqual({ - title, - subtitle, - description, - content: [ - { - id: 'list-0', - type: 'LIST', - listType: 'SINGLE_SELECTION', - autoSubmit: true, - selectedIds: [selectedId], - items, - }, - ], - }); - }, - getResponse: (message) => ({ - type: message.type, - id: message.id, - payload: { - action: 'SUBMIT', - result: [ - { - id: 'list-0', - selectedIds: ['item-2'], - }, - ], - }, - }), - }); - - bottomSheetSingleSelector({ - title, - subtitle, - description, - selectedId, - items, - }).then((res) => { - expect(res).toEqual({action: 'SUBMIT', selectedId: 'item-2'}); - done(); - }); -}); - -test('actions', (done) => { - const title = 'any title'; - const subtitle = 'any subtitle'; - const description = 'any description'; - const items: Array = [ - { - id: 'item-0', - title: 'item 0 title', - icon: { - url: 'https://example.com/icon.png', - urlDark: 'https://example.com/dark-icon.png', - }, - }, - { - id: 'item-1', - title: 'item 1 title', - style: 'normal', - }, + const content = [ { - id: 'item-2', - title: 'item 2 title', - style: 'destructive', - }, - ]; - - createFakeAndroidPostMessage({ - checkMessage: (message) => { - expect(message.type).toBe('SHEET'); - expect(message.payload).toEqual({ - title, - subtitle, - description, - content: [ - { - id: 'list-0', - type: 'LIST', - listType: 'ACTIONS', - autoSubmit: true, - selectedIds: [], - items, - }, - ], - }); - }, - getResponse: (message) => ({ - type: message.type, - id: message.id, - payload: { - action: 'SUBMIT', - result: [ - { - id: 'list-0', - selectedIds: ['item-2'], + id: 'list-0', + type: 'LIST' as const, + listType: 'SINGLE_SELECTION' as const, + autoSubmit: true, + selectedIds: [selectedId], + items: [ + { + id: 'item-0', + title: 'item 0 title', + description: 'item 0 description', + icon: { + url: 'https://example.com/icon.png', + urlDark: 'https://example.com/dark-icon.png', }, - ], - }, - }), - }); - - bottomSheetActionSelector({ - title, - subtitle, - description, - items, - }).then((res) => { - expect(res).toEqual({action: 'SUBMIT', selectedId: 'item-2'}); - done(); - }); -}); - -test('informative', (done) => { - const title = 'any title'; - const subtitle = 'any subtitle'; - const description = 'any description'; - const items: Array = [ - { - id: 'item-0', - title: 'item 0 title', - icon: { - url: 'https://example.com/icon.png', - urlDark: 'https://example.com/dark-icon.png', - type: 'regular', - }, - }, - { - id: 'item-1', - title: 'item 1 title', - icon: { - type: 'small', - url: 'https://example.com/icon.png', - }, - }, - { - id: 'item-2', - title: 'item 2 title', - icon: { - type: 'bullet', - }, + }, + { + id: 'item-1', + title: 'item 1 title', + description: 'item 1 description', + }, + { + id: 'item-2', + title: 'item 2 title', + description: 'item 2 description', + }, + ], }, ]; @@ -191,16 +44,7 @@ test('informative', (done) => { title, subtitle, description, - content: [ - { - id: 'list-0', - type: 'LIST', - listType: 'INFORMATIVE', - autoSubmit: false, - selectedIds: [], - items, - }, - ], + content, }); }, getResponse: (message) => ({ @@ -218,74 +62,21 @@ test('informative', (done) => { }), }); - bottomSheetInfo({ - title, - subtitle, - description, - items, - }).then((res) => { - expect(res).toBe(undefined); - done(); - }); -}); - -test('actions', (done) => { - const title = 'any title'; - const subtitle = 'any subtitle'; - const description = 'any description'; - const button = { - text: 'button text', - }; - const link = { - text: 'link text', - chevron: true, - }; - const secondaryButton = { - text: 'secondary button text', - }; - - createFakeAndroidPostMessage({ - checkMessage: (message) => { - expect(message.type).toBe('SHEET'); - expect(message.payload).toEqual({ - title, - subtitle, - description, - content: [ - { - id: 'bottom-actions-0', - type: 'BOTTOM_ACTIONS', - button, - link, - secondaryButton, - }, - ], - }); - }, - getResponse: (message) => ({ - type: message.type, - id: message.id, - payload: { - action: 'SUBMIT', - result: [ - { - id: 'bottom-actions-0', - selectedIds: ['PRIMARY'], - }, - ], - }, - }), - }); - - bottomSheetActions({ + bottomSheet({ title, subtitle, description, - button, - secondaryButton, - link, + content, }).then((res) => { - expect(res).toEqual({action: 'PRIMARY'}); + expect(res).toEqual({ + action: 'SUBMIT', + result: [ + { + id: 'list-0', + selectedIds: ['item-2'], + }, + ], + }); done(); }); }); diff --git a/src/bottom-sheet.ts b/src/bottom-sheet.ts index 2dbc62e..68faba3 100644 --- a/src/bottom-sheet.ts +++ b/src/bottom-sheet.ts @@ -109,188 +109,3 @@ export const bottomSheet = async (payload: SheetUI): Promise => { throw e; } }; - -export const bottomSheetSingleSelector = ({ - title, - subtitle, - description, - selectedId, - items, -}: { - title?: string; - subtitle?: string; - description?: string; - selectedId?: string; - items: Array; -}): Promise< - | { - action: 'SUBMIT'; - selectedId: string; - } - | { - action: 'DISMISS'; - selectedId: null; - } -> => - bottomSheet({ - title, - subtitle, - description, - content: [ - { - type: 'LIST', - id: 'list-0', - listType: 'SINGLE_SELECTION', - autoSubmit: true, - selectedIds: typeof selectedId === 'string' ? [selectedId] : [], - items, - }, - ], - }).then(({action, result}) => { - if (action === 'SUBMIT') { - return { - action, - selectedId: result[0].selectedIds[0], - }; - } else { - return { - action, - selectedId: null, - }; - } - }); - -export const bottomSheetActionSelector = ({ - title, - subtitle, - description, - items, -}: { - title?: string; - subtitle?: string; - description?: string; - items: Array; -}): Promise< - | { - action: 'SUBMIT'; - selectedId: string; - } - | { - action: 'DISMISS'; - selectedId: null; - } -> => - bottomSheet({ - title, - subtitle, - description, - content: [ - { - type: 'LIST', - id: 'list-0', - listType: 'ACTIONS', - autoSubmit: true, - selectedIds: [], - items, - }, - ], - }).then(({action, result}) => { - if (action === 'SUBMIT') { - return { - action, - selectedId: result[0].selectedIds[0], - }; - } else { - return { - action, - selectedId: null, - }; - } - }); - -export const bottomSheetInfo = async ({ - title, - subtitle, - description, - items, -}: { - title?: string; - subtitle?: string; - description?: string; - items: Array; -}): Promise => { - await bottomSheet({ - title, - subtitle, - description, - content: [ - { - type: 'LIST', - id: 'list-0', - listType: 'INFORMATIVE', - autoSubmit: false, - selectedIds: [], - items, - }, - ], - }); -}; - -export const bottomSheetActions = async ({ - title, - subtitle, - description, - button, - secondaryButton, - link, -}: { - title?: string; - subtitle?: string; - description?: string; - button: { - text: string; - }; - secondaryButton?: { - text: string; - }; - link?: { - text: string; - withChevron?: boolean; - }; -}): Promise<{ - action: 'PRIMARY' | 'SECONDARY' | 'LINK' | 'DISMISS'; -}> => { - return bottomSheet({ - title, - subtitle, - description, - content: [ - { - type: 'BOTTOM_ACTIONS', - id: 'bottom-actions-0', - button, - secondaryButton, - link, - }, - ], - }).then(({action, result}) => { - if (action === 'SUBMIT') { - const bottomActionsResult = result.find( - ({id}) => id === 'bottom-actions-0', - ); - const pressedAction = bottomActionsResult?.selectedIds[0]; - if ( - pressedAction === 'PRIMARY' || - pressedAction === 'SECONDARY' || - pressedAction === 'LINK' - ) { - return { - action: pressedAction, - }; - } - } - return { - action: 'DISMISS', - }; - }); -};