Skip to content

Commit

Permalink
Disable edit (#97)
Browse files Browse the repository at this point in the history
* Add tree prop for disabling edit on individual items (#96)
* Update Gmail example to only allow editing of items inside Categories folder using disableEdit (#96)
  • Loading branch information
danielnilsson9 authored Jan 23, 2023
1 parent dab1f94 commit ec40e35
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 3 deletions.
6 changes: 4 additions & 2 deletions packages/react-arborist/src/components/default-container.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,11 @@ export function DefaultContainer() {
return;
}
if (e.key === "Enter") {
if (!tree.props.onRename) return;
const node = tree.focusedNode;
if (!node) return;
if (!node.isEditable || !tree.props.onRename) return;
setTimeout(() => {
if (tree.focusedNode) tree.edit(tree.focusedNode);
if (node) tree.edit(node);
});
return;
}
Expand Down
4 changes: 4 additions & 0 deletions packages/react-arborist/src/interfaces/node-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ export class NodeApi<T = any> {
return this.isLeaf ? false : !this.tree.isOpen(this.id);
}

get isEditable() {
return this.tree.isEditable(this.data);
}

get isEditing() {
return this.tree.editingId === this.id;
}
Expand Down
5 changes: 5 additions & 0 deletions packages/react-arborist/src/interfaces/tree-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,11 @@ export class TreeApi<T> {
}
}

isEditable(data: T) {
const check = this.props.disableEdit || (() => false);
return !utils.access(data, check) ?? true;
}

isDraggable(data: T) {
const check = this.props.disableDrag || (() => false);
return !utils.access(data, check) ?? true;
Expand Down
1 change: 1 addition & 0 deletions packages/react-arborist/src/types/tree-props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export interface TreeProps<T> {
openByDefault?: boolean;
selectionFollowsFocus?: boolean;
disableMultiSelection?: boolean;
disableEdit?: string | boolean | BoolFunc<T>;
disableDrag?: string | boolean | BoolFunc<T>;
disableDrop?: string | boolean | BoolFunc<T>;
childrenAccessor?: string | ((d: T) => T[] | null);
Expand Down
17 changes: 17 additions & 0 deletions packages/showcase/data/gmail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export type GmailItem = {
name: string;
icon: ComponentType;
unread?: number;
readOnly: boolean;
children?: GmailItem[];
};

Expand All @@ -14,95 +15,111 @@ export const gmailData: GmailItem[] = [
id: "1",
name: "Inbox",
unread: 1,
readOnly: true,
icon: icons.MdInbox,
},
{
id: "2",
name: "Starred",
unread: 0,
readOnly: true,
icon: icons.MdStarOutline,
},
{
id: "3",
name: "Snoozed",
unread: 0,
readOnly: true,
icon: icons.MdAccessTime,
},
{
id: "4",
name: "Sent",
unread: 0,
readOnly: true,
icon: icons.MdSend,
},
{
id: "5",
name: "Drafts",
unread: 14,
readOnly: true,
icon: icons.MdOutlineDrafts,
},
{
id: "6",
name: "Spam",
unread: 54,
readOnly: true,
icon: icons.MdOutlineReportGmailerrorred,
},
{
id: "7",
name: "Important",
unread: 0,
readOnly: true,
icon: icons.MdLabelImportantOutline,
},
{
id: "8",
name: "Chats",
unread: 0,
readOnly: true,
icon: icons.MdOutlineChat,
},
{
id: "9",
name: "Scheduled",
unread: 0,
readOnly: true,
icon: icons.MdOutlineScheduleSend,
},
{
id: "10",
name: "All Mail",
unread: 0,
readOnly: true,
icon: icons.MdOutlineMail,
},
{
id: "11",
name: "Trash",
unread: 0,
readOnly: true,
icon: icons.MdOutlineDelete,
},
{
id: "12",
name: "Categories",
icon: icons.MdOutlineLabel,
readOnly: true,
children: [
{
id: "13",
name: "Social",
unread: 946,
readOnly: false,
icon: icons.MdPeopleOutline,
},
{
id: "14",
name: "Updates",
unread: 4580,
readOnly: false,
icon: icons.MdOutlineInfo,
},
{
id: "15",
name: "Forums",
unread: 312,
readOnly: false,
icon: icons.MdChatBubbleOutline,
},
{
id: "16",
name: "Promotions",
unread: 312,
readOnly: false,
icon: icons.MdOutlineLocalOffer,
},
],
Expand Down
3 changes: 2 additions & 1 deletion packages/showcase/pages/gmail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export default function GmailSidebar() {
renderCursor={Cursor}
searchTerm={term}
paddingBottom={32}
disableEdit={(data) => data.readOnly}
>
{Node}
</Tree>
Expand All @@ -57,7 +58,7 @@ export default function GmailSidebar() {
<li>Drag the items around</li>
<li>Move focus with the arrow keys</li>
<li>Toggle folders (press spacebar)</li>
<li>Rename (press enter)</li>
<li>Rename (press enter, only allowed on items in 'Categories')</li>
<li>Create a new item (press A)</li>
<li>Create a new folder (press shift+A)</li>
<li>Delete items (press delete)</li>
Expand Down

0 comments on commit ec40e35

Please sign in to comment.