diff --git a/src/data-workspace/custom-form/custom-form-indicator-cell.jsx b/src/data-workspace/custom-form/custom-form-indicator-cell.jsx new file mode 100644 index 000000000..4d7f7e14f --- /dev/null +++ b/src/data-workspace/custom-form/custom-form-indicator-cell.jsx @@ -0,0 +1,29 @@ +import PropTypes from 'prop-types' +import React from 'react' +import { IndicatorTableCell } from '../indicators-table-body/indicator-table-cell.jsx' + +export const CustomFormIndicatorCell = ({ indicatorId, metadata }) => { + const { + denominator, + numerator, + indicatorType: { factor }, + decimals, + } = metadata.indicators[indicatorId] + + return ( + + ) +} +CustomFormIndicatorCell.propTypes = { + indicatorId: PropTypes.string.isRequired, + metadata: PropTypes.object.isRequired, +} + +export const replaceIndicatorCell = (indicatorId, metadata) => ( + +) diff --git a/src/data-workspace/custom-form/custom-form-total-cell.jsx b/src/data-workspace/custom-form/custom-form-total-cell.jsx index 2e0270da2..7b7d288dd 100644 --- a/src/data-workspace/custom-form/custom-form-total-cell.jsx +++ b/src/data-workspace/custom-form/custom-form-total-cell.jsx @@ -1,5 +1,5 @@ import PropTypes from 'prop-types' -import React, { useState, useEffect } from 'react' +import React, { useMemo, useState, useEffect } from 'react' import { useValueStore } from '../../shared/index.js' import { TotalCell } from '../category-combo-table-body/total-cells.jsx' @@ -18,7 +18,10 @@ const computeTotalValues = (dataElementValues) => { export const CustomFormTotalCell = ({ dataElementId }) => { const dataValues = useValueStore((state) => state.getDataValues()) - const dataElementValues = dataValues?.[dataElementId] || {} + const dataElementValues = useMemo( + () => dataValues?.[dataElementId] || {}, + [dataValues, dataElementId] + ) const [total, setTotal] = useState(() => computeTotalValues(dataElementValues) diff --git a/src/data-workspace/custom-form/parse-html-to-react.test.js b/src/data-workspace/custom-form/parse-html-to-react.test.js index bc08ff2f5..ba625c790 100644 --- a/src/data-workspace/custom-form/parse-html-to-react.test.js +++ b/src/data-workspace/custom-form/parse-html-to-react.test.js @@ -1,5 +1,6 @@ import React from 'react' import { DataEntryField } from '../data-entry-cell/index.js' +import { CustomFormIndicatorCell } from './custom-form-indicator-cell.jsx' import { CustomFormTotalCell } from './custom-form-total-cell.jsx' import { parseHtmlToReact } from './parse-html-to-react.jsx' @@ -24,6 +25,50 @@ describe('parseHtmlToReact', () => { ) }) + it('replaces indicator cells inside td elements', () => { + const htmlCode = + "" + const metadata = { + indicators: { + RANDOMuid01: { + indicatorType: { factor: 1 }, + numerator: 'numerator', + denominator: 'denominator', + decimals: 2, + }, + }, + } + const result = parseHtmlToReact(htmlCode, metadata) + expect(result).toEqual( + + ) + }) + it('replaces indicator cells outside of elements', () => { + const htmlCode = + "
" + const metadata = { + indicators: { + RANDOMuid01: { + indicatorType: { factor: 1 }, + numerator: 'numerator', + denominator: 'denominator', + decimals: 2, + }, + }, + } + const result = parseHtmlToReact(htmlCode, metadata) + expect(result).toEqual( +
+ +
+ ) + }) it('uses disabled entry field when input has disabled attribute', () => { const htmlCode = diff --git a/src/data-workspace/custom-form/replace-input-node.jsx b/src/data-workspace/custom-form/replace-input-node.jsx index 1010ebc53..f82771829 100644 --- a/src/data-workspace/custom-form/replace-input-node.jsx +++ b/src/data-workspace/custom-form/replace-input-node.jsx @@ -1,6 +1,7 @@ import React from 'react' import { selectors } from '../../shared/index.js' import { DataEntryField } from '../data-entry-cell/index.js' +import { replaceIndicatorCell } from './custom-form-indicator-cell.jsx' import { replaceTotalCell } from './custom-form-total-cell.jsx' const INPUT_TYPES = { @@ -34,6 +35,10 @@ export const replaceInputNode = (domNode, metadata) => { return replaceTotalCell(domNode.attribs.dataelementid) } + if (inputType === INPUT_TYPES.INDICATOR) { + return replaceIndicatorCell(domNode.attribs.indicatorid, metadata) + } + if (inputType !== INPUT_TYPES.ENTRYFIELD) { return undefined } diff --git a/src/data-workspace/custom-form/replace-td-node.jsx b/src/data-workspace/custom-form/replace-td-node.jsx index 8a188a971..f361d19c0 100644 --- a/src/data-workspace/custom-form/replace-td-node.jsx +++ b/src/data-workspace/custom-form/replace-td-node.jsx @@ -1,26 +1,8 @@ import { attributesToProps } from 'html-react-parser' import React from 'react' -import { IndicatorTableCell } from '../indicators-table-body/indicator-table-cell.jsx' +import { replaceIndicatorCell } from './custom-form-indicator-cell.jsx' import { replaceTotalCell } from './custom-form-total-cell.jsx' -const replaceIndicatorCell = (indicatorId, metadata) => { - const { - denominator, - numerator, - indicatorType: { factor }, - decimals, - } = metadata.indicators[indicatorId] - - return ( - - ) -} - const replaceTextCell = (domNode) => { const cleanedText = domNode.children[0].nodeValue.trim() const props = attributesToProps(domNode.attribs)