mirror of
https://github.com/johndoe6345789/low-code-react-app-b.git
synced 2026-04-24 13:44:54 +00:00
Add component binding dialog hook
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
import { useState } from 'react'
|
||||
import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle } from '@/components/ui/dialog'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs'
|
||||
import { BindingEditor } from '@/components/molecules/BindingEditor'
|
||||
import { DataSource, UIComponent } from '@/types/json-ui'
|
||||
import { Link } from '@phosphor-icons/react'
|
||||
import { useComponentBindingDialog } from '@/hooks/use-component-binding-dialog'
|
||||
|
||||
interface ComponentBindingDialogProps {
|
||||
open: boolean
|
||||
@@ -21,18 +21,12 @@ export function ComponentBindingDialog({
|
||||
onOpenChange,
|
||||
onSave,
|
||||
}: ComponentBindingDialogProps) {
|
||||
const [editingComponent, setEditingComponent] = useState<UIComponent | null>(component)
|
||||
|
||||
const handleSave = () => {
|
||||
if (!editingComponent) return
|
||||
onSave(editingComponent)
|
||||
onOpenChange(false)
|
||||
}
|
||||
|
||||
const updateBindings = (bindings: Record<string, any>) => {
|
||||
if (!editingComponent) return
|
||||
setEditingComponent({ ...editingComponent, bindings })
|
||||
}
|
||||
const { editingComponent, handleSave, updateBindings } = useComponentBindingDialog({
|
||||
component,
|
||||
open,
|
||||
onOpenChange,
|
||||
onSave,
|
||||
})
|
||||
|
||||
if (!editingComponent) return null
|
||||
|
||||
|
||||
40
src/hooks/use-component-binding-dialog.ts
Normal file
40
src/hooks/use-component-binding-dialog.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
import { useCallback, useEffect, useState } from 'react'
|
||||
import { UIComponent } from '@/types/json-ui'
|
||||
|
||||
interface UseComponentBindingDialogOptions {
|
||||
component: UIComponent | null
|
||||
open: boolean
|
||||
onOpenChange: (open: boolean) => void
|
||||
onSave: (component: UIComponent) => void
|
||||
}
|
||||
|
||||
export function useComponentBindingDialog({
|
||||
component,
|
||||
open,
|
||||
onOpenChange,
|
||||
onSave,
|
||||
}: UseComponentBindingDialogOptions) {
|
||||
const [editingComponent, setEditingComponent] = useState<UIComponent | null>(component)
|
||||
|
||||
useEffect(() => {
|
||||
if (open) {
|
||||
setEditingComponent(component)
|
||||
}
|
||||
}, [component, open])
|
||||
|
||||
const updateBindings = useCallback((bindings: Record<string, any>) => {
|
||||
setEditingComponent(prev => (prev ? { ...prev, bindings } : prev))
|
||||
}, [])
|
||||
|
||||
const handleSave = useCallback(() => {
|
||||
if (!editingComponent) return
|
||||
onSave(editingComponent)
|
||||
onOpenChange(false)
|
||||
}, [editingComponent, onOpenChange, onSave])
|
||||
|
||||
return {
|
||||
editingComponent,
|
||||
handleSave,
|
||||
updateBindings,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user