diff --git a/components/colorpicker/ColorPicker.stories.tsx b/components/colorpicker/ColorPicker.stories.tsx new file mode 100644 index 0000000..1130b3f --- /dev/null +++ b/components/colorpicker/ColorPicker.stories.tsx @@ -0,0 +1,21 @@ +import { ColorPicker } from './ColorPicker'; +import {Meta, StoryObj} from "@storybook/react"; + +const meta: Meta = { + title: "Components/ColorPicker", + component: ColorPicker, + parameters: { + layout: "centered", + }, + tags: ["autodocs"], +}; + +export default meta; + +type Story = StoryObj + +export const Default: Story = { + args: { + preColors: ["#4A90E2", "#FF6B6B", "#48D1CC", "#FFD700", "#9370DB", "#E17055", "#2ECC71", "#F39C12"], + }, +}; \ No newline at end of file diff --git a/components/colorpicker/ColorPicker.tsx b/components/colorpicker/ColorPicker.tsx new file mode 100644 index 0000000..e491b0a --- /dev/null +++ b/components/colorpicker/ColorPicker.tsx @@ -0,0 +1,53 @@ +import React, {useEffect, useState} from "react"; + + +interface ColorPickerProps { + initialColor?: string; + onChange: (color: string) => void; + preColors?: string[]; +} + +const ColorPicker: React.FC = ({ initialColor, preColors, onChange }) => { + const [color, setColor] = useState(initialColor || "#FFFFFF"); + + const handleColorChange = (e: React.ChangeEvent) => { + setColor(e.target.value); + onChange(e.target.value); + }; + + return ( +
+
+ {preColors?.map((preColor) => ( + setColor(preColor)} + /> + ))} +
+ +
+ + handleColorChange(e)} + /> +
+
+ ); +} + +const ColorBox: React.FC<{ color: string, onClick: () => void }> = ({color, onClick}) => { + return ( +
+ ); +} + +export { ColorPicker }; \ No newline at end of file diff --git a/hooks/useTableSort.ts b/hooks/useTableSort.ts new file mode 100644 index 0000000..95213fa --- /dev/null +++ b/hooks/useTableSort.ts @@ -0,0 +1,45 @@ +import {useMemo, useState} from "react"; + +type SortDirection = 'asc' | 'desc'; + +type SortConfig = { + key: keyof T; + direction: SortDirection; +} + +type customSort = (a: T, b: T, order: SortDirection) => number; + +interface useTableSortProps { + data: T[]; + initialSort: SortConfig; + customSort?: Partial>>; +} + +export function useTableSort>({ data, initialSort, customSort = {} }: useTableSortProps) { + const [sortConfig, setSortConfig] = useState>(initialSort); + + const sortedData = useMemo(() => { + const sortableItems = [...data]; + const { key, direction } = sortConfig; + + return sortableItems.sort((a, b) => { + if (customSort[key]) return customSort[key]!(a, b, direction); + + const aValue = a[key]; + const bValue = b[key]; + + if (aValue < bValue) return direction === 'asc' ? -1 : 1; + if (aValue > bValue) return direction === 'asc' ? 1 : -1; + return 0; + }); + }, [data, sortConfig, customSort]); + + const requestSort = (key: keyof T) => { + setSortConfig((prev) => ({ + key: key, + direction: prev.key === key && prev.direction === 'asc' ? 'desc' : 'asc', + })); + } + + return { sortedData, requestSort, sortConfig }; +} \ No newline at end of file