Skip to content

Commit f525dee

Browse files
committed
add isCollapseDetail prop to support collapsing row details in tables
1 parent 7a0dc06 commit f525dee

4 files changed

Lines changed: 42 additions & 8 deletions

File tree

example/src/components/primary/Table/Table.stories.tsx

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ export const WithRowSpanField: Story = {
241241

242242
export const WithDetail: Story = {
243243
args: {
244+
isCollapseDetail: true,
244245
title: {
245246
plus: {text: '#', col: 40, titleAlign: 'center', dataAlign: 'center'},
246247
...baseData.title,
@@ -273,7 +274,41 @@ export const WithDetail: Story = {
273274
}
274275
};
275276

276-
export const WithClickRow: Story = {
277+
export const WithDetailClickCol: Story = {
278+
args: {
279+
title: {
280+
plus: {text: '#', col: 40, titleAlign: 'center', dataAlign: 'center'},
281+
...baseData.title,
282+
},
283+
data: baseData.data.map(row => {
284+
return {
285+
...row,
286+
detail: [
287+
{
288+
plus: {colSpan: 4, className: 'detail-css', value: <div style={{color: '#000', fontWeight: 700}}>Fax</div>},
289+
amount: {dataAlign: 'right', value: 10},
290+
},
291+
{
292+
plus: {colSpan: 4, value: <div style={{color: '#000', fontWeight: 700}}>Total</div>},
293+
amount: {value: calcAmount(paginateData), dataAlign: 'right'},
294+
}
295+
],
296+
field: {
297+
...row.field,
298+
plus: (args) => <CollapseButton
299+
type="button"
300+
onClick={args.collapse}
301+
data-active={args.isActive ? '':undefined}
302+
>
303+
{args.isActive ? '-': '+'}
304+
</CollapseButton>,
305+
}
306+
};
307+
})
308+
}
309+
};
310+
311+
export const WithDetailClickRow: Story = {
277312
args: {
278313
...baseData,
279314
data: baseData.data.map(row => {

src/Body/Body.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ interface IProps<K extends TBodyDataFieldKey, I extends TBodyDataID> {
4343
isEnableDragSortable?: boolean
4444
onChangeSortable?: TOnChangeSortable
4545
onHover?: (id: I) => void
46+
isCollapseDetail?: boolean
4647
}
4748

4849

@@ -55,6 +56,7 @@ const Body = <K extends TBodyDataFieldKey, I extends TBodyDataID>({
5556
tableMode,
5657
isEnableDragSortable,
5758
onChangeSortable,
59+
isCollapseDetail,
5860
}: IProps<K, I>) => {
5961
const items = useMemo<I[]>(() => {
6062
return data?.map(row => row.id) ?? [];
@@ -164,6 +166,7 @@ const Body = <K extends TBodyDataFieldKey, I extends TBodyDataID>({
164166
let cellTdIndex = 0;
165167

166168

169+
const isCollapse = isCollapseDetail || collapseIds?.includes(dataRow.id);
167170

168171

169172
const titleKeys = objectKeys(title);
@@ -203,7 +206,7 @@ const Body = <K extends TBodyDataFieldKey, I extends TBodyDataID>({
203206
const nthType = cellTdIndex % 2 === 0 ? 'odd': 'even';
204207

205208

206-
const children = getBodyData(field, collapseIds.includes(dataRow.id), collapseEvent);
209+
const children = getBodyData(field, isCollapse, collapseEvent);
207210

208211
const stickyLeftStyles = getCalcStickyLeftStyles(fieldConfig.sticky === 'left' ? stickyLeft.widths: stickyRight.widths, fieldConfig.sticky);
209212

@@ -234,7 +237,6 @@ const Body = <K extends TBodyDataFieldKey, I extends TBodyDataID>({
234237
}, []);
235238

236239

237-
const isCollapse = collapseIds?.includes(dataRow.id);
238240

239241
return (<React.Fragment
240242
key={dataRow.id}

src/Body/BodyDetail.tsx

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,6 @@ interface IProps <K extends TBodyDataFieldKey>{
2121

2222

2323

24-
25-
26-
27-
28-
2924
/**
3025
* Table Body Detail
3126
* 額外顯示資訊 例如 詳細

src/Table.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ const Table = <I extends TBodyDataID, K extends TBodyDataFieldKey>({
5959
isVisiblePagePicker = true,
6060
isVisiblePageLimit = true,
6161
isVisiblePageInfo = true,
62+
isCollapseDetail = false,
6263
}: ITableProps<I, K>) => {
6364
const [tableMode, setTableMode] = useState<ETableMode>(ETableMode.table);
6465

@@ -192,6 +193,7 @@ const Table = <I extends TBodyDataID, K extends TBodyDataFieldKey>({
192193
tableMode={tableMode}
193194
title={title}
194195
data={data}
196+
isCollapseDetail={isCollapseDetail}
195197
isEnableDragSortable={isEnableDragSortable}
196198
onChangeSortable={onChangeSortable}
197199
/>;

0 commit comments

Comments
 (0)