Code Monkey home page Code Monkey logo

Comments (1)

jbrodriguez avatar jbrodriguez commented on June 1, 2024 1

i tried this kind of workaround

the idea is to have a placeholder loading node as children and update the nodes array as needed, declaring it as a function state var
Screenshot 2023-12-14 at 14 04 46

but i'm getting an error
Screenshot 2023-12-14 at 14 06 47

seems like it's not calling the static getDerivedFromProps (and thus flattenNode), but can't check it because the lib is already bundled

import React from 'react';
import CheckboxTree from 'react-checkbox-tree';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import {
  faCheckSquare,
  faSquareMinus,
  faChevronRight,
  faChevronDown,
  faPlusSquare,
  faMinusSquare,
  faFolder,
  faFolderOpen,
  faFile,
  faSpinner,
} from '@fortawesome/free-solid-svg-icons';
import { faSquare } from '@fortawesome/free-regular-svg-icons';

import 'react-checkbox-tree/lib/react-checkbox-tree.css';


interface Node {
  value: string;
  label: string;
  children?: Node[];
  showCheckbox?: boolean;
  icon?: JSX.Element;
}

export const FileSystem: React.FunctionComponent = () => {
  const [nodes, setNodes] = React.useState<Node[]>([
    {
      value: '/mnt',
      label: '/mnt',
      children: [
        {
          value: 'loading',
          label: 'loading',
          // children: [],
          showCheckbox: false,
          icon: (
            <FontAwesomeIcon
              className="fill-red-400 text-yellow-600"
              icon={faSpinner}
            />
          ),
        },
      ],
    },
  ]);
  const [checked, setChecked] = React.useState<string[]>([]);
  const [expanded, setExpanded] = React.useState<string[]>([]);

  const onCheck = (value: string[]) => {
    console.log('onCheck ', value);
    setChecked(value);
  };

  const onExpand = (value: string[]) => {
    console.log('onExpand ', value);
    // setIsLoading(true);
    // const loaded =
    const newNodes = [...nodes];
    newNodes[0].children = [
      {
        value: 'films',
        label: 'films',
        children: [
          {
            value: 'loading',
            label: 'loading',
            children: [],
            showCheckbox: false,
            icon: (
              <FontAwesomeIcon
                className="fill-red-400 text-yellow-600"
                icon={faSpinner}
              />
            ),
          },
        ],
      },
    ];
    console.log('newNodes ', newNodes);
    setNodes(newNodes);
    setExpanded(value);
  };

  return (
    <div className="flex flex-1 bg-neutral-200 dark:bg-gray-950">
      <div className={`overflow-y-auto`} style={{ height: 500 }}>
        <FontAwesomeIcon
          className="rct-icon rct-icon-check"
          icon="check-square"
        />

        <CheckboxTree
          checked={checked}
          expanded={expanded}
          nodes={nodes}
          onCheck={onCheck}
          onExpand={onExpand}
          noCascade
          optimisticToggle={false}
          icons={{
            check: (
              <FontAwesomeIcon
                className="fill-red-400 text-yellow-600"
                icon={faCheckSquare}
              />
            ),
            uncheck: (
              <FontAwesomeIcon className="text-slate-700" icon={faSquare} />
            ),
            halfCheck: (
              <FontAwesomeIcon
                className="rct-icon rct-icon-half-check"
                icon={faSquareMinus}
              />
            ),
            expandClose: (
              <FontAwesomeIcon
                className="rct-icon rct-icon-expand-close"
                icon={faChevronRight}
              />
            ),
            expandOpen: (
              <FontAwesomeIcon
                className="rct-icon rct-icon-expand-close"
                icon={faChevronDown}
              />
            ),
            expandAll: (
              <FontAwesomeIcon className="fill-red-600" icon={faPlusSquare} />
            ),
            collapseAll: (
              <FontAwesomeIcon
                className="rct-icon rct-icon-collapse-all"
                icon={faMinusSquare}
              />
            ),
            parentClose: (
              <FontAwesomeIcon
                className="rct-icon rct-icon-parent-close"
                icon={faFolder}
              />
            ),
            parentOpen: (
              <FontAwesomeIcon
                className="rct-icon rct-icon-parent-open"
                icon={faFolderOpen}
              />
            ),
            leaf: (
              <FontAwesomeIcon
                className="rct-icon rct-icon-leaf-close"
                icon={faFile}
              />
            ),
          }}
        />
      </div>
    </div>
  );
};

from react-checkbox-tree.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.