From b0de6033b54dc4d8d16b8635ba7fce3872e063fd Mon Sep 17 00:00:00 2001 From: unknown <1533540012@qq.com> Date: Wed, 18 Oct 2023 18:39:49 +0800 Subject: [PATCH] emmm --- demo/app.jsx | 222 ++++++++++++++++++++++++-- demo/index.html => index.html | 2 +- src/index.js | 30 ++-- demo/vite.config.js => vite.config.js | 0 4 files changed, 222 insertions(+), 32 deletions(-) rename demo/index.html => index.html (81%) rename demo/vite.config.js => vite.config.js (100%) diff --git a/demo/app.jsx b/demo/app.jsx index 33bc02d..0bb69c7 100644 --- a/demo/app.jsx +++ b/demo/app.jsx @@ -1,16 +1,208 @@ -import { h, render, useState, Fragment, useEffect } from './fre' - - -const App = () => { - useEffect(()=>{ - console.log(123) - },[]) - const [count, setCount] = useState(0) - console.log(count) - return <> - - > +import { h, render, useReducer, useCallback } from "../src/index" + +function random(max) { + return Math.round(Math.random() * 1000) % max +} + +const A = [ + "pretty", + "large", + "big", + "small", + "tall", + "short", + "long", + "handsome", + "plain", + "quaint", + "clean", + "elegant", + "easy", + "angry", + "crazy", + "helpful", + "mushy", + "odd", + "unsightly", + "adorable", + "important", + "inexpensive", + "cheap", + "expensive", + "fancy", +] +const C = [ + "red", + "yellow", + "blue", + "green", + "pink", + "brown", + "purple", + "brown", + "white", + "black", + "orange", +] +const N = [ + "table", + "chair", + "house", + "bbq", + "desk", + "car", + "pony", + "cookie", + "sandwich", + "burger", + "pizza", + "mouse", + "keyboard", +] + +let nextId = 1 + +function buildData(count) { + const data = new Array(count) + for (let i = 0; i < count; i++) { + data[i] = { + id: nextId++, + label: `${A[random(A.length)]} ${C[random(C.length)]} ${N[random(N.length)] + }`, + } + } + return data +} + +function listReducer(state, action) { + const { data, selected } = state + switch (action.type) { + case "RUN": + return { data: buildData(1000), selected: 0 } + case "RUN_LOTS": + return { data: buildData(10000), selected: 0 } + case "ADD": + return { data: data.concat(buildData(1000)), selected } + case "UPDATE": + const newData = data.slice(0) + for (let i = 0; i < newData.length; i += 10) { + const r = newData[i] + newData[i] = { id: r.id, label: r.label + " !!!" } + } + return { data: newData, selected } + case "CLEAR": + return { data: [], selected: 0 } + case "SWAP_ROWS": + return data.length > 998 ? { data: [data[0], data[998], ...data.slice(2, 998), data[1], data[999]], selected } : state + case "REMOVE": + const idx = data.findIndex((d) => d.id === action.id) + return { data: [...data.slice(0, idx), ...data.slice(idx + 1)], selected } + case "SELECT": + return { data, selected: action.id } + } + return state +} + +const Row = ({ selected, item, dispatch, id }) => { + return ( +