Skip to content

Commit

Permalink
Data Elements Table rows (#1776)
Browse files Browse the repository at this point in the history
  • Loading branch information
jlee-enquizit committed Sep 23, 2024
1 parent d8edbbe commit 712e061
Show file tree
Hide file tree
Showing 6 changed files with 392 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,17 @@ import { useDataElementsContext } from '../context/DataElementsContext';
import { useDedupeContext } from '../context/DedupeContext';
import styles from './data-elements-configuration.module.scss';
import { Input } from 'components/FormInputs/Input';
import { DataElementsTable } from './DataElementsTable';
import { useForm, FormProvider } from 'react-hook-form';

const DataElementsConfiguration = () => {
const { dataElements } = useDataElementsContext();
const { setMode } = useDedupeContext();
console.log(dataElements);
const form = useForm({
mode: 'onBlur',
defaultValues: { dataElements }
});

return (
<div className={styles.dataElements}>
<div className={styles.header}>
Expand All @@ -17,27 +23,31 @@ const DataElementsConfiguration = () => {
</div>
<div className={styles.body}>
<div className={styles.sections}>
<div className={styles.section}>
<div className={styles.sectionHeader}>
<h4>Global settings</h4>
</div>
<div className={styles.sectionBody}>
<Input
type="text"
label="Belongingness ratio"
defaultValue="0.5"
orientation="horizontal"
/>
</div>
</div>
<div className={styles.section}>
<div className={styles.table}>
<FormProvider {...form}>
<div className={styles.section}>
<div className={styles.sectionHeader}>
<h4>Data elements</h4>
<h4>Global settings</h4>
</div>
<div className={styles.sectionBody}>
<Input
type="text"
label="Belongingness ratio"
defaultValue="0.5"
orientation="horizontal"
/>
</div>
</div>
<div className={styles.section}>
<div className={styles.table}>
<div className={styles.sectionHeader}>
<h4>Data elements</h4>
</div>
<div className={styles.sectionBody}>
<DataElementsTable />
</div>
</div>
<div className={styles.sectionBody}>Table data</div>
</div>
</div>
</FormProvider>
</div>
<div className={styles.sidebar}>
<h3>How to </h3>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
import React, { useEffect, useState } from 'react';
import { useFormContext, Controller, useWatch } from 'react-hook-form';
import { DataElement } from '../const/init';
import { DataTable, Column } from 'design-system/table';
import { Checkbox } from '@trussworks/react-uswds';
import { Input } from 'components/FormInputs/Input';
import { useDataElementsContext } from '../context/DataElementsContext';

const columns = (
control: any,
handleRowCheckboxChange: (index: number, checked: boolean) => void,
checkedState: boolean[],
watchedDataElements: DataElement[]
): Column<DataElement>[] => [
{
id: 'active',
name: 'Active',
render: (dataElement, index) => (
<Controller
control={control}
name={`dataElements.${index}.active`}
render={({ field: { value, onChange, name } }) => (
<Checkbox
name={name}
label=""
id={`checkbox-${index}`}
checked={value || false}
onChange={(e) => {
handleRowCheckboxChange(index, e.target.checked);
onChange(e.target.checked);
}}
/>
)}
/>
)
},
{
id: 'name',
name: 'Name',
render: (dataElement) => dataElement.name
},
{
id: 'm',
name: 'M',
render: (dataElement, index) => (
<Controller
control={control}
name={`dataElements.${index}.m`}
render={({ field: { value, onChange, onBlur, name } }) => (
<Input
type="number"
defaultValue={value}
value={value}
onChange={onChange}
onBlur={onBlur}
name={name}
disabled={!checkedState[index]}
/>
)}
/>
)
},
{
id: 'u',
name: 'U',
render: (dataElement, index) => (
<Controller
control={control}
name={`dataElements.${index}.u`}
render={({ field: { value, onChange, onBlur, name } }) => (
<Input
type="number"
defaultValue={value}
value={value}
onChange={onChange}
onBlur={onBlur}
name={name}
disabled={!checkedState[index]}
/>
)}
/>
)
},
{
id: 'oddsRatio',
name: 'Odds ratio',
render: (dataElement, index) => {
const m = watchedDataElements[index]?.m;
const u = watchedDataElements[index]?.u;
const oddsRatio = m && u && u !== 0 ? (m / u).toFixed(2) : 'No Data';
return oddsRatio;
}
},
{
id: 'logOdds',
name: 'Log odds',
render: (dataElement, index) => {
const m = watchedDataElements[index]?.m;
const u = watchedDataElements[index]?.u;
const logOdds = m && u && u !== 0 && m !== 0 ? Math.log(m / u).toFixed(2) : 'No Data';
return logOdds;
}
},
{
id: 'threshold',
name: 'Threshold',
render: (dataElement, index) => (
<Controller
control={control}
name={`dataElements.${index}.threshold`}
render={({ field: { value, onChange, onBlur, name } }) => (
<Input
type="number"
defaultValue={value}
value={value}
onChange={onChange}
onBlur={onBlur}
name={name}
disabled={!checkedState[index]}
/>
)}
/>
)
}
];

const DataElementsTable = () => {
const { control, setValue } = useFormContext<{ dataElements: DataElement[] }>();
const { dataElements } = useDataElementsContext();
const [checkedState, setCheckedState] = useState<boolean[]>([]);

const watchedDataElements = useWatch({
control,
name: 'dataElements',
defaultValue: dataElements
});

useEffect(() => {
setValue('dataElements', dataElements);
setCheckedState(dataElements.map((de) => de.active));
}, [dataElements, setValue]);

const handleRowCheckboxChange = (index: number, checked: boolean) => {
const updatedCheckedState = [...checkedState];
updatedCheckedState[index] = checked;
setCheckedState(updatedCheckedState);
};

return (
<DataTable<DataElement>
id="dataElementsTable"
columns={columns(control, handleRowCheckboxChange, checkedState, watchedDataElements)}
data={dataElements}
/>
);
};

export { DataElementsTable };
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
@use 'styles/colors';

.table {
width: 100%;
tbody tr:nth-child(odd) {
background-color: colors.$base-lightest;
}
th {
label::before {
margin-top: -0.5rem;
content: '';
display: inline-block;
width: 1rem;
height: 1rem;
margin-right: 0.5rem;
}
}
td {
height: 3rem;
label::before {
margin-top: -0.75rem;
content: '';
display: inline-block;
width: 1rem;
height: 1rem;
margin-right: 0.5rem;
}
}
}
106 changes: 87 additions & 19 deletions apps/modernization-ui/src/apps/dedup-config/const/init.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,87 @@
export const DataElements = {
"lastName": "",
"secondLastName": "",
"firstName": "",
"middleName": "",
"secondMiddleName": "",
"suffix": "",
"currentSex": "",
"dateOfBirth": "",
"ssn": "",
"idType": "",
"idAssigningAuthority": "",
"idValue": "",
"streetAddress1": "",
"city": "",
"state": "",
"zipCode": "",
"telephone": ""
};
export type DataElement = {
name: string;
active: boolean;
m: number;
u: number;
threshold: number;
};

export const DataElements: DataElement[] = [
{ name: "lastName", active: true, m: 0.5, u: 0.5, threshold: 0.5 },
{ name: "secondLastName", active: true, m: 0.5, u: 0.5, threshold: 0.5 },
{ name: "firstName", active: true, m: 0.5, u: 0.5, threshold: 0.5 },
{ name: "middleName", active: true, m: 0.5, u: 0.5, threshold: 0.5 },
{ name: "secondMiddleName", active: true, m: 0.5, u: 0.5, threshold: 0.5 },
{ name: "suffix", active: true, m: 0.5, u: 0.5, threshold: 0.5 },
{ name: "currentSex", active: true, m: 0.5, u: 0.5, threshold: 0.5 },
{
name: "dateOfBirth",
active: true,
m: 0.5,
u: 0.5,
threshold: 0.5,
},
{
name: "ssn",
active: true,
m: 0.5,
u: 0.5,
threshold: 0.5,
},
{
name: "idType",
active: true,
m: 0.5,
u: 0.25,
threshold: 0.5,
},
{
name: "idAssigningAuthority",
active: true,
m: 0.5,
u: 0.25,
threshold: 0.5,
},
{
name: "idValue",
active: true,
m: 0.5,
u: 0.25,
threshold: 0.5,
},
{
name: "streetAddress1",
active: true,
m: 0.5,
u: 0.25,
threshold: 0.5,
},
{
name: "city",
active: true,
m: 0.5,
u: 0.25,
threshold: 0.5,
},
{
name: "state",
active: true,
m: 0.5,
u: 0.25,
threshold: 0.5,
},
{
name: "zip",
active: true,
m: 0.5,
u: 0.25,
threshold: 0.5,
},
{
name: "telephone",
active: true,
m: 0.5,
u: 0.1,
threshold: 0.5,
}
];
Loading

0 comments on commit 712e061

Please sign in to comment.