Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented a more generic value-label list #205

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@code4ro/taskforce-fe-components",
"version": "2.0.0",
"version": "2.1.0",
"private": false,
"sideEffects": false,
"dependencies": {
Expand Down
47 changes: 47 additions & 0 deletions src/components/label-value-list/label-value-list.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import React from "react";
import PropTypes from "prop-types";
import "./label-value-list.scss";

function renderValue(value) {
if (typeof value === "boolean") {
return value ? "Da" : "Nu";
}

if (Array.isArray(value)) {
return value.join(", ");
}

return value;
}

export const LabelValueList = ({ data }) => {
let rows = data;

if (!Array.isArray(data)) {
rows = Object.values(data);
}

return (
<div className="container">
<div className="label-value-list">
<table className="table">
<tbody>
{ rows.map((row, index) => (
<tr key={index}>
<th>{row.label}</th>
<td>{renderValue(row.value)}</td>
</tr>
))}
</tbody>
</table>
</div>
</div>
);
};

LabelValueList.propTypes = {
data: PropTypes.oneOfType([
PropTypes.object,
PropTypes.array
]).isRequired
};
7 changes: 7 additions & 0 deletions src/components/label-value-list/label-value-list.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.label-value-list {
padding: 2em 0;

.table th, .table td {
border: none;
}
}
47 changes: 47 additions & 0 deletions src/components/label-value-list/label-value-list.stories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import React from "react";
import { LabelValueList } from "./label-value-list";

export default { title: "Label-value List" };

const objectData = {
age: {
label: "Vârsta",
value: 23
},
gender: {
label: "Sexul",
value: "feminin"
},
name: { label: "Nume", value: "name" },
smoking: { label: "Fumator/fumatoare", value: true },
comorbidities: {
label: "Alte afectiuni",
value: ["diabet", "hipertensiune"]
},
inIsolation: { label: "In izolare", value: true },
othersInHousehold: { label: "Alte persoane in casă", value: true }
};

const arrayData = [
{
label: "Vârsta",
value: 23
},
{
label: "Alte afectiuni",
value: ["diabet", "hipertensiune"]
},
{
label: "Fumator/fumatoare",
value: false
}
]

export const usingObject = () => (
<LabelValueList data={objectData} />
);

export const usingArray = () => (
<LabelValueList data={arrayData} />
);

46 changes: 12 additions & 34 deletions src/components/personal-data-table/personal-data-table.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,19 @@
import React from "react";
import React, { useEffect } from "react";
import PropTypes from "prop-types";
import "./personal-data-table.scss";
import { LabelValueList } from "../label-value-list/label-value-list";

export const PersonalDataTable = ({ personalData }) => {
const dataClone = Object.assign(personalData);
delete dataClone.name;

useEffect(
function warnOnMount() {
console.warn("PersonalDataTable is deprecated. Please use the more generic LabelValueList component.")
}, []
)

return (
<div className="container">
<div className="personal-data">
<table className="table personal-data-table">
<tbody>
<tr>
<th>{personalData.age.label}</th>
<td>{personalData.age.value}</td>
</tr>
<tr>
<th>{personalData.gender.label}</th>
<td>{personalData.gender.value}</td>
</tr>
<tr>
<th>{personalData.smoking.label}</th>
<td>{personalData.smoking.value ? "Da" : "Nu"}</td>
</tr>
<tr>
<th>{personalData.comorbidities.label}</th>
<td>{personalData.comorbidities.value.join(", ")}</td>
</tr>
<tr>
<th>{personalData.inIsolation.label}</th>
<td>{personalData.inIsolation.value ? "Da" : "Nu"}</td>
</tr>
<tr>
<th>{personalData.othersInHousehold.label}</th>
<td>{personalData.othersInHousehold ? "Da" : "Nu"}</td>
</tr>
</tbody>
</table>
</div>
</div>
<LabelValueList data={personalData} />
);
};

Expand Down
8 changes: 0 additions & 8 deletions src/components/personal-data-table/personal-data-table.scss

This file was deleted.

1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,4 @@ export { DataTableRow } from "./components/data-table-row/data-table-row";
export { FilterModal } from "./components/filter-modal/filter-modal";
export { DropdownSearch } from "./components/dropdown-search/dropdown-search";
export { HeaderFilter } from "./components/header-filter/header-filter";
export { LabelValueList } from "./components/label-value-list/label-value-list";