Skip to content

Commit

Permalink
Merge pull request #22 from bleu-fi/add-intial-filter-on-table
Browse files Browse the repository at this point in the history
Add initial filters on swr table if param is passed, month short on datetime and handle time picker for AM/PM
  • Loading branch information
luizakp authored Feb 19, 2024
2 parents d73c5a0 + 39b7a78 commit 998e9d0
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
21 changes: 20 additions & 1 deletion src/components/DataTable/DataTableToolbar.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Cross2Icon } from "@radix-ui/react-icons";
import React from "react";
import React, { useEffect, useRef } from "react";
import { Button, Input } from "@/components/ui";

import { DataTableFacetedFilter } from "./DataTableFacetedFilter";
Expand All @@ -17,6 +17,25 @@ export function DataTableToolbar({

const isFiltered = table.getState().columnFilters.length > 0;

const initialFilterSet = useRef(false);

useEffect(() => {
if (filters && !initialFilterSet.current) {
filters.map((filter) => {
const column = table.getColumn(filter.value);
if (column) {
column.setFilterValue(
filter.options
.filter((option) => option.defaultSelected)
.map((option) => option.value)
);
}
return null;
});
initialFilterSet.current = true;
}
}, [filters, table]);

return (
<div className="flex items-center justify-between">
<div className="flex flex-1 items-center space-x-2">
Expand Down
16 changes: 13 additions & 3 deletions src/components/ui/Calendar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,21 @@ function Calendar({

const handleTimeChange: React.ChangeEventHandler<HTMLInputElement> = (e) => {
const time = e.target.value;
let [hours, minutes] = time.split(":").map((str) => parseInt(str, 10));

// If the input is not a valid time, set the time to 00:00
// This will handle 00 being inputed on 12h format
hours = !Number.isFinite(hours) || hours < 0 || hours > 23 ? 0 : hours;
minutes =
!Number.isFinite(minutes) || minutes < 0 || minutes > 59 ? 0 : minutes;

const validatedTime = `${hours.toString().padStart(2, "0")}:${minutes.toString().padStart(2, "0")}`;

if (!selected) {
setTimeValue(time);
setTimeValue(validatedTime);
return;
}
const [hours, minutes] = time.split(":").map((str) => parseInt(str, 10));

const selectedDate = new Date(selected);
const newSelectedDate = new Date(
selectedDate.getFullYear(),
Expand All @@ -68,7 +78,7 @@ function Calendar({
minutes
);
setSelected(newSelectedDate);
setTimeValue(time);
setTimeValue(validatedTime);
};

const handleDaySelect = (date: Date | undefined) => {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/formatDate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export function formatDate(date) {
export function formatDateTime(date: Date | string | number) {
return Intl.DateTimeFormat("en", {
year: "numeric",
month: "long",
month: "short",
day: "numeric",
hour: "numeric",
minute: "numeric",
Expand Down

0 comments on commit 998e9d0

Please sign in to comment.