@vigilio/preact-paginator
TypeScript icon, indicating that this package has built-in type declarations

0.0.10 • Public • Published

@Vigilio/preact-table

A simple preact Hooks library for table.

npm add tslib
npm add @preact/signals

Getting Started

useTable

API REFERENCE

function Component() {
    // You can use @vigilio/react-fetching
    const { refetch, isLoading } = useQuery(
        "/product",
        async function (url) {
            const data = new URLSearchParams();
            data.append("offset", String(pagination.pagination.offset));
            data.append("limit", String(pagination.pagination.limit));
            for (const [key, value] of Object.entries(sort.sort)) {
                data.append(key, value);
            }
            const response = await fetch(`http://localhost:4000${url}?${data}`);
            const result = await response.json();
            return result;
        },
        {
            onSuccess(data) {
                updateData(data.results, { total: data.count });
            },
        }
    );

    useEffect(() => {
        refetch();
    }, [pagination.page, pagination.value.limit, search.debounceTerm]);
    let component: null | JSX.Element = null;
    if (isLoading) {
        component = <div>cargando...</div>;
    }
    if (isFetching) {
        component = <div>fetching...</div>;
    }
    if (isSuccess) {
        component = <div>{JSON.stringify(data?.results)}</div>;
    }
    if (isError) {
        component = <div>{JSON.stringify(error, null, 3)}</div>;
    }

    return (
        <div class="text-white">
            <button
                class="bg-red-600 px-4 rounded-sm"
                type="button"
                onClick={() => refetch()}
            >
                Refetch
            </button>

            {component}
            {/* search */}
            <div>
                <label for="name">Name:</label>
                <input
                    type="text"
                    id="name"
                    placeholder="search by name"
                    value={search.value}
                    onChange={(e) =>
                        search.onSearchByName(e.currentTarget.value)
                    }
                />
            </div>
            {/* limit */}
            <div>
                <label for="name">limit:</label>
                <input
                    type="number"
                    id="name"
                    value={String(pagination.value.limit)}
                    placeholder={String(pagination.value.limit)}
                    onChange={(e) =>
                        pagination.onchangeLimit(Number(e.currentTarget.value))
                    }
                />
            </div>
            <div class="flex items-center gap-2">
                <button type="button" onClick={() => pagination.onBackPage()}>
                    {"<"}
                </button>
                <button onClick={() => pagination.onNextPage()} type="button">
                    {">"}
                </button>
            </div>

            <pre>
                {JSON.stringify({
                    isLoading,
                    isError,
                    data: data?.results,
                    error,
                    isFetching,
                    isSuccess,
                })}
            </pre>
        </div>
    );
}

Package Sidebar

Install

npm i @vigilio/preact-paginator

Weekly Downloads

8

Version

0.0.10

License

ISC

Unpacked Size

35 kB

Total Files

21

Last publish

Collaborators

  • vigilio