From 86dc6cd4e738a8f9183130b1331656d58f99b2ca Mon Sep 17 00:00:00 2001 From: Syauqizaidan Khairan Khalaf Date: Sat, 17 Feb 2024 12:47:06 +0700 Subject: [PATCH] feat: adding autodocs and adjustment in slider components --- packages/forms/src/lib/toggle/index.tsx | 1 - packages/forms/src/stories/Toggle.stories.tsx | 1 + packages/slider/src/lib/slider.tsx | 52 +++++++++++++----- .../slider/src/stories/slider.stories.tsx | 53 +++++++++++-------- 4 files changed, 70 insertions(+), 37 deletions(-) diff --git a/packages/forms/src/lib/toggle/index.tsx b/packages/forms/src/lib/toggle/index.tsx index 2b9e010..931c9c6 100644 --- a/packages/forms/src/lib/toggle/index.tsx +++ b/packages/forms/src/lib/toggle/index.tsx @@ -40,4 +40,3 @@ export const Toggle: React.FC = ({ color, iconOn, iconOff, classNam ); }; - diff --git a/packages/forms/src/stories/Toggle.stories.tsx b/packages/forms/src/stories/Toggle.stories.tsx index 92d9833..1d34b15 100644 --- a/packages/forms/src/stories/Toggle.stories.tsx +++ b/packages/forms/src/stories/Toggle.stories.tsx @@ -21,6 +21,7 @@ export const Basic: Story = { color: 'primary' } } + export const WithIcon: Story = { args: { color: 'primary', diff --git a/packages/slider/src/lib/slider.tsx b/packages/slider/src/lib/slider.tsx index 38054ea..ad4d513 100644 --- a/packages/slider/src/lib/slider.tsx +++ b/packages/slider/src/lib/slider.tsx @@ -1,42 +1,66 @@ import React, { ReactNode, useState } from "react"; +import { cn } from "@bootwind/common"; export interface SliderProps { min?: number; max?: number; + className?: string; leftContent?: ReactNode; rightContent?: ReactNode; } -const Slider: React.FC = ({ min = 1, max = 5, leftContent, rightContent }) => { +export const Slider: React.FC = ({ + min = 1, + max = 5, + className, + leftContent, + rightContent, +}) => { const [value, setValue] = useState((min + max) / 2); + const [hoveredValue, setHoveredValue] = useState(null); + const [tooltipPosition, setTooltipPosition] = useState({ x: 0, y: 0 }); + const [showTooltip, setShowTooltip] = useState(false); const handleSliderChange = (event: React.ChangeEvent) => { setValue(parseInt(event.target.value, 10)); }; + const handleSliderHover = (event: React.MouseEvent) => { + const newValue = parseInt(event.currentTarget.value, 10); + const rect = event.currentTarget.getBoundingClientRect(); + setTooltipPosition({ x: event.clientX, y: rect.top - 30 }); + setHoveredValue(newValue); + setShowTooltip(true); // Show tooltip when hovering + }; + + const handleSliderLeave = () => { + setHoveredValue(null); + setShowTooltip(false); // Hide tooltip when leaving + }; + return ( -
- {leftContent && ( - - )} +
+ {leftContent && } - {rightContent && ( - + {showTooltip && hoveredValue !== null && ( +
+ {hoveredValue} +
)} + {rightContent && }
); }; - -export default Slider; diff --git a/packages/slider/src/stories/slider.stories.tsx b/packages/slider/src/stories/slider.stories.tsx index c1266bf..1b0f20f 100644 --- a/packages/slider/src/stories/slider.stories.tsx +++ b/packages/slider/src/stories/slider.stories.tsx @@ -1,32 +1,41 @@ import React from 'react'; +import { Meta, StoryObj } from '@storybook/react'; -import Slider from '../lib/slider'; +import { Slider, SliderProps } from '../lib/slider'; export default { title: '❖ • Components/Slider', + tags: ['autodocs'], component: Slider, -}; + argTypes: { + leftContent: { control: 'text', description: 'Your left content, can be text, emoji or icon' }, + rightContent: { control: 'text', description: 'Your right content, can be text, emoji or icon' }, + className: { control: 'text', description: 'String' }, + }, +} as Meta; +type Story = StoryObj; -export const Basic = () => { - return ; +export const Basic: Story = { + args: { + min: 0, + max: 10, + } } -export const WithIcons = () => { - return ( - 🔊} - rightContent={🔉} - /> - ); -}; +export const WithEmojiOrIcon: Story = { + args: { + min: 0, + max: 100, + leftContent: (🔉), + rightContent: (🔊) + } +} -export const CustomRange = () => { - return ( - 1} - rightContent={75} - /> - ); -}; \ No newline at end of file +export const WithNumber: Story = { + args: { + min: 0, + max: 100, + leftContent: (0), + rightContent: (100) + } +}