Direactree

Names & permissions

Direactree checks names while the user types, refuses moves that would clash, and lets you decide per node what can be renamed, deleted, moved, or dropped into.

Name checks

The create and rename editor checks the name on every keystroke. While it is invalid, Save is disabled and a message explains why. By default a name is refused when it:

  • is empty or made only of spaces
  • contains / \ : * ? " < > | or a control character
  • is . or ..
  • is longer than 255 characters
  • already exists in the same folder, ignoring case (Readme.md and readme.md clash)

The name sent to onSave is trimmed. A rename that only changes case, such as readme.md to README.md, is allowed.

TRY ITName checks in the editor

Select "Web Applications", create a file, and try " ", "a/b", "..", "Config.JSON", or "notes". Save stays disabled while the name is invalid.

  • Desktop
  • Projects
  • Web Applications
  • project-notes.txt
  • config.json
  • utils.ts
  • Documents
  • REST-API.md
  • DireactreeUsage.tsx

nameRules

Type
Partial<NameRules>
Required
No
interface NameRules {
  trim: boolean;                 // default true
  invalidChars: RegExp | string; // a string is read as a list of characters
  reserved: string[];            // default [".", ".."]
  caseSensitive: boolean;        // default false
  maxLength: number;             // default 255, 0 turns it off
}
<Direactree
  structure={structure}
  onSave={onSave}
  nameRules={{ caseSensitive: true, maxLength: 64, invalidChars: /[\\/#]/ }}
/>

validateName

Type
false | (name: string, context: NameValidationContext) => string | null | undefined
Required
No

Your own rule runs after the built-in ones. Return a message to block saving, or null to allow it. The context says what is being saved and where:

interface NameValidationContext {
  actionType: "create" | "edit";
  type: "file" | "folder";
  parent: NodePath | null; // the folder that will hold it
  node: NodePath | null;   // the node being renamed
  siblings: TreeNode[];
}
<Direactree
  structure={structure}
  onSave={onSave}
  validateName={(name, { type }) =>
    type === "file" && !/\.\w+$/.test(name) ? "Add a file extension." : null
  }
/>

validateName={false} turns every check off except the empty check.

The same check as a function

validateName() is exported, so your own forms and your server can apply the same rules:

TRY ITvalidateName()

Type a name. The folder already holds README.md and src. Add your own forbidden characters to replace the default set.

validateName("readme.md", { siblings })
result
{
  "ok": false,
  "name": "readme.md",
  "code": "duplicate",
  "message": "“readme.md” already exists in this folder."
}
import { validateName, hasNameConflict, defaultNameRules } from "direactree";

const result = validateName(" notes.md ", { siblings, rules: { caseSensitive: true } });
// { ok: true, name: "notes.md" }
// or { ok: false, name, code: "duplicate", message: "“notes.md” already exists in this folder." }

code is one of empty, invalidChars, reserved, tooLong, or duplicate. Messages come from messages, so they follow your translation.

Move conflicts

A drop that would put two items with the same name in one folder is refused before onNodeMove runs. Screen readers hear messages.moveConflict. Reordering inside the same folder is never a conflict.

TRY ITMove conflicts

Drag drafts/notes.md into "published". It already holds Notes.md, so the drop is refused and onNodeMove never runs. plan.md moves fine.

  • drafts
  • notes.md
  • plan.md
  • published
  • Notes.md
onNodeMove calls
Nothing yet. Use the tree.

Permissions

Every node has five permissions. A missing one counts as allowed.

PermissionAllows
renameRename from the toolbox, F2, or the menu
deleteDelete from the toolbox, the Delete key, or the menu
moveDragging the node
dropDropping other nodes into this folder
createChildCreating files and folders inside this folder

There are three ways to set them, merged in this order:

  1. locked: true on a node turns all five off.
  2. permissions on a node turns single ones on or off.
  3. getNodePermissions(node, path) decides from your own rules.
const structure: TreeNode[] = [
  {
    id: "root",
    name: "project",
    type: "folder",
    permissions: { delete: false, rename: false, move: false },
    children: [
      { id: "nm", name: "node_modules", type: "folder", locked: true, children: [] },
      { id: "env", name: ".env", type: "file", permissions: { move: false } },
    ],
  },
];

<Direactree
  structure={structure}
  getNodePermissions={(node, path) => (user.isAdmin ? {} : { delete: false })}
/>
TRY ITPermissions

Select rows and watch the toolbox, dragging, Delete, and F2 follow the rules. vendor is locked with locked: true.

  • workspace
  • shared (read-only)
  • policy.pdf
  • my files
  • draft.docx
  • .env
selected node permissions
null

Locked rows get data-locked="true", so they can be styled. Permissions only shape the interface; check them on your server too.