Skip to content

Commit ec40e35

Browse files
Disable edit (#97)
* 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)
1 parent dab1f94 commit ec40e35

File tree

6 files changed

+33
-3
lines changed

6 files changed

+33
-3
lines changed

packages/react-arborist/src/components/default-container.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,11 @@ export function DefaultContainer() {
160160
return;
161161
}
162162
if (e.key === "Enter") {
163-
if (!tree.props.onRename) return;
163+
const node = tree.focusedNode;
164+
if (!node) return;
165+
if (!node.isEditable || !tree.props.onRename) return;
164166
setTimeout(() => {
165-
if (tree.focusedNode) tree.edit(tree.focusedNode);
167+
if (node) tree.edit(node);
166168
});
167169
return;
168170
}

packages/react-arborist/src/interfaces/node-api.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ export class NodeApi<T = any> {
5858
return this.isLeaf ? false : !this.tree.isOpen(this.id);
5959
}
6060

61+
get isEditable() {
62+
return this.tree.isEditable(this.data);
63+
}
64+
6165
get isEditing() {
6266
return this.tree.editingId === this.id;
6367
}

packages/react-arborist/src/interfaces/tree-api.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,11 @@ export class TreeApi<T> {
529529
}
530530
}
531531

532+
isEditable(data: T) {
533+
const check = this.props.disableEdit || (() => false);
534+
return !utils.access(data, check) ?? true;
535+
}
536+
532537
isDraggable(data: T) {
533538
const check = this.props.disableDrag || (() => false);
534539
return !utils.access(data, check) ?? true;

packages/react-arborist/src/types/tree-props.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export interface TreeProps<T> {
3838
openByDefault?: boolean;
3939
selectionFollowsFocus?: boolean;
4040
disableMultiSelection?: boolean;
41+
disableEdit?: string | boolean | BoolFunc<T>;
4142
disableDrag?: string | boolean | BoolFunc<T>;
4243
disableDrop?: string | boolean | BoolFunc<T>;
4344
childrenAccessor?: string | ((d: T) => T[] | null);

packages/showcase/data/gmail.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export type GmailItem = {
66
name: string;
77
icon: ComponentType;
88
unread?: number;
9+
readOnly: boolean;
910
children?: GmailItem[];
1011
};
1112

@@ -14,95 +15,111 @@ export const gmailData: GmailItem[] = [
1415
id: "1",
1516
name: "Inbox",
1617
unread: 1,
18+
readOnly: true,
1719
icon: icons.MdInbox,
1820
},
1921
{
2022
id: "2",
2123
name: "Starred",
2224
unread: 0,
25+
readOnly: true,
2326
icon: icons.MdStarOutline,
2427
},
2528
{
2629
id: "3",
2730
name: "Snoozed",
2831
unread: 0,
32+
readOnly: true,
2933
icon: icons.MdAccessTime,
3034
},
3135
{
3236
id: "4",
3337
name: "Sent",
3438
unread: 0,
39+
readOnly: true,
3540
icon: icons.MdSend,
3641
},
3742
{
3843
id: "5",
3944
name: "Drafts",
4045
unread: 14,
46+
readOnly: true,
4147
icon: icons.MdOutlineDrafts,
4248
},
4349
{
4450
id: "6",
4551
name: "Spam",
4652
unread: 54,
53+
readOnly: true,
4754
icon: icons.MdOutlineReportGmailerrorred,
4855
},
4956
{
5057
id: "7",
5158
name: "Important",
5259
unread: 0,
60+
readOnly: true,
5361
icon: icons.MdLabelImportantOutline,
5462
},
5563
{
5664
id: "8",
5765
name: "Chats",
5866
unread: 0,
67+
readOnly: true,
5968
icon: icons.MdOutlineChat,
6069
},
6170
{
6271
id: "9",
6372
name: "Scheduled",
6473
unread: 0,
74+
readOnly: true,
6575
icon: icons.MdOutlineScheduleSend,
6676
},
6777
{
6878
id: "10",
6979
name: "All Mail",
7080
unread: 0,
81+
readOnly: true,
7182
icon: icons.MdOutlineMail,
7283
},
7384
{
7485
id: "11",
7586
name: "Trash",
7687
unread: 0,
88+
readOnly: true,
7789
icon: icons.MdOutlineDelete,
7890
},
7991
{
8092
id: "12",
8193
name: "Categories",
8294
icon: icons.MdOutlineLabel,
95+
readOnly: true,
8396
children: [
8497
{
8598
id: "13",
8699
name: "Social",
87100
unread: 946,
101+
readOnly: false,
88102
icon: icons.MdPeopleOutline,
89103
},
90104
{
91105
id: "14",
92106
name: "Updates",
93107
unread: 4580,
108+
readOnly: false,
94109
icon: icons.MdOutlineInfo,
95110
},
96111
{
97112
id: "15",
98113
name: "Forums",
99114
unread: 312,
115+
readOnly: false,
100116
icon: icons.MdChatBubbleOutline,
101117
},
102118
{
103119
id: "16",
104120
name: "Promotions",
105121
unread: 312,
122+
readOnly: false,
106123
icon: icons.MdOutlineLocalOffer,
107124
},
108125
],

packages/showcase/pages/gmail.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export default function GmailSidebar() {
3535
renderCursor={Cursor}
3636
searchTerm={term}
3737
paddingBottom={32}
38+
disableEdit={(data) => data.readOnly}
3839
>
3940
{Node}
4041
</Tree>
@@ -57,7 +58,7 @@ export default function GmailSidebar() {
5758
<li>Drag the items around</li>
5859
<li>Move focus with the arrow keys</li>
5960
<li>Toggle folders (press spacebar)</li>
60-
<li>Rename (press enter)</li>
61+
<li>Rename (press enter, only allowed on items in 'Categories')</li>
6162
<li>Create a new item (press A)</li>
6263
<li>Create a new folder (press shift+A)</li>
6364
<li>Delete items (press delete)</li>

0 commit comments

Comments
 (0)