Skip to content

Commit 23639bd

Browse files
committed
Implement custom buttons
1 parent 50038ee commit 23639bd

File tree

6 files changed

+35
-0
lines changed

6 files changed

+35
-0
lines changed

demo/src/App.tsx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,22 @@ function App() {
366366
stringTruncate={90}
367367
customNodeDefinitions={dataDefinition?.customNodeDefinitions}
368368
customText={dataDefinition?.customTextDefinitions}
369+
// customButtons={[
370+
// {
371+
// Element: () => (
372+
// <svg fill="none" viewBox="0 0 24 24" height="1em" width="1em">
373+
// <path
374+
// fill="currentColor"
375+
// fillRule="evenodd"
376+
// d="M12 21a9 9 0 100-18 9 9 0 000 18zm0 2c6.075 0 11-4.925 11-11S18.075 1 12 1 1 5.925 1 12s4.925 11 11 11z"
377+
// clipRule="evenodd"
378+
// />
379+
// <path fill="currentColor" d="M16 12l-6 4.33V7.67L16 12z" />
380+
// </svg>
381+
// ),
382+
// onClick: (nodeData) => console.log(nodeData),
383+
// },
384+
// ]}
369385
onChange={dataDefinition?.onChange ?? undefined}
370386
jsonParse={JSON5.parse}
371387
/>

src/ButtonPanels.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
type CopyFunction,
99
type CopyType,
1010
type NodeData,
11+
type CustomButtonDefinition,
1112
} from './types'
1213
import './style.css'
1314

@@ -19,6 +20,7 @@ interface EditButtonProps {
1920
type?: CollectionDataType
2021
nodeData: NodeData
2122
translate: TranslateFunction
23+
customButtons: CustomButtonDefinition[]
2224
}
2325

2426
export const EditButtons: React.FC<EditButtonProps> = ({
@@ -27,6 +29,7 @@ export const EditButtons: React.FC<EditButtonProps> = ({
2729
handleAdd,
2830
enableClipboard,
2931
type,
32+
customButtons,
3033
nodeData,
3134
translate,
3235
}) => {
@@ -98,6 +101,11 @@ export const EditButtons: React.FC<EditButtonProps> = ({
98101
<Icon name="add" nodeData={nodeData} />
99102
</div>
100103
)}
104+
{customButtons?.map(({ Element, onClick }, i) => (
105+
<div key={i} onClick={(e) => onClick(nodeData, e)}>
106+
<Element />
107+
</div>
108+
))}
101109
{isAdding && handleAdd && type === 'object' && (
102110
<>
103111
<input

src/CollectionNode.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,7 @@ export const CollectionNode: React.FC<CollectionNodeProps> = (props) => {
363363
type={collectionType}
364364
nodeData={nodeData}
365365
translate={translate}
366+
customButtons={props.customButtons}
366367
/>
367368
)
368369

src/JsonEditor.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ const Editor: React.FC<JsonEditorProps> = ({
6060
id,
6161
customText = {},
6262
customNodeDefinitions = [],
63+
customButtons = [],
6364
jsonParse = JSON.parse,
6465
jsonStringify = (data: JsonData) => JSON.stringify(data, null, 2),
6566
}) => {
@@ -276,6 +277,7 @@ const Editor: React.FC<JsonEditorProps> = ({
276277
stringTruncate,
277278
translate,
278279
customNodeDefinitions,
280+
customButtons,
279281
parentData: null,
280282
jsonParse,
281283
jsonStringify,

src/ValueNodeWrapper.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,7 @@ export const ValueNodeWrapper: React.FC<ValueNodeProps> = (props) => {
304304
handleDelete={canDelete ? handleDelete : undefined}
305305
enableClipboard={enableClipboard}
306306
translate={translate}
307+
customButtons={props.customButtons}
307308
nodeData={nodeData}
308309
/>
309310
)

src/types.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ export interface JsonEditorProps {
4343
translations?: Partial<LocalisedStrings>
4444
customNodeDefinitions?: CustomNodeDefinition[]
4545
customText?: CustomTextDefinitions
46+
customButtons?: CustomButtonDefinition[]
4647
jsonParse?: (input: string) => JsonData
4748
jsonStringify?: (input: JsonData) => string
4849
}
@@ -187,6 +188,7 @@ interface BaseNodeProps {
187188
indent: number
188189
translate: TranslateFunction
189190
customNodeDefinitions: CustomNodeDefinition[]
191+
customButtons: CustomButtonDefinition[]
190192
}
191193

192194
export interface CollectionNodeProps extends BaseNodeProps {
@@ -244,6 +246,11 @@ export interface CustomNodeDefinition<T = Record<string, unknown>, U = Record<st
244246

245247
export type CustomTextDefinitions = Partial<{ [key in keyof LocalisedStrings]: CustomTextFunction }>
246248

249+
export interface CustomButtonDefinition {
250+
Element: React.FC
251+
onClick: (nodeData: NodeData, e: React.MouseEvent) => void
252+
}
253+
247254
export interface InputProps {
248255
value: unknown
249256
setValue: (value: ValueData) => void

0 commit comments

Comments
 (0)