@@ -31,7 +31,6 @@ function parseTaskItems(content: string): TaskItem[] {
3131
3232export const MarkdownRenderer = memo ( function MarkdownRenderer ( { content, className = '' , onContentChange } : MarkdownRendererProps ) {
3333 const taskItems = useMemo ( ( ) => parseTaskItems ( content ) , [ content ] )
34- let taskInputIndex = 0
3534
3635 const handleToggle = useCallback ( ( taskItem : TaskItem ) => {
3736 if ( ! onContentChange ) return
@@ -47,27 +46,33 @@ export const MarkdownRenderer = memo(function MarkdownRenderer({ content, classN
4746 }
4847 } , [ content , onContentChange ] )
4948
49+ const handleInputToggle = useCallback ( ( input : HTMLInputElement ) => {
50+ const inputs = Array . from ( input . closest ( '.prose' ) ?. querySelectorAll ( 'input[type="checkbox"]' ) ?? [ ] )
51+ const taskItem = taskItems [ inputs . indexOf ( input ) ]
52+ if ( taskItem ) {
53+ handleToggle ( taskItem )
54+ }
55+ } , [ handleToggle , taskItems ] )
56+
5057 const components : Components = {
5158 ...markdownComponents ,
5259 input ( props ) {
53- const { type, ...rest } = props
60+ const { type, checked, disabled, ...rest } = props
61+ delete ( rest as Record < string , unknown > ) . node
5462
5563 if ( type === 'checkbox' ) {
56- const taskItem = taskItems [ taskInputIndex ++ ]
57-
58- if ( taskItem ) {
59- return (
60- < input
61- type = "checkbox"
62- checked = { taskItem . checked }
63- onChange = { ( ) => handleToggle ( taskItem ) }
64- className = "cursor-pointer accent-primary"
65- />
66- )
67- }
64+ return (
65+ < input
66+ type = "checkbox"
67+ checked = { Boolean ( checked ) }
68+ onChange = { ( event ) => handleInputToggle ( event . currentTarget ) }
69+ className = "cursor-pointer accent-primary"
70+ { ...rest }
71+ />
72+ )
6873 }
6974
70- return < input type = { type } { ...rest } />
75+ return < input type = { type } checked = { checked } disabled = { disabled } { ...rest } />
7176 } ,
7277 }
7378
0 commit comments