From 873a609bb37fef8dcf3f62097593a4257bc9dad8 Mon Sep 17 00:00:00 2001 From: Achilleas Mitrotasios <58337217+mitrotasios@users.noreply.github.com> Date: Sat, 8 Oct 2022 17:42:23 +0100 Subject: [PATCH] fix: bug fixes Datepicker (#77) * fix: minor datepicker logic fixes * fix(Datepicker): calendar icon flex-none * fix(Datepicker): self-detecting modal * minor fixes * fix Modal exceeds window detection * fix: fix Modal self detecting exceeds window * refactor(ChartLegend): use withWindowResize * add semantic release notes generator * fix: put resize-observer-polyfill to dev dependencies * fix: put resize-observer-polyfill to dev dependencies --- package-lock.json | 15 ++-- package.json | 15 +++- .../chart-elements/common/ChartLegend.tsx | 31 ++++--- .../input-elements/Datepicker/Datepicker.tsx | 45 +++++++--- .../input-elements/Dropdown/Dropdown.tsx | 2 +- .../MultiSelectBox/MultiSelectBox.tsx | 2 +- .../input-elements/SelectBox/SelectBox.tsx | 2 +- .../layout-elements/Modal/Modal.tsx | 86 ++++++++++++++++--- src/lib/utils.tsx | 15 +++- src/tests/layout-elements/Modal.test.tsx | 2 +- tailwind.config.js | 2 +- 11 files changed, 168 insertions(+), 49 deletions(-) diff --git a/package-lock.json b/package-lock.json index e62fc4377..d4b42cb2c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,18 +1,17 @@ { "name": "@tremor/react", - "version": "0.1.0", + "version": "0.0.0-development", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@tremor/react", - "version": "0.1.0", + "version": "0.0.0-development", "license": "Apache 2.0", "dependencies": { "@tippyjs/react": "^4.2.6", "date-fns": "^2.28.0", - "recharts": "^2.1.13", - "resize-observer-polyfill": "^1.5.1" + "recharts": "^2.1.13" }, "devDependencies": { "@babel/core": "^7.17.9", @@ -23,6 +22,7 @@ "@rollup/plugin-commonjs": "^21.1.0", "@rollup/plugin-node-resolve": "^13.2.1", "@rollup/plugin-typescript": "^8.3.2", + "@semantic-release/release-notes-generator": "^10.0.3", "@storybook/addon-actions": "^6.5.0-alpha.64", "@storybook/addon-essentials": "^6.5.0-alpha.64", "@storybook/addon-interactions": "^6.5.0-alpha.64", @@ -43,6 +43,7 @@ "jest": "^27.5.1", "postcss": "^8.4.12", "react-dom": "^18.0.0", + "resize-observer-polyfill": "^1.5.1", "rollup": "^2.70.2", "rollup-plugin-dts": "^4.2.1", "rollup-plugin-peer-deps-external": "^2.2.4", @@ -26940,7 +26941,8 @@ "node_modules/resize-observer-polyfill": { "version": "1.5.1", "resolved": "https://registry.npmjs.org/resize-observer-polyfill/-/resize-observer-polyfill-1.5.1.tgz", - "integrity": "sha512-LwZrotdHOo12nQuZlHEmtuXdqGoOD0OhaxopaNFxWzInpEgaLWoVuAMbTzixuosCx2nEG58ngzW3vxdWoxIgdg==" + "integrity": "sha512-LwZrotdHOo12nQuZlHEmtuXdqGoOD0OhaxopaNFxWzInpEgaLWoVuAMbTzixuosCx2nEG58ngzW3vxdWoxIgdg==", + "dev": true }, "node_modules/resolve": { "version": "1.22.0", @@ -52014,7 +52016,8 @@ "resize-observer-polyfill": { "version": "1.5.1", "resolved": "https://registry.npmjs.org/resize-observer-polyfill/-/resize-observer-polyfill-1.5.1.tgz", - "integrity": "sha512-LwZrotdHOo12nQuZlHEmtuXdqGoOD0OhaxopaNFxWzInpEgaLWoVuAMbTzixuosCx2nEG58ngzW3vxdWoxIgdg==" + "integrity": "sha512-LwZrotdHOo12nQuZlHEmtuXdqGoOD0OhaxopaNFxWzInpEgaLWoVuAMbTzixuosCx2nEG58ngzW3vxdWoxIgdg==", + "dev": true }, "resolve": { "version": "1.22.0", diff --git a/package.json b/package.json index ef5e657fa..18a3e5e20 100644 --- a/package.json +++ b/package.json @@ -27,8 +27,7 @@ "dependencies": { "@tippyjs/react": "^4.2.6", "date-fns": "^2.28.0", - "recharts": "^2.1.13", - "resize-observer-polyfill": "^1.5.1" + "recharts": "^2.1.13" }, "devDependencies": { "@babel/core": "^7.17.9", @@ -39,6 +38,7 @@ "@rollup/plugin-commonjs": "^21.1.0", "@rollup/plugin-node-resolve": "^13.2.1", "@rollup/plugin-typescript": "^8.3.2", + "@semantic-release/release-notes-generator": "^10.0.3", "@storybook/addon-actions": "^6.5.0-alpha.64", "@storybook/addon-essentials": "^6.5.0-alpha.64", "@storybook/addon-interactions": "^6.5.0-alpha.64", @@ -59,6 +59,7 @@ "jest": "^27.5.1", "postcss": "^8.4.12", "react-dom": "^18.0.0", + "resize-observer-polyfill": "^1.5.1", "rollup": "^2.70.2", "rollup-plugin-dts": "^4.2.1", "rollup-plugin-peer-deps-external": "^2.2.4", @@ -82,6 +83,14 @@ ], "types": "dist/index.d.ts", "release": { - "branches": ["main"] + "branches": [ + "main" + ], + "plugins": [ + "@semantic-release/commit-analyzer", + "@semantic-release/release-notes-generator", + "@semantic-release/npm", + "@semantic-release/github" + ] } } diff --git a/src/components/chart-elements/common/ChartLegend.tsx b/src/components/chart-elements/common/ChartLegend.tsx index 0d4f1dd44..03569dd30 100644 --- a/src/components/chart-elements/common/ChartLegend.tsx +++ b/src/components/chart-elements/common/ChartLegend.tsx @@ -1,11 +1,14 @@ import React, { useEffect, useRef, useState } from 'react'; +import { themeColorRange, useWindowSize } from 'lib'; import { Color } from '../../../lib'; import Legend from 'components/text-elements/Legend'; -import { themeColorRange } from 'lib'; -const ChartLegend = ({ payload }: any, colors: Color[] = themeColorRange, - setLegendHeight: React.Dispatch>) => { +const ChartLegend = ( + { payload }: any, + colors: Color[] = themeColorRange, + setLegendHeight: React.Dispatch> +) => { const calculateHeight = (height: number|undefined) => ( height ? Number(height) + 20 // 20px extra padding @@ -13,20 +16,20 @@ const ChartLegend = ({ payload }: any, colors: Color[] = themeColorRange, ); const legendRef = useRef(null); - const [height, setHeight] = useState(calculateHeight(undefined)); - - // eslint-disable-next-line @typescript-eslint/no-unused-vars - // dummy windowsize listener to trigger useEffect on resize - const [_windowSize, setWindowSize] = useState(window.innerWidth); + const [currentheight, setCurrentHeight] = useState(calculateHeight(undefined)); + useEffect(() => { - const handleResize = () => setWindowSize(window.innerWidth); - window.addEventListener('resize', handleResize); + setCurrentHeight(calculateHeight(currentheight)); + // setLegendHeight setState action from Chart parent + setLegendHeight(calculateHeight(legendRef.current?.clientHeight)); + }, []); + useWindowSize(() => { + setCurrentHeight(calculateHeight(currentheight)); + // setLegendHeight setState action from Chart parent setLegendHeight(calculateHeight(legendRef.current?.clientHeight)); - setHeight(calculateHeight(height)); - - return () => window.removeEventListener('resize', handleResize); - }, [_windowSize]); + }); + return (
( + hasDefaultDateRange ? startOfDay( + minDate + ? max([defaultStartDate, minDate]) + : defaultStartDate + ) : null + ); + const getInitialEndDate = (): Date | null => ( + hasDefaultDateRange ? startOfDay( + maxDate + ? min([defaultEndDate, maxDate]) + : defaultEndDate + ) : null + ); + const datePickerRef = useRef(null); const dropdownRef = useRef(null); @@ -81,12 +98,20 @@ const Datepicker = ({ const [selectedRelativeFilterOption, setSelectedRelativeFilterOption] = useState(null); const [hoveredDay, setHoveredDay] = useState(null); - const [selectedStartDay, setSelectedStartDay] = useState( - hasDefaultDateRange ? startOfDay(defaultStartDate) : null); - const [selectedEndDay, setSelectedEndDay] = useState( - hasDefaultDateRange ? startOfDay(defaultEndDate) : null); - const [currentMonth, setCurrentMonth] = useState( - hasDefaultDateRange ? format(startOfDay(defaultEndDate)!, 'MMM-yyyy') : format(today, 'MMM-yyyy')); + const [selectedStartDay, setSelectedStartDay] = useState(getInitialStartDate()); + const [selectedEndDay, setSelectedEndDay] = useState(getInitialEndDate()); + + // Get month that will be displayed when opening the modal + const getInitialCurrentMonth = () => { + if (getInitialEndDate() !== null) { + return format(getInitialEndDate() as Date, 'MMM-yyyy'); + } else if (maxDate !== null) { + return format(maxDate, 'MMM-yyyy'); + } else { + return format(today, 'MMM-yyyy'); + } + }; + const [currentMonth, setCurrentMonth] = useState(getInitialCurrentMonth()); const firstDayCurrentMonth = parse(currentMonth, 'MMM-yyyy', new Date()); const lastDayCurrentMonth = endOfMonth(firstDayCurrentMonth); @@ -184,7 +209,7 @@ const Datepicker = ({ >
{ relativeFilterOptions.map((filterOption) => (