Skip to content

Commit

Permalink
feat: format the decimal fields in database panel
Browse files Browse the repository at this point in the history
close #1592
  • Loading branch information
hamed-musallam committed Jun 28, 2022
1 parent ec2607d commit 4700745
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 21 deletions.
12 changes: 6 additions & 6 deletions src/component/panels/databasePanel/DatabasePreferences.tsx
Expand Up @@ -80,13 +80,13 @@ function DatabasePreferences(props, ref) {
/>
<FormikColumnFormatField
label="Range"
checkControllerName="showRange"
hideFormatField
checkControllerName="range.show"
formatControllerName="range.format"
/>
<FormikColumnFormatField
label="δ (ppm)"
checkControllerName="showDelta"
hideFormatField
checkControllerName="delta.show"
formatControllerName="delta.format"
/>
<FormikColumnFormatField
label="Assignment"
Expand All @@ -95,8 +95,8 @@ function DatabasePreferences(props, ref) {
/>
<FormikColumnFormatField
label="J (Hz)"
checkControllerName="showCoupling"
hideFormatField
checkControllerName="coupling.show"
formatControllerName="coupling.format"
/>
<FormikColumnFormatField
label="Multiplicity"
Expand Down
32 changes: 23 additions & 9 deletions src/component/panels/databasePanel/DatabaseTable.tsx
@@ -1,4 +1,5 @@
/** @jsxImportSource @emotion/react */
import lodashGet from 'lodash/get';
import { useMemo, memo, CSSProperties } from 'react';
import { ResponsiveChart } from 'react-d3-utils';
import { FaPlus } from 'react-icons/fa';
Expand All @@ -10,6 +11,7 @@ import addCustomColumn, {
} from '../../elements/ReactTable/utility/addCustomColumn';
import { HighlightedSource } from '../../highlight';
import { usePanelPreferences } from '../../hooks/usePanelPreferences';
import { formatNumber } from '../../utility/formatNumber';

interface DatabaseTableProps {
data: any;
Expand All @@ -22,7 +24,9 @@ const overFlowStyle: CSSProperties = {
textOverflow: 'ellipsis',
};

const COLUMNS: (CustomColumn & { showWhen: string })[] = [
const databaseTableColumns = (
databasePreferences,
): (CustomColumn & { showWhen: string })[] => [
{
showWhen: 'showNames',
index: 1,
Expand All @@ -37,18 +41,26 @@ const COLUMNS: (CustomColumn & { showWhen: string })[] = [
},
},
{
showWhen: 'showRange',
showWhen: 'range.show',
index: 2,
Header: 'From - To',
accessor: (row) => `${row.from.toFixed(2)} - ${row.to.toFixed(2)}`,
accessor: (row) => {
const rangeFormat = databasePreferences.range.format;
return `${formatNumber(row.from, rangeFormat)} - ${formatNumber(
row.to,
rangeFormat,
)}`;
},
enableRowSpan: true,
},
{
showWhen: 'showDelta',
showWhen: 'delta.show',
index: 3,
Header: 'δ (ppm)',
accessor: 'delta',
accessor: (row) =>
`${formatNumber(row.delta, databasePreferences.delta.format)}`,
},

{
showWhen: 'showAssignment',
index: 4,
Expand All @@ -61,11 +73,13 @@ const COLUMNS: (CustomColumn & { showWhen: string })[] = [
Header: 'Multi.',
accessor: 'multiplicity',
},

{
showWhen: 'showCoupling',
showWhen: 'coupling.show',
index: 6,
Header: 'J (Hz)',
accessor: 'coupling',
accessor: (row) =>
`${formatNumber(row.coupling, databasePreferences.coupling.format)}`,
style: {
width: '60px',
minWidth: '60px',
Expand Down Expand Up @@ -153,9 +167,9 @@ function DatabaseTable({ data, onAdd }: DatabaseTableProps) {

const tableColumns = useMemo(() => {
let columns = [...initialColumns];
for (const col of COLUMNS) {
for (const col of databaseTableColumns(databasePreferences)) {
const { showWhen, ...colParams } = col;
if (databasePreferences[showWhen]) {
if (lodashGet(databasePreferences, showWhen, false)) {
addCustomColumn(columns, colParams);
}
}
Expand Down
Expand Up @@ -60,10 +60,10 @@ const databaseDefaultValues: PanelsPreferences['database'] = {
showSmiles: true,
showSolvent: false,
showNames: true,
showRange: false,
showDelta: true,
range: { show: false, format: '0.00' },
delta: { show: true, format: '0.00' },
showAssignment: false,
showCoupling: true,
coupling: { show: true, format: '0.0' },
showMultiplicity: true,
color: '#C0B000',
marginBottom: 30,
Expand Down
6 changes: 3 additions & 3 deletions src/component/workspaces/Workspace.ts
Expand Up @@ -58,10 +58,10 @@ export interface DatabasePanelPreferences {
showSmiles: boolean;
showSolvent: boolean;
showNames: boolean;
showRange: boolean;
showDelta: boolean;
range: ColumnPreferences;
delta: ColumnPreferences;
showAssignment: boolean;
showCoupling: boolean;
coupling: ColumnPreferences;
showMultiplicity: boolean;
color: string;
marginBottom: number;
Expand Down

0 comments on commit 4700745

Please sign in to comment.