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.mdandreadme.mdclash)
The name sent to onSave is trimmed. A rename that only changes case, such as readme.md to README.md, is allowed.
Select "Web Applications", create a file, and try " ", "a/b", "..", "Config.JSON", or "notes". Save stays disabled while the name is invalid.
nameRules
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
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:
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 }){
"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.
Drag drafts/notes.md into "published". It already holds Notes.md, so the drop is refused and onNodeMove never runs. plan.md moves fine.
Nothing yet. Use the tree.
Permissions
Every node has five permissions. A missing one counts as allowed.
| Permission | Allows |
|---|---|
rename | Rename from the toolbox, F2, or the menu |
delete | Delete from the toolbox, the Delete key, or the menu |
move | Dragging the node |
drop | Dropping other nodes into this folder |
createChild | Creating files and folders inside this folder |
There are three ways to set them, merged in this order:
locked: trueon a node turns all five off.permissionson a node turns single ones on or off.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 })}
/>
Select rows and watch the toolbox, dragging, Delete, and F2 follow the rules. vendor is locked with locked: true.
null
Locked rows get data-locked="true", so they can be styled. Permissions only shape the interface; check them on your server too.