Skip to content

Commit bb48d68

Browse files
committed
feat: added tag attribute pass dynamicly
1 parent ca76bd9 commit bb48d68

File tree

3 files changed

+36
-15
lines changed

3 files changed

+36
-15
lines changed

src/MixInput.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ import Placeholder from '@tiptap/extension-placeholder'
1111
import TagExtension from './TagExtension'
1212
import { type MixInputProps, type MixInputRef, type MixInputValue } from './MixInputType'
1313

14+
const DEFAULT_TAG_ATTRS = {
15+
id: undefined,
16+
label: undefined,
17+
class: undefined,
18+
style: undefined,
19+
}
20+
1421
const MixInput = forwardRef((props: MixInputProps, ref: ForwardedRef<MixInputRef>) => {
1522
const {
1623
onChange,
@@ -22,6 +29,7 @@ const MixInput = forwardRef((props: MixInputProps, ref: ForwardedRef<MixInputRef
2229
tagClassName,
2330
editorOptions,
2431
className,
32+
tagAttrs,
2533
...restProps
2634
} = props
2735

@@ -38,6 +46,7 @@ const MixInput = forwardRef((props: MixInputProps, ref: ForwardedRef<MixInputRef
3846
Placeholder.configure({ placeholder }),
3947
TagExtension.configure({
4048
tagClassName,
49+
attrs: { ...DEFAULT_TAG_ATTRS, ...tagAttrs},
4150
}),
4251
],
4352
onUpdate: ({ editor }) => {
@@ -62,7 +71,7 @@ const MixInput = forwardRef((props: MixInputProps, ref: ForwardedRef<MixInputRef
6271

6372
useImperativeHandle(ref, () => ({
6473
element: editorRef.current,
65-
editor: editor,
74+
editor,
6675
insertContent,
6776
}))
6877

src/MixInputType.d.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import '@total-typescript/ts-reset'
22
import { Editor, } from '@tiptap/core'
3-
import { UseEditorOptions } from '@tiptap/react'
3+
import { EditorContentProps, UseEditorOptions } from '@tiptap/react'
44

55
import type { CSSProperties, HTMLAttributes } from 'react'
66
export interface Tag {
@@ -10,20 +10,22 @@ export interface Tag {
1010
label?: string
1111
class?: string | string[]
1212
style?: CSSProperties
13+
[key: string]: string | string[] | CSSProperties | undefined
1314
}
1415
}
1516

1617
export type MixInputValue = Tag | string
1718

1819
export type MixInputValues = MixInputValue[][]
1920

20-
export interface MixInputProps extends HTMLAttributes<HTMLDivElement> {
21+
export interface MixInputProps extends HTMLAttributes<HTMLDivElement>, Omit<EditorContentProps, 'editor'> {
2122
value: MixInputValues
2223
placeholder?: string
2324
onChange: (value: MixInputValues) => void
2425
readonly?: boolean
2526
tagClassName?: string
2627
editorOptions?: UseEditorOptions
28+
tagAttrs?: Record<string, string | undefined>
2729
// multiline?: boolean
2830
// showTagDeleteBtn?: boolean
2931
}

src/TagExtension.ts

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { mergeAttributes, Node } from "@tiptap/core";
1+
import { Attribute, mergeAttributes, Node } from "@tiptap/core";
22

33
export default Node.create({
44
name: "tag",
@@ -23,30 +23,36 @@ export default Node.create({
2323
addNodeView: () => ({ node, extension }) => {
2424
const span = document.createElement('span');
2525
span.className = 'mi-tag'
26-
span.innerHTML = node.attrs.label
27-
// span.setAttribute('contenteditable', 'false');
26+
const { label, id, class: classes, style, ...restAttrs } = node.attrs
27+
span.innerHTML = label
2828
span.setAttribute('data-type', 'tag')
2929

30-
if (node.attrs.id) {
31-
span.setAttribute('data-id', node.attrs.id)
30+
if (id) {
31+
span.setAttribute('data-id', id)
3232
}
3333

3434
if (extension.options.tagClassName) {
3535
span.classList.add(extension.options.tagClassName)
3636
}
3737

38-
if (node.attrs.class) {
39-
if (Array.isArray(node.attrs.class)) {
40-
node.attrs.class.forEach((c) => {
38+
if (classes) {
39+
if (Array.isArray(classes)) {
40+
classes.forEach((c) => {
4141
span.classList.add(c as string)
4242
})
4343
} else {
44-
span.classList.add(node.attrs.class)
44+
span.classList.add(classes)
4545
}
4646
}
4747

48-
if (node.attrs.style) {
49-
Object.assign(span.style, node.attrs.style)
48+
if (style) {
49+
Object.assign(span.style, style)
50+
}
51+
52+
if (Object.keys(restAttrs).length) {
53+
Object.keys(restAttrs).forEach((key) => {
54+
span.dataset[key] = restAttrs[key]
55+
})
5056
}
5157

5258
const dom = document.createElement('span')
@@ -58,6 +64,10 @@ export default Node.create({
5864
},
5965

6066
addAttributes() {
61-
return { label: undefined, id: undefined, class: undefined, style: undefined };
67+
const extraAttrs: Record<string, Attribute> = {}
68+
for (const key in this.options.attrs) {
69+
extraAttrs[key] = { default: this.options.attrs[key] }
70+
}
71+
return extraAttrs
6272
}
6373
});

0 commit comments

Comments
 (0)