-
Notifications
You must be signed in to change notification settings - Fork 7
Enhanced sort feature to enable user to click on the column header th… #76
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
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,5 @@ | ||
| import Table from "./Table/Table" | ||
| import Table from "./Table/Table"; | ||
|
|
||
| export default function Dashboard({ featureFlags, headers }) { | ||
| return ( | ||
| <Table | ||
| featureFlags={featureFlags} | ||
| headers={headers} | ||
| /> | ||
| )} | ||
|
|
||
| return <Table featureFlags={featureFlags} headers={headers} />; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,39 +1,30 @@ | ||
| import {useState} from "react" | ||
| import TableHeader from "./TableHeader/TableHeader" | ||
| import TableRow from "./TableRow/TableRow" | ||
| import { compareValues } from "../../../Utils/helpers" | ||
| import { useState } from "react"; | ||
| import TableHeader from "./TableHeader/TableHeader"; | ||
| import TableRow from "./TableRow/TableRow"; | ||
| import { compareValues } from "../../../Utils/helpers"; | ||
|
|
||
| export default function Table({ featureFlags, headers }) { | ||
| const [rowData, setRowData] = useState(featureFlags); | ||
|
|
||
| const [rowData,setRowData] = useState(featureFlags) | ||
|
|
||
| const handleSorting = (type,onColumn) => { | ||
|
|
||
| let rows = rowData; | ||
| rows.sort(compareValues(onColumn,type)) | ||
| setRowData([...rows]) | ||
|
|
||
| } | ||
| const handleSorting = (type, onColumn) => { | ||
| let rows = rowData; | ||
| rows.sort(compareValues(onColumn, type)); | ||
| setRowData([...rows]); | ||
| }; | ||
|
|
||
| return ( | ||
|
|
||
| <table | ||
| className='shadow text-sm text-left table-fixed w-3/4 max-w-screen-md' | ||
| > | ||
| <TableHeader | ||
| headers={headers} | ||
| handleSorting={handleSorting} | ||
| /> | ||
| <table className="shadow text-sm text-left table-fixed w-3/4 max-w-screen-md"> | ||
| <TableHeader headers={headers} handleSorting={handleSorting} /> | ||
|
|
||
| <tbody> | ||
| {rowData.map(featureFlag => | ||
| {rowData.map((featureFlag) => ( | ||
| <TableRow | ||
| featureFlag={featureFlag} | ||
| headers={headers} | ||
| key={featureFlag.id} | ||
| /> | ||
| )} | ||
| /> | ||
| ))} | ||
| </tbody> | ||
| </table> | ||
| ) | ||
| } | ||
| ); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,17 @@ | ||
| import {TABLE} from "../Constant/constant" | ||
| import { TABLE } from "../Constant/constant"; | ||
|
|
||
| export function camelCaseToNormal(string) { | ||
| return string | ||
| // insert a space before all caps | ||
| .replace(/([A-Z])/g, ' $1') | ||
| // uppercase the first character | ||
| .replace(/^./, (str) => str.toUpperCase()) | ||
| return ( | ||
| string | ||
| // insert a space before all caps | ||
| .replace(/([A-Z])/g, " $1") | ||
| // uppercase the first character | ||
| .replace(/^./, (str) => str.toUpperCase()) | ||
| ); | ||
| } | ||
|
|
||
| /**** | ||
| * | ||
| * | ||
| * Currying | ||
| * https://javascript.info/currying-partials | ||
| */ | ||
|
|
@@ -19,19 +21,15 @@ export function compareValues(key, order = TABLE.ASCENDING) { | |
| return 0; | ||
| } | ||
|
|
||
| const varA = (typeof a[key] === 'string') | ||
| ? a[key].toUpperCase() : a[key]; | ||
| const varB = (typeof b[key] === 'string') | ||
| ? b[key].toUpperCase() : b[key]; | ||
| const varA = typeof a[key] === "string" ? a[key].toUpperCase() : a[key]; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we have better naming for variable varA and varB doesn't describe it's purpose
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi @DeRaowl this is a comparator function. What do you think would be better? Since those are keys can i name them key1 and key2? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok instead you can just add small comment saying what these variables are |
||
| const varB = typeof b[key] === "string" ? b[key].toUpperCase() : b[key]; | ||
|
|
||
| let comparison = 0; | ||
| if (varA > varB) { | ||
| comparison = 1; | ||
| } else if (varA < varB) { | ||
| comparison = -1; | ||
| } | ||
| return ( | ||
| (order === TABLE.DESCENDING) ? (comparison * -1) : comparison | ||
| ); | ||
| return order === TABLE.DESCENDING ? comparison * -1 : comparison; | ||
| }; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,45 +1,19 @@ | ||
| import { fireEvent, render, screen } from "@testing-library/react"; | ||
| import {render, screen } from "@testing-library/react"; | ||
| import SortCursor from "../Components/Dashboard/Table/TableHeader/SortCursor"; | ||
| import { TABLE } from "../Constant/constant"; | ||
|
|
||
| describe("SortCursor Component", () => { | ||
| it("Should render the SortCursor Component, which includes Ascending cursor", () => { | ||
| render(<SortCursor />); | ||
|
|
||
| it("Should render the SortCursor Component with Ascending cursor", () => { | ||
| render(<SortCursor />) | ||
| const cursor = screen.getByTitle(TABLE.ASCENDING); | ||
| expect(cursor).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| const cursor = screen.getByTitle(TABLE.ASCENDING) | ||
| expect(cursor).toBeInTheDocument(); | ||
| }) | ||
|
|
||
| it("Should render the SortCursor Component with Descending cursor", () => { | ||
| render(<SortCursor />) | ||
|
|
||
| const cursor = screen.getByTitle(TABLE.DESCENDING) | ||
| expect(cursor).toBeInTheDocument(); | ||
|
|
||
| }) | ||
|
|
||
| it("Should fire event on click ascending", async () => { | ||
| //const mockFn = sinon.spy(); | ||
|
|
||
| const mockFn = jest.fn(); | ||
| render(<SortCursor handleSorting={mockFn}/>) | ||
|
|
||
| const cursor = screen.getByTitle(TABLE.ASCENDING) | ||
| await fireEvent.click(cursor); | ||
|
|
||
|
|
||
| }) | ||
|
|
||
| it("Should fire event on click descending", async () => { | ||
| const mockFn = jest.fn(); | ||
| render(<SortCursor handleSorting={mockFn}/>) | ||
|
|
||
| const cursor = screen.getByTitle(TABLE.DESCENDING) | ||
| await fireEvent.click(cursor); | ||
| expect(mockFn.mock.calls.length).toEqual(1); | ||
|
|
||
| }) | ||
|
|
||
| }) | ||
| it("Should render the SortCursor Component, which includes Descending cursor", () => { | ||
| render(<SortCursor />); | ||
|
|
||
| const cursor = screen.getByTitle(TABLE.DESCENDING); | ||
| expect(cursor).toBeInTheDocument(); | ||
| }); | ||
| }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we just make use of arrow function here? No need to write return explicitly
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same is followed everywhere. This is not a change from this PR. It shows it changed because i ran perttier. Let me know if you still need me to change it because all components follow the same
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, we can change this to arrow function