-
Notifications
You must be signed in to change notification settings - Fork 14
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
Fixing Duration Style for Desktop View #36
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,64 +1,75 @@ | ||
import React, { useState, useRef } from "react"; | ||
import { useTradeStore } from "@/stores/tradeStore"; | ||
import { useBottomSheetStore } from "@/stores/bottomSheetStore"; | ||
import { useDeviceDetection } from "@/hooks/useDeviceDetection"; | ||
import TradeParam from "@/components/TradeFields/TradeParam"; | ||
import { DurationController } from "./DurationController"; | ||
import { Popover } from "@/components/ui/popover"; | ||
import React, { useState, useRef } from "react" | ||
import { useTradeStore } from "@/stores/tradeStore" | ||
import { useBottomSheetStore } from "@/stores/bottomSheetStore" | ||
import { useDeviceDetection } from "@/hooks/useDeviceDetection" | ||
import TradeParam from "@/components/TradeFields/TradeParam" | ||
import { DurationController } from "./DurationController" | ||
import { Popover } from "@/components/ui/popover" | ||
import { DesktopTradeFieldCard } from "@/components/ui/desktop-trade-field-card" | ||
|
||
interface DurationFieldProps { | ||
className?: string; | ||
className?: string | ||
} | ||
|
||
export const DurationField: React.FC<DurationFieldProps> = ({ className }) => { | ||
const { duration } = useTradeStore(); | ||
const { setBottomSheet } = useBottomSheetStore(); | ||
const { isDesktop } = useDeviceDetection(); | ||
const [isOpen, setIsOpen] = useState(false); | ||
const popoverRef = useRef<{ isClosing: boolean }>({ isClosing: false }); | ||
const { duration } = useTradeStore() | ||
const { setBottomSheet } = useBottomSheetStore() | ||
const { isDesktop } = useDeviceDetection() | ||
const [isOpen, setIsOpen] = useState(false) | ||
const popoverRef = useRef<{ isClosing: boolean }>({ isClosing: false }) | ||
|
||
const handleClick = () => { | ||
if (isDesktop) { | ||
if (!popoverRef.current.isClosing) { | ||
setIsOpen(!isOpen); | ||
setIsOpen(!isOpen) | ||
} | ||
} else { | ||
setBottomSheet(true, "duration", "470px"); | ||
setBottomSheet(true, "duration", "470px") | ||
} | ||
}; | ||
} | ||
|
||
const handleClose = () => { | ||
popoverRef.current.isClosing = true; | ||
setIsOpen(false); | ||
popoverRef.current.isClosing = true | ||
setIsOpen(false) | ||
// Reset after a longer delay | ||
setTimeout(() => { | ||
popoverRef.current.isClosing = false; | ||
}, 300); // 300ms should be enough for the animation to complete | ||
}; | ||
popoverRef.current.isClosing = false | ||
}, 300) // 300ms should be enough for the animation to complete | ||
} | ||
|
||
return ( | ||
<div className="relative"> | ||
<TradeParam | ||
label="Duration" | ||
value={duration} | ||
onClick={handleClick} | ||
className={className} | ||
/> | ||
|
||
{isDesktop ? ( | ||
<DesktopTradeFieldCard isSelected={isOpen}> | ||
<TradeParam | ||
label="Duration" | ||
value={duration} | ||
onClick={handleClick} | ||
className={className} | ||
/> | ||
</DesktopTradeFieldCard> | ||
) : ( | ||
<TradeParam | ||
label="Duration" | ||
value={duration} | ||
onClick={handleClick} | ||
className={className} | ||
/> | ||
)} | ||
{isDesktop && isOpen && ( | ||
<Popover | ||
isOpen={isOpen} | ||
onClose={handleClose} | ||
style={{ | ||
position: 'absolute', | ||
right: '100%', | ||
top: '-8px', | ||
marginRight: '16px' | ||
position: "absolute", | ||
right: "100%", | ||
top: "-8px", | ||
marginRight: "16px", | ||
}} | ||
> | ||
<DurationController onClose={handleClose} /> | ||
</Popover> | ||
)} | ||
</div> | ||
); | ||
}; | ||
) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,7 +20,7 @@ describe('DesktopTradeFieldCard', () => { | |
); | ||
|
||
const card = container.firstChild as HTMLElement; | ||
expect(card).toHaveClass('bg-white', 'rounded-lg', 'p-2'); | ||
expect(card).toHaveClass('rounded-lg', 'p-2','bg-[rgba(246,247,248,1)]','border','border-transparent'); | ||
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. suggestion (testing): Test the isSelected style Add a test case to verify the styles when the Suggested implementation: it('merges custom className with default styles', () => {
// existing test code...
});
// New test to verify styles when isSelected is true it('applies isSelected style when isSelected prop is true', () => {
// Render the component with the isSelected prop set to true.
const { container } = render(<DesktopTradeFieldCard isSelected={true} />);
const card = container.firstChild as HTMLElement;
// Expect the card to have default styles plus the selected border style.
expect(card).toHaveClass('rounded-lg', 'p-2', 'bg-[rgba(246,247,248,1)]', 'border', 'border-blue-500');
}); Make sure that the component DesktopTradeFieldCard is imported in this test file if it isn't already. For example: <<<<<<< SEARCH
|
||
}); | ||
|
||
it('merges custom className with default styles', () => { | ||
|
@@ -31,6 +31,6 @@ describe('DesktopTradeFieldCard', () => { | |
); | ||
|
||
const card = container.firstChild as HTMLElement; | ||
expect(card).toHaveClass('bg-white p-2 rounded-lg'); | ||
expect(card).toHaveClass('bg-[rgba(246,247,248,1)] rounded-lg p-2 border border-transparent custom-class'); | ||
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. suggestion (testing): Test the error style It's important to also test the error styles. Add a test case where the Suggested implementation: it('applies error styles when error prop is true', () => {
const { container } = render(<DesktopTradeFieldCard error />);
const card = container.firstChild as HTMLElement;
expect(card).toHaveClass('bg-[rgba(246,247,248,1)]', 'rounded-lg', 'p-2', 'border', 'border-red-500');
});
}); Make sure that the DesktopTradeFieldCard component applies "border-red-500" (or the correct error class for your design) when the error prop is true. Adjust expected class names in the test if your design uses different classes. |
||
}); | ||
}); |
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.
issue (complexity): Consider extracting the conditional wrapper of the "TradeParam" component to reduce JSX duplication.
Consider extracting the conditional wrapper into its own component or helper function so that you only render the
<TradeParam>
once. This reduces the duplicated props and branching in your JSX.For example, you can create a simple wrapper component:
This approach retains all functionality while reducing duplication and simplifying the rendering logic.