|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +import React, { useRef, useState } from 'react'; |
| 5 | + |
| 6 | +import { UseCollectionResult } from '@cloudscape-design/collection-hooks'; |
| 7 | + |
| 8 | +import { |
| 9 | + Box, |
| 10 | + Button, |
| 11 | + Checkbox, |
| 12 | + FormField, |
| 13 | + Modal, |
| 14 | + Popover, |
| 15 | + PopoverProps, |
| 16 | + Select, |
| 17 | + SelectProps, |
| 18 | + SpaceBetween, |
| 19 | + Token, |
| 20 | +} from '~components'; |
| 21 | + |
| 22 | +import { groupOptions, sortOptions } from './grouped-table-config'; |
| 23 | +import { GroupDefinition, isGroupRow, TransactionRow } from './grouped-table-data'; |
| 24 | +import { createWysiwygQuery } from './grouped-table-query'; |
| 25 | + |
| 26 | +import styles from './styles.scss'; |
| 27 | + |
| 28 | +export function FilterLayout({ filter, dataGrouping }: { filter: React.ReactNode; dataGrouping: React.ReactNode }) { |
| 29 | + return ( |
| 30 | + <div className={styles['filter-layout']}> |
| 31 | + <div className={styles['filter-layout-filter']}>{filter}</div> |
| 32 | + <div>{dataGrouping}</div> |
| 33 | + </div> |
| 34 | + ); |
| 35 | +} |
| 36 | + |
| 37 | +export function DataGrouping({ |
| 38 | + groups, |
| 39 | + onChange, |
| 40 | +}: { |
| 41 | + groups: GroupDefinition[]; |
| 42 | + onChange: (groups: GroupDefinition[]) => void; |
| 43 | +}) { |
| 44 | + const popoverRef = useRef<PopoverProps.Ref>(null); |
| 45 | + const propertyToGroup = new Map(groups.map(g => [g.property, g])); |
| 46 | + return ( |
| 47 | + <div className={styles['grouping-wrapper']}> |
| 48 | + <Box fontWeight="bold">group by</Box> |
| 49 | + <ul aria-label="selected data groups"> |
| 50 | + {groups.map(({ property, sorting }, groupIndex) => { |
| 51 | + const groupLabel = `${groupOptions.find(o => o.value === property)!.label} (${sorting})`; |
| 52 | + return ( |
| 53 | + <li key={property}> |
| 54 | + <Token |
| 55 | + ariaLabel={groupLabel} |
| 56 | + label={ |
| 57 | + <Popover |
| 58 | + ref={popoverRef} |
| 59 | + header="Edit group" |
| 60 | + content={ |
| 61 | + <GroupEditor |
| 62 | + group={{ property, sorting }} |
| 63 | + groupOptions={groupOptions.filter(o => o.value === property || !propertyToGroup.get(o.value))} |
| 64 | + onDismiss={() => popoverRef.current?.dismiss()} |
| 65 | + onSubmit={updated => { |
| 66 | + onChange(groups.map((g, index) => (index === groupIndex ? updated : g))); |
| 67 | + popoverRef.current?.dismiss(); |
| 68 | + }} |
| 69 | + /> |
| 70 | + } |
| 71 | + > |
| 72 | + {groupLabel} |
| 73 | + </Popover> |
| 74 | + } |
| 75 | + onDismiss={() => onChange(groups.filter((_, index) => index !== groupIndex))} |
| 76 | + dismissLabel={`Remove group ${groupLabel}`} |
| 77 | + /> |
| 78 | + </li> |
| 79 | + ); |
| 80 | + })} |
| 81 | + </ul> |
| 82 | + {groups.length < 5 && ( |
| 83 | + <Popover |
| 84 | + ref={popoverRef} |
| 85 | + triggerType="custom" |
| 86 | + header="Add group" |
| 87 | + content={ |
| 88 | + <GroupEditor |
| 89 | + group={{ property: undefined, sorting: undefined }} |
| 90 | + groupOptions={groupOptions.filter(o => !propertyToGroup.get(o.value))} |
| 91 | + onDismiss={() => popoverRef.current?.dismiss()} |
| 92 | + onSubmit={group => { |
| 93 | + onChange([...groups, group]); |
| 94 | + popoverRef.current?.dismiss(); |
| 95 | + }} |
| 96 | + /> |
| 97 | + } |
| 98 | + > |
| 99 | + <Button variant="inline-link">add group</Button> |
| 100 | + </Popover> |
| 101 | + )} |
| 102 | + </div> |
| 103 | + ); |
| 104 | +} |
| 105 | + |
| 106 | +function GroupEditor({ |
| 107 | + group, |
| 108 | + groupOptions, |
| 109 | + onDismiss, |
| 110 | + onSubmit, |
| 111 | +}: { |
| 112 | + group: Partial<GroupDefinition>; |
| 113 | + groupOptions: SelectProps.Option[]; |
| 114 | + onDismiss: () => void; |
| 115 | + onSubmit: (group: GroupDefinition) => void; |
| 116 | +}) { |
| 117 | + const [selectedProperty, setSelectedProperty] = useState<null | string>(group.property ?? null); |
| 118 | + const [selectedSorting, setSelectedSorting] = useState<null | 'asc' | 'desc'>(group.sorting ?? null); |
| 119 | + return ( |
| 120 | + <SpaceBetween size="m"> |
| 121 | + <FormField label="Property"> |
| 122 | + <Select |
| 123 | + options={groupOptions} |
| 124 | + selectedOption={groupOptions.find(o => o.value === selectedProperty) ?? null} |
| 125 | + onChange={({ detail }) => setSelectedProperty(detail.selectedOption.value!)} |
| 126 | + /> |
| 127 | + </FormField> |
| 128 | + <FormField label="Sort by"> |
| 129 | + <Select |
| 130 | + options={sortOptions} |
| 131 | + selectedOption={sortOptions.find(o => o.value === selectedSorting) ?? null} |
| 132 | + onChange={({ detail }) => setSelectedSorting(detail.selectedOption.value as 'asc' | 'desc')} |
| 133 | + /> |
| 134 | + </FormField> |
| 135 | + <SpaceBetween size="m" direction="horizontal"> |
| 136 | + <Button variant="link" onClick={onDismiss}> |
| 137 | + Cancel |
| 138 | + </Button> |
| 139 | + <Button |
| 140 | + disabled={!selectedProperty || !selectedSorting} |
| 141 | + onClick={() => onSubmit({ property: selectedProperty!, sorting: selectedSorting! })} |
| 142 | + > |
| 143 | + Apply |
| 144 | + </Button> |
| 145 | + </SpaceBetween> |
| 146 | + </SpaceBetween> |
| 147 | + ); |
| 148 | +} |
| 149 | + |
| 150 | +export function BulkActionModal({ |
| 151 | + collection, |
| 152 | + onDismiss, |
| 153 | + onSubmit, |
| 154 | +}: { |
| 155 | + collection: UseCollectionResult<TransactionRow>; |
| 156 | + onDismiss: () => void; |
| 157 | + onSubmit: () => void; |
| 158 | +}) { |
| 159 | + const [includeNew, setIncludeNew] = useState(true); |
| 160 | + const selectedItemsCount = collection.collectionProps.selectedItems?.length ?? 0; |
| 161 | + const query = createWysiwygQuery(collection.items, { |
| 162 | + getId: item => item.key, |
| 163 | + getGroup: item => [item.groupKey, item.group], |
| 164 | + getChildren: item => (isGroupRow(item) ? item.children : []), |
| 165 | + selection: collection.collectionProps.expandableRows!.groupSelection!, |
| 166 | + filter: collection.propertyFilterProps!.query, |
| 167 | + }); |
| 168 | + return ( |
| 169 | + <Modal |
| 170 | + header="Update confirmation" |
| 171 | + visible={true} |
| 172 | + onDismiss={onDismiss} |
| 173 | + footer={ |
| 174 | + <Box float="right"> |
| 175 | + <SpaceBetween direction="horizontal" size="xs"> |
| 176 | + <Button variant="link" onClick={onDismiss}> |
| 177 | + Cancel |
| 178 | + </Button> |
| 179 | + <Button variant="primary" onClick={onSubmit}> |
| 180 | + Submit |
| 181 | + </Button> |
| 182 | + </SpaceBetween> |
| 183 | + </Box> |
| 184 | + } |
| 185 | + > |
| 186 | + <SpaceBetween size="s"> |
| 187 | + <Box fontWeight="bold">You selected {selectedItemsCount} transactions for update.</Box> |
| 188 | + |
| 189 | + <Checkbox checked={includeNew} onChange={({ detail }) => setIncludeNew(detail.checked)}> |
| 190 | + Include new transactions that match the current filters. |
| 191 | + </Checkbox> |
| 192 | + |
| 193 | + <FormField label="Update query"> |
| 194 | + <Box variant="awsui-inline-code">{query}</Box> |
| 195 | + </FormField> |
| 196 | + </SpaceBetween> |
| 197 | + </Modal> |
| 198 | + ); |
| 199 | +} |
0 commit comments