-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
15 changed files
with
374 additions
and
152 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
128 changes: 128 additions & 0 deletions
128
packages/design/src/FormManager/FormEdit/DraggableList.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
import React, { Children, useState } from 'react'; | ||
import { | ||
DndContext, | ||
closestCenter, | ||
KeyboardSensor, | ||
PointerSensor, | ||
useSensor, | ||
useSensors, | ||
} from '@dnd-kit/core'; | ||
import { | ||
arrayMove, | ||
SortableContext, | ||
sortableKeyboardCoordinates, | ||
useSortable, | ||
verticalListSortingStrategy, | ||
} from '@dnd-kit/sortable'; | ||
import { CSS } from '@dnd-kit/utilities'; | ||
|
||
import { | ||
getFormElement, | ||
type FormDefinition, | ||
type FormElement, | ||
FormElementId, | ||
} from '@atj/forms'; | ||
|
||
import { SequenceElement } from '@atj/forms/src/config/elements/sequence'; | ||
|
||
const SortableItem = ({ | ||
id, | ||
children, | ||
}: { | ||
id: string; | ||
children: React.ReactNode; | ||
}) => { | ||
const { attributes, listeners, setNodeRef, transform, transition } = | ||
useSortable({ id }); | ||
|
||
const style = { | ||
transform: CSS.Transform.toString(transform), | ||
transition, | ||
}; | ||
|
||
return ( | ||
<li ref={setNodeRef} style={style}> | ||
<div className="editFieldsRowWrapper grid-row grid-gap"> | ||
<div | ||
className="editPageGrabButtonWrapper grid-col-1 grid-col" | ||
{...listeners} | ||
{...attributes} | ||
style={{ cursor: 'grab' }} | ||
> | ||
<span className="grabber1">:::</span> | ||
<span className="grabber2">:::</span> | ||
</div> | ||
<div className="editFieldsWrapper grid-col-11 grid-col">{children}</div> | ||
</div> | ||
</li> | ||
); | ||
}; | ||
|
||
type DraggableListProps = React.PropsWithChildren<{ | ||
element: FormElement<SequenceElement>; | ||
form: FormDefinition; | ||
setSelectedElement: (element: FormElement) => void; | ||
}>; | ||
export const DraggableList: React.FC<DraggableListProps> = ({ | ||
element, | ||
form, | ||
setSelectedElement, | ||
children, | ||
}) => { | ||
const [elements, setElements] = useState<FormElement[]>( | ||
element.data.elements.map((elementId: FormElementId) => { | ||
return getFormElement(form, elementId); | ||
}) | ||
); | ||
const sensors = useSensors( | ||
useSensor(PointerSensor), | ||
useSensor(KeyboardSensor, { coordinateGetter: sortableKeyboardCoordinates }) | ||
); | ||
|
||
const arrayChildren = Children.toArray(children); | ||
return ( | ||
<DndContext | ||
sensors={sensors} | ||
collisionDetection={closestCenter} | ||
onDragEnd={event => { | ||
const { active, over } = event; | ||
if (over === null) { | ||
return; | ||
} | ||
if (active.id !== over.id) { | ||
const oldIndex = elements.findIndex(element => { | ||
return element.id === active.id; | ||
}); | ||
const newIndex = elements.findIndex(element => { | ||
return element.id === over.id; | ||
}); | ||
const newOrder = arrayMove(elements, oldIndex, newIndex); | ||
setElements(newOrder); | ||
setSelectedElement({ | ||
id: element.id, | ||
type: element.type, | ||
data: { | ||
elements: newOrder.map(element => element.id), | ||
}, | ||
default: { | ||
elements: [], | ||
}, | ||
required: element.required, | ||
} satisfies SequenceElement); | ||
} | ||
}} | ||
> | ||
<SortableContext items={elements} strategy={verticalListSortingStrategy}> | ||
<ul className="editFormWrapper"> | ||
{arrayChildren.map((child, index) => ( | ||
<SortableItem key={index} id={elements[index].id}> | ||
{child} | ||
</SortableItem> | ||
))} | ||
</ul> | ||
</SortableContext> | ||
</DndContext> | ||
); | ||
}; | ||
|
||
export default DraggableList; |
85 changes: 45 additions & 40 deletions
85
packages/design/src/FormManager/FormEdit/FormElementEdit.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,67 @@ | ||
import React from 'react'; | ||
import React, { useEffect } from 'react'; | ||
import { FormProvider, useForm } from 'react-hook-form'; | ||
|
||
import { | ||
FormDefinition, | ||
FormElement, | ||
FormElementMap, | ||
getFormElementConfig, | ||
updateElement, | ||
type FormElementMap, | ||
} from '@atj/forms'; | ||
import { FormEditUIContext } from '../../config'; | ||
import { usePreviewContext } from './context'; | ||
|
||
export const FormElementEdit = ({ | ||
context, | ||
initialForm, | ||
formElement, | ||
onChange, | ||
}: { | ||
context: FormEditUIContext; | ||
initialForm: FormDefinition; | ||
formElement: FormElement; | ||
onChange: (form: FormDefinition) => void; | ||
}) => { | ||
const { form, selectedElement, setCurrentForm } = usePreviewContext(); | ||
if (!selectedElement) { | ||
return; | ||
} | ||
|
||
const methods = useForm<FormElementMap>({ | ||
defaultValues: { | ||
[formElement.id]: formElement, | ||
[selectedElement.id]: selectedElement, | ||
}, | ||
}); | ||
const SelectedEditComponent = context.editComponents[formElement.type]; | ||
useEffect(() => { | ||
methods.setValue(selectedElement.id, selectedElement); | ||
}, [selectedElement]); | ||
|
||
const SelectedEditComponent = context.editComponents[selectedElement.type]; | ||
return ( | ||
<FormProvider {...methods}> | ||
<form | ||
className="editForm" | ||
onSubmit={methods.handleSubmit(formData => { | ||
const elementConfig = getFormElementConfig( | ||
context.config, | ||
formElement.type | ||
); | ||
const data = formData[formElement.id].data; | ||
const result = elementConfig.parseConfigData(data); | ||
if (!result.success) { | ||
return; | ||
} | ||
const updatedForm = updateElement(initialForm, { | ||
...formElement, | ||
data: result.data, | ||
}); | ||
onChange(updatedForm); | ||
})} | ||
> | ||
<SelectedEditComponent | ||
context={context} | ||
form={initialForm} | ||
element={formElement} | ||
/> | ||
<p> | ||
<input className="usa-button" type="submit" value="Save" /> | ||
</p> | ||
</form> | ||
<div className="settingsContainer"> | ||
<h2>Editing {selectedElement.id}...</h2> | ||
<form | ||
className="editForm" | ||
onSubmit={methods.handleSubmit(formData => { | ||
const elementConfig = getFormElementConfig( | ||
context.config, | ||
selectedElement.type | ||
); | ||
const data = formData[selectedElement.id].data; | ||
const result = elementConfig.parseConfigData(data); | ||
if (!result.success) { | ||
return; | ||
} | ||
const updatedForm = updateElement(form, { | ||
...selectedElement, | ||
data: result.data, | ||
}); | ||
setCurrentForm(updatedForm); | ||
})} | ||
> | ||
<SelectedEditComponent | ||
context={context} | ||
form={form} | ||
element={selectedElement} | ||
/> | ||
<p> | ||
<input className="usa-button" type="submit" value="Save" /> | ||
</p> | ||
</form> | ||
</div> | ||
</FormProvider> | ||
); | ||
}; |
Oops, something went wrong.