This repository has been archived by the owner on May 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(widget): Enhance 9dof table widget (#56)
- define types for table column and item - new component Table - clean up imports - use title from user configuration Co-authored-by: Pablo Klaschka <[email protected]>
- Loading branch information
Showing
7 changed files
with
124 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
export type ColumnKey = string; | ||
|
||
/** | ||
* A table column. | ||
*/ | ||
export interface Column { | ||
/** | ||
* The key of the table column. | ||
*/ | ||
key: ColumnKey; | ||
|
||
/** | ||
* The name/title of the table column. | ||
*/ | ||
name: string | JSX.Element; | ||
} | ||
|
||
export type ItemValue = string | number | boolean; | ||
|
||
/** | ||
* A table item. | ||
*/ | ||
export type Item = { | ||
/** | ||
* Unique key of the table item. | ||
*/ | ||
key: string; | ||
} & Record<ColumnKey, ItemValue>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { | ||
Table as RSTable, | ||
TableHeader, | ||
Column, | ||
TableBody, | ||
Row, | ||
Cell | ||
} from '@react-spectrum/table'; | ||
import { Column as ColumnType, Item as ItemType, ItemValue } from './model'; | ||
|
||
function display(value: ItemValue): string | number { | ||
switch (typeof value) { | ||
case 'boolean': | ||
return value ? 'true' : 'false'; | ||
case 'number': | ||
return Math.round(value * 1000) / 1000; | ||
case 'string': | ||
return value; | ||
default: | ||
throw new TypeError( | ||
`Type ${typeof value} can not be rendered in a table cell.` | ||
); | ||
} | ||
} | ||
|
||
export interface TableProps { | ||
columns: ColumnType[]; | ||
items: ItemType[]; | ||
} | ||
|
||
export function Table({ columns, items }: TableProps) { | ||
return ( | ||
<RSTable> | ||
<TableHeader columns={columns}> | ||
{column => <Column>{column.name}</Column>} | ||
</TableHeader> | ||
<TableBody items={items}> | ||
{item => ( | ||
<Row key={item.key}>{key => <Cell>{display(item[key])}</Cell>}</Row> | ||
)} | ||
</TableBody> | ||
</RSTable> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { Heading, View } from '@adobe/react-spectrum'; | ||
import { useChannelLatest } from '@wuespace/telestion-client-core'; | ||
import { LoadingIndicator } from '@wuespace/telestion-client-common'; | ||
import { BaseRendererProps } from '@wuespace/telestion-client-types'; | ||
|
||
import { NineDofMessage } from '../../model/messages/nine-dof'; | ||
import { NineDOF } from '../../model/channels'; | ||
|
||
import { Table } from './table'; | ||
import { Column, Item } from './model'; | ||
|
||
const columns: Column[] = [ | ||
{ name: 'Sensor', key: 'sensor' }, | ||
{ name: <code>x</code>, key: 'x' }, | ||
{ name: <code>y</code>, key: 'y' }, | ||
{ name: <code>z</code>, key: 'z' } | ||
]; | ||
|
||
export function Widget({ title }: BaseRendererProps) { | ||
const latestData = useChannelLatest<NineDofMessage>(NineDOF)?.result[0]; | ||
|
||
const items: Item[] = [ | ||
{ | ||
sensor: 'Accelerometer', | ||
key: `acc-${latestData ? Object.values(latestData?.acc) : ''}`, | ||
...latestData?.acc | ||
}, | ||
{ | ||
sensor: 'Gyroscope', | ||
key: `gyro-${latestData ? Object.values(latestData?.gyro) : ''}`, | ||
...latestData?.gyro | ||
}, | ||
{ | ||
sensor: 'Magnetometer', | ||
key: `mag-${latestData ? Object.values(latestData?.mag) : ''}`, | ||
...latestData?.mag | ||
} | ||
]; | ||
|
||
return ( | ||
<View marginX={'size-200'}> | ||
<Heading level={3}>{title}</Heading> | ||
<LoadingIndicator dependencies={[latestData]}> | ||
{() => <Table columns={columns} items={items} />} | ||
</LoadingIndicator> | ||
</View> | ||
); | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters