@bouko/react 2.8.7 → 2.8.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,7 @@
1
1
  import { ReactNode } from "react";
2
2
  import type { Component } from "../../core/types";
3
3
  export type ButtonProps = Component & {
4
- variant?: "primary" | "outline" | "ghost" | "secondary";
4
+ variant?: "primary" | "outline" | "ghost" | "secondary" | "error";
5
5
  size?: "xs" | "sm" | "md" | "lg";
6
6
  icon?: ReactNode;
7
7
  action?: () => void;
@@ -25,10 +25,11 @@ const styles = tv({
25
25
  primary: "bg-primary border-primary-dark",
26
26
  outline: "bg-transparent border-accent hover:border-accent-dark text-primary",
27
27
  ghost: "bg-transparent border-transparent text-accent hover:brightness-110",
28
- secondary: "bg-background-dark dark:bg-background-light border-border-dark dark:border-border-light text-background-darker dark:text-primary"
28
+ secondary: "bg-background-dark dark:bg-background-light border-border-dark dark:border-border-light text-background-darker dark:text-primary",
29
+ error: "bg-error border-error-dark"
29
30
  },
30
31
  size: {
31
- xs: "px-3 py-1 text-xs",
32
+ xs: "gap-1 px-2 py-1 text-xs font-medium",
32
33
  circle: "px-2 py-2 text-xs rounded-full",
33
34
  sm: "px-3 py-2 text-xs sm:text-sm",
34
35
  md: "px-4 py-2",
@@ -17,3 +17,4 @@ export * from "./animate";
17
17
  export * from "./list";
18
18
  export * from "./carousel";
19
19
  export * from "./media-row";
20
+ export * from "./scroll-box";
@@ -17,3 +17,4 @@ export * from "./animate";
17
17
  export * from "./list";
18
18
  export * from "./carousel";
19
19
  export * from "./media-row";
20
+ export * from "./scroll-box";
@@ -0,0 +1,14 @@
1
+ import { JSX, Dispatch, SetStateAction } from "react";
2
+ type Props<T> = {
3
+ style?: string;
4
+ items: T[];
5
+ element: ({ data, hovered, setHovered, index }: {
6
+ data: T;
7
+ hovered: boolean;
8
+ setHovered: Dispatch<SetStateAction<number | null>>;
9
+ index: number;
10
+ key: number;
11
+ }) => JSX.Element;
12
+ };
13
+ export declare function ScrollBox<T>({ style, items, element }: Props<T>): import("react/jsx-runtime").JSX.Element;
14
+ export {};
@@ -0,0 +1,14 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ import { useState } from "react";
3
+ import { cn } from "@bouko/style";
4
+ import { ColumnBox } from "./layout/flex";
5
+ export function ScrollBox({ style, items, element }) {
6
+ const [hoveredIndex, setHoveredIndex] = useState(null);
7
+ return (_jsx(ColumnBox, { style: cn("grow gap-1 w-full overflow-y-auto", style), children: items.map((x, i) => (element({
8
+ data: x,
9
+ hovered: hoveredIndex === i,
10
+ setHovered: setHoveredIndex,
11
+ index: i,
12
+ key: i
13
+ }))) }));
14
+ }
@@ -1,4 +1,18 @@
1
- export declare function usePagination(defaultPage?: number): {
1
+ type Options<T> = {
2
+ fetcher: (query: PaginationQuery) => Promise<T[]>;
3
+ pageSize: number;
4
+ };
5
+ type PaginationQuery = {
6
+ page: number;
7
+ pageSize: number;
8
+ refresh?: boolean;
9
+ };
10
+ export declare function usePagination<T>({ fetcher, pageSize }: Options<T>): {
11
+ items: T[];
12
+ loading: boolean;
2
13
  page: number;
3
- setPage: (nextPage: number) => void;
14
+ prev: () => void;
15
+ next: () => void;
16
+ refresh: () => Promise<void>;
4
17
  };
18
+ export {};
@@ -1,11 +1,30 @@
1
+ import { useState, useCallback, useEffect } from "react";
1
2
  import { useSearchParams } from "react-router-dom";
2
- export function usePagination(defaultPage = 1) {
3
+ export function usePagination({ fetcher, pageSize }) {
3
4
  const [params, setParams] = useSearchParams();
5
+ const [items, setItems] = useState([]);
6
+ const [loading, setLoading] = useState(false);
4
7
  const raw = params.get("page");
5
- const page = Math.max(1, Number(raw) || defaultPage);
8
+ const page = Math.max(1, Number(raw) || 1);
6
9
  const setPage = (nextPage) => {
7
10
  params.set("page", String(Math.max(1, nextPage)));
8
11
  setParams(params);
9
12
  };
10
- return { page, setPage };
13
+ const fetchPage = useCallback(async (refresh = false) => {
14
+ setLoading(true);
15
+ fetcher({ page, pageSize, refresh })
16
+ .then(setItems)
17
+ .finally(() => setLoading(false));
18
+ }, [page, pageSize]);
19
+ useEffect(() => {
20
+ fetchPage(false);
21
+ }, [fetchPage]);
22
+ return {
23
+ items,
24
+ loading,
25
+ page,
26
+ prev: () => page > 1 ? setPage(page - 1) : undefined,
27
+ next: () => !(page > 1 && items.length === 0) ? setPage(page + 1) : undefined,
28
+ refresh: () => fetchPage(true)
29
+ };
11
30
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
 
3
3
  "name": "@bouko/react",
4
- "version": "2.8.7",
4
+ "version": "2.8.9",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
7
7
  "license": "MIT",