Skip to content

Commit

Permalink
Replace input/output variables list with table
Browse files Browse the repository at this point in the history
  • Loading branch information
vgeorge committed Jun 20, 2024
1 parent 7f54228 commit 2128ca3
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ import {
sortReferences
} from '../../../utils/references';
import { applyNumberCaptionsToDocument } from '../../../utils/apply-number-captions-to-document';
import { VariableItem } from '../single-view/document-body';
import { variableNodeType } from '../../../types';
import { VariablesTable } from '../single-view/common/variables-table';

const ReferencesList = styled.ol`
&& {
Expand Down Expand Up @@ -219,28 +218,6 @@ ImplementationDataList.propTypes = {
)
};

function VariablesList({ list }) {
if (!list || list.length === 0) {
return EMPTY_CONTENT_MESSAGE;
}

return (
<DataListContainer>
{list?.map((variable, i) => (
<VariableItem
key={`variable-${i + 1}`}
variable={variable}
element={{ id: `variable-${i}`, label: `Variable #${i + 1}` }}
/>
))}
</DataListContainer>
);
}

VariablesList.propTypes = {
list: T.arrayOf(variableNodeType)
};

function ContactOutput(props) {
const { data } = props;
const { affiliations, contact, roles } = data;
Expand Down Expand Up @@ -721,15 +698,15 @@ function JournalPdfPreview() {
id='algorithm_input_variables'
title='Algorithm Input Variables'
>
<VariablesList list={algorithm_input_variables} />
<VariablesTable variables={algorithm_input_variables} />
</Section>
)}
{algorithmOutputVariablesVisible && (
<Section
id='algorithm_output_variables'
title='Algorithm Output Variables'
>
<VariablesList list={algorithm_output_variables} />
<VariablesTable variables={algorithm_output_variables} />
</Section>
)}
</Section>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import React from 'react';
import PropTypes from 'prop-types';
import { EmptySection } from '../document-body';
import SafeReadEditor from '../../../slate/safe-read-editor';
import styled from 'styled-components';
import { VariablePropType } from '../../../../types';

const TableElement = styled.table`
width: 100%;
border-collapse: collapse;
margin: 20px 0;
`;

const Tr = styled.tr`
&:nth-child(even) {
background-color: #f2f2f2;
}
`;

const Th = styled.th`
background-color: rgb(244, 245, 247);
border: 1px solid rgb(193, 199, 208);
padding: 8px;
min-width: 48px;
text-align: center;
> * {
margin: 0;
}
`;

const Td = styled.td`
background-color: rgb(255, 255, 255);
border: 1px solid rgb(193, 199, 208);
padding: 8px;
min-width: 48px;
> * {
margin: 0;
}
`;

const NameTd = styled(Td)`
width: 150px;
`;

const UnitTd = styled(Td)`
width: 100px;
`;

export const VariablesTable = ({ variables }) => {
return (
<TableElement>
<thead>
<Tr>
<Th>Name</Th>
<Th>Long Name</Th>
<Th>Unit</Th>
</Tr>
</thead>
<tbody>
{variables.map((variable) => (
<Tr key={variable.name.children[0].children[0].text}>
<NameTd>
<SafeReadEditor
value={variable.name}
whenEmpty={<EmptySection />}
/>
</NameTd>
<Td>
<SafeReadEditor
value={variable.long_name}
whenEmpty={<EmptySection />}
/>
</Td>
<UnitTd>
<SafeReadEditor
value={variable.unit}
whenEmpty={<EmptySection />}
/>
</UnitTd>
</Tr>
))}
</tbody>
</TableElement>
);
};

VariablesTable.propTypes = {
variables: PropTypes.arrayOf(VariablePropType).isRequired
};
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { useCommentCenter } from '../../../context/comment-center';
import { isJournalPublicationIntended } from '../status';
import serializeSlateToString from '../../slate/serialize-to-string';
import { useContextualAbility } from '../../../a11n';
import { VariablesTable } from './common/variables-table';

const PDFPreview = styled.iframe`
width: 100%;
Expand Down Expand Up @@ -214,31 +215,6 @@ const DataAccessItem = ({ id, label, url, description }) => (
</AtbdSubSection>
);

export const VariableItem = ({ element, variable }) => (
<React.Fragment>
<h4 id={element.id} data-scroll='target'>
{element.label}
</h4>
<DetailsList>
<dt>Name</dt>
<dd>
<SafeReadEditor value={variable.name} whenEmpty={<EmptySection />} />
</dd>
<dt>Long name</dt>
<dd>
<SafeReadEditor
value={variable.long_name}
whenEmpty={<EmptySection />}
/>
</dd>
<dt>Unit</dt>
<dd>
<SafeReadEditor value={variable.unit} whenEmpty={<EmptySection />} />
</dd>
</DetailsList>
</React.Fragment>
);

const ContactItem = ({ id, label, contact, roles, affiliations }) => (
<AtbdSubSection itemScope itemType='https://schema.org/ContactPoint'>
<h3
Expand Down Expand Up @@ -303,6 +279,10 @@ export const EmptySection = ({ className }) => (
<p className={className}>No content available.</p>
);

EmptySection.propTypes = {
className: T.string
};

/**
* Renders each element of the given array (by calling their `render` function)
* and its children.
Expand Down Expand Up @@ -601,52 +581,40 @@ const htmlAtbdContentSections = [
{
label: 'Algorithm Input Variables',
id: 'input_variables',
render: ({ printMode, element, children, atbd }) => (
render: ({ printMode, element, document, atbd }) => (
<React.Fragment key={element.id}>
<HeadingWActions as='h3' id={element.id} data-scroll='target'>
<span>{element.label}</span>
{!printMode && (
<HeadingContextualActions id={element.id} atbd={atbd} />
)}
</HeadingWActions>
{React.Children.count(children) ? children : <EmptySection />}
{document.algorithm_input_variables?.length > 0 ? (
<VariablesTable variables={document.algorithm_input_variables} />
) : (
<EmptySection />
)}
</React.Fragment>
),
children: ({ document }) => {
const items = document.algorithm_input_variables || [];
return items.map((o, idx) => ({
label: `Variable #${idx + 1}`,
id: `input_variables_${idx + 1}`,
render: ({ element }) => (
<VariableItem key={element.id} element={element} variable={o} />
)
}));
}
)
},
{
label: 'Algorithm Output Variables',
id: 'output_variables',
render: ({ printMode, element, children, atbd }) => (
render: ({ printMode, element, document, atbd }) => (
<React.Fragment key={element.id}>
<HeadingWActions as='h3' id={element.id} data-scroll='target'>
<span>{element.label}</span>
{!printMode && (
<HeadingContextualActions id={element.id} atbd={atbd} />
)}
</HeadingWActions>
{React.Children.count(children) ? children : <EmptySection />}
{document.algorithm_output_variables?.length > 0 ? (
<VariablesTable variables={document.algorithm_output_variables} />
) : (
<EmptySection />
)}
</React.Fragment>
),
children: ({ document }) => {
const items = document.algorithm_output_variables || [];
return items.map((o, idx) => ({
label: `Variable #${idx + 1}`,
id: `output_variables_${idx + 1}`,
render: ({ element }) => (
<VariableItem key={element.id} element={element} variable={o} />
)
}));
}
)
}
]
},
Expand Down
18 changes: 9 additions & 9 deletions app/assets/scripts/types.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
import PropTypes from 'prop-types';

// Basic text child type
const textChildType = PropTypes.shape({
const TextChildType = PropTypes.shape({
text: PropTypes.string.isRequired
});

// Type for a paragraph element with children
const pChildType = PropTypes.shape({
const ParagraphType = PropTypes.shape({
type: PropTypes.string.isRequired,
children: PropTypes.arrayOf(textChildType).isRequired
children: PropTypes.arrayOf(TextChildType).isRequired
});

// Type for a variable node with children
const variableNodePropType = PropTypes.shape({
children: PropTypes.arrayOf(pChildType).isRequired
export const VariableNodeType = PropTypes.shape({
children: PropTypes.arrayOf(ParagraphType).isRequired
});

// Type for a variable item in the list
export const variableNodeType = PropTypes.shape({
name: variableNodePropType.isRequired,
long_name: variableNodePropType.isRequired,
unit: variableNodePropType.isRequired
export const VariablePropType = PropTypes.shape({
name: VariableNodeType.isRequired,
long_name: VariableNodeType.isRequired,
unit: VariableNodeType.isRequired
});

0 comments on commit 2128ca3

Please sign in to comment.