|
| 1 | +import { |
| 2 | + Body, |
| 3 | + Box, |
| 4 | + Button, |
| 5 | + ButtonProps, |
| 6 | + Column, |
| 7 | + Columns, |
| 8 | + Divider, |
| 9 | + IconButton, |
| 10 | + IconChevronDown, |
| 11 | + IconChevronLeft, |
| 12 | + IconChevronRight, |
| 13 | + Inline, |
| 14 | + Inset, |
| 15 | + LocalizedString, |
| 16 | + Menu, |
| 17 | + PaginationItemsPerPage, |
| 18 | + unsafeLocalizedString, |
| 19 | +} from ".."; |
| 20 | +import { useBentoConfig } from "../BentoConfigContext"; |
| 21 | + |
| 22 | +export type PaginationMessages = { |
| 23 | + itemsPerPageOptionsLabel: LocalizedString; |
| 24 | + currentPageItemsLabel: (start: number, end: number, total: number) => LocalizedString; |
| 25 | + pageCountLabel: (pageCount: number) => LocalizedString; |
| 26 | + singlePageLabel: LocalizedString; |
| 27 | + previousPageButtonLabel: LocalizedString; |
| 28 | + nextPageButtonLabel: LocalizedString; |
| 29 | +}; |
| 30 | + |
| 31 | +export type ItemsPerPageOption = PaginationItemsPerPage[number]; |
| 32 | + |
| 33 | +type Props = { |
| 34 | + page: number; |
| 35 | + pageCount: number; |
| 36 | + itemsPerPage: ItemsPerPageOption; |
| 37 | + itemsPerPageOptions: Record<ItemsPerPageOption, LocalizedString>; |
| 38 | + onPageChange: (page: number) => void; |
| 39 | + onItemsPerPageChange: (itemsPerPage: ItemsPerPageOption) => void; |
| 40 | + messages: PaginationMessages; |
| 41 | +}; |
| 42 | + |
| 43 | +export type { Props as PaginationProps }; |
| 44 | + |
| 45 | +type DropdownButtonProps = Pick<ButtonProps, "label" | "onPress" | "isDisabled">; |
| 46 | + |
| 47 | +function DropdownButton({ label, onPress, isDisabled }: DropdownButtonProps) { |
| 48 | + const config = useBentoConfig().pagination; |
| 49 | + return ( |
| 50 | + <Button |
| 51 | + kind={config.dropdownButtonKind} |
| 52 | + hierarchy="secondary" |
| 53 | + label={label} |
| 54 | + icon={IconChevronDown} |
| 55 | + iconPosition="trailing" |
| 56 | + onPress={onPress} |
| 57 | + isDisabled={isDisabled} |
| 58 | + /> |
| 59 | + ); |
| 60 | +} |
| 61 | + |
| 62 | +export function Pagination(props: Props) { |
| 63 | + const { |
| 64 | + page, |
| 65 | + pageCount, |
| 66 | + itemsPerPage, |
| 67 | + itemsPerPageOptions, |
| 68 | + onPageChange, |
| 69 | + onItemsPerPageChange, |
| 70 | + messages, |
| 71 | + } = props; |
| 72 | + |
| 73 | + const config = useBentoConfig().pagination; |
| 74 | + |
| 75 | + const divider = config.showDivider && ( |
| 76 | + <Column width="content"> |
| 77 | + <Divider orientation="vertical" /> |
| 78 | + </Column> |
| 79 | + ); |
| 80 | + |
| 81 | + return ( |
| 82 | + <Inset spaceX={24}> |
| 83 | + <Columns space={24} alignY="stretch"> |
| 84 | + <Column width="content"> |
| 85 | + <Box height="full" display="flex" alignItems="center" paddingY={config.paddingY}> |
| 86 | + <Inline space={8} alignY="center"> |
| 87 | + <Menu |
| 88 | + size="medium" |
| 89 | + trigger={(ref, triggerProps, { toggle }) => ( |
| 90 | + <Box ref={ref} display="inline-block" {...triggerProps} outline="none"> |
| 91 | + <DropdownButton label={unsafeLocalizedString(itemsPerPage)} onPress={toggle} /> |
| 92 | + </Box> |
| 93 | + )} |
| 94 | + items={Object.entries(itemsPerPageOptions).map(([n, label]) => ({ |
| 95 | + label, |
| 96 | + onPress: () => onItemsPerPageChange(parseInt(n) as ItemsPerPageOption), |
| 97 | + }))} |
| 98 | + closeOnSelect |
| 99 | + /> |
| 100 | + <Body size="medium" color="secondary"> |
| 101 | + {messages.itemsPerPageOptionsLabel} |
| 102 | + </Body> |
| 103 | + </Inline> |
| 104 | + </Box> |
| 105 | + </Column> |
| 106 | + {divider} |
| 107 | + <Box height="full" display="flex" alignItems="center"> |
| 108 | + <Body size="medium" color="secondary"> |
| 109 | + {messages.currentPageItemsLabel( |
| 110 | + (page - 1) * itemsPerPage + 1, |
| 111 | + page * itemsPerPage, |
| 112 | + pageCount * itemsPerPage |
| 113 | + )} |
| 114 | + </Body> |
| 115 | + </Box> |
| 116 | + {divider} |
| 117 | + <Box |
| 118 | + height="full" |
| 119 | + width="full" |
| 120 | + display="flex" |
| 121 | + flexDirection="column" |
| 122 | + justifyContent="center" |
| 123 | + paddingY={config.paddingY} |
| 124 | + > |
| 125 | + <Columns space={8} alignY="center"> |
| 126 | + {pageCount === 1 ? ( |
| 127 | + <Body size="medium" color="secondary"> |
| 128 | + {messages.singlePageLabel} |
| 129 | + </Body> |
| 130 | + ) : ( |
| 131 | + <Inline space={8} alignY="center"> |
| 132 | + <Menu |
| 133 | + size="medium" |
| 134 | + trigger={(ref, triggerProps, { toggle }) => ( |
| 135 | + <Box ref={ref} display="inline-block" {...triggerProps} outline="none"> |
| 136 | + <DropdownButton |
| 137 | + label={unsafeLocalizedString(page)} |
| 138 | + onPress={toggle} |
| 139 | + isDisabled={pageCount === 1} |
| 140 | + /> |
| 141 | + </Box> |
| 142 | + )} |
| 143 | + items={Array.from(Array(pageCount).keys()).map((n) => ({ |
| 144 | + label: unsafeLocalizedString(n + 1), |
| 145 | + onPress: () => onPageChange(n + 1), |
| 146 | + }))} |
| 147 | + closeOnSelect |
| 148 | + /> |
| 149 | + <Body size="medium" color="secondary"> |
| 150 | + {messages.pageCountLabel(pageCount)} |
| 151 | + </Body> |
| 152 | + </Inline> |
| 153 | + )} |
| 154 | + <Column width="content"> |
| 155 | + <Inline space={config.navigationButtonSpacing}> |
| 156 | + <IconButton |
| 157 | + kind={config.navigationButtonKind} |
| 158 | + hierarchy="secondary" |
| 159 | + icon={IconChevronLeft} |
| 160 | + label={messages.previousPageButtonLabel} |
| 161 | + size={24} |
| 162 | + onPress={() => onPageChange(page - 1)} |
| 163 | + isDisabled={page === 1} |
| 164 | + /> |
| 165 | + <IconButton |
| 166 | + kind={config.navigationButtonKind} |
| 167 | + hierarchy="secondary" |
| 168 | + icon={IconChevronRight} |
| 169 | + label={messages.nextPageButtonLabel} |
| 170 | + size={24} |
| 171 | + onPress={() => onPageChange(page + 1)} |
| 172 | + isDisabled={page === pageCount} |
| 173 | + /> |
| 174 | + </Inline> |
| 175 | + </Column> |
| 176 | + </Columns> |
| 177 | + </Box> |
| 178 | + </Columns> |
| 179 | + </Inset> |
| 180 | + ); |
| 181 | +} |
0 commit comments