-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add model-properties form component
- Loading branch information
Showing
11 changed files
with
440 additions
and
199 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
src/leihs/inventory/client/components/basic_form_field.cljs
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,57 @@ | ||
(ns leihs.inventory.client.components.basic-form-field | ||
(:require | ||
["@@/checkbox" :refer [Checkbox]] | ||
["@@/dropzone" :refer [Dropzone]] | ||
["@@/form" :refer [FormField FormItem FormLabel FormControl FormDescription FormMessage]] | ||
["@@/input" :refer [Input]] | ||
["@@/textarea" :refer [Textarea]] | ||
[leihs.inventory.client.lib.utils :refer [cj jc]] | ||
[leihs.inventory.client.routes.models.create.components.accessories-list :refer [AccessoryList]] | ||
[uix.core :as uix :refer [defui $]])) | ||
|
||
comment "available form fields" | ||
(def fields-map | ||
{"input" Input | ||
"dropzone" Dropzone | ||
"textarea" Textarea | ||
"checkbox" Checkbox | ||
"accessory-list" AccessoryList}) | ||
|
||
(defui main | ||
"Main function for rendering a form field component. | ||
Arguments: | ||
- `control`: The control element for the form field. | ||
- `input`: A map containing input properties. | ||
- `class-name`: A string for additional CSS class names for the input field | ||
- `label`: A boolean indicating whether to display the label. | ||
- `description`: A boolean indicating whether to display the description. | ||
- `name`: The name of the form field. | ||
Default values: | ||
- `label`: true | ||
- `description`: true | ||
- `class-name`: empty string | ||
- `name`: The name from the `input` map." | ||
|
||
[{:keys [control input class-name | ||
label description name] | ||
:or {label true description true | ||
class-name "" name (:name input)}}] | ||
|
||
(let [comp (get fields-map (:component input))] | ||
(when comp | ||
($ FormField {:control (cj control) | ||
:name name | ||
:render #($ FormItem | ||
(when label ($ FormLabel (:label input))) | ||
($ FormControl | ||
($ comp (merge | ||
{:class-name class-name} | ||
(:props input) | ||
(:field (jc %))))) | ||
|
||
(when description ($ FormDescription | ||
($ :<> (:description input)))) | ||
|
||
($ FormMessage))})))) |
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
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
113 changes: 113 additions & 0 deletions
113
src/leihs/inventory/client/routes/models/create/components/model_properties.cljs
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,113 @@ | ||
(ns leihs.inventory.client.routes.models.create.components.model-properties | ||
(:require | ||
["@/components/react/sortable-list" :refer [SortableList Draggable DragHandle]] | ||
["@@/button" :refer [Button]] | ||
["@@/form" :refer [FormField FormItem FormControl FormMessage]] | ||
["@@/table" :refer [Table TableHeader TableRow TableHead TableBody TableCell]] | ||
["@@/textarea" :refer [Textarea]] | ||
["lucide-react" :refer [CirclePlus Trash]] | ||
["react-hook-form" :as hook-form] | ||
[leihs.inventory.client.lib.utils :refer [cj jc]] | ||
[uix.core :as uix :refer [defui $]] | ||
[uix.dom])) | ||
|
||
(defn find-index-by-id [vec id] | ||
(some (fn [[idx item]] | ||
(when (= (:id item) id) | ||
idx)) | ||
(map-indexed vector vec))) | ||
|
||
(defn handle-drag-end [event fields move] | ||
(let [ev (jc event)] | ||
(when-not (= (-> ev :over :id) | ||
(-> ev :active :id)) | ||
|
||
(let [old-index (find-index-by-id fields | ||
(-> ev :active :id)) | ||
new-index (find-index-by-id fields | ||
(-> ev :over :id))] | ||
|
||
(move old-index new-index))))) | ||
|
||
(defui properties-table [{:keys [children inputs]}] | ||
($ Table | ||
($ TableHeader | ||
($ TableRow | ||
($ TableHead (-> inputs (nth 0) :label)) | ||
($ TableHead (-> inputs (nth 1) :label)) | ||
($ TableHead "Actions"))) | ||
($ TableBody children))) | ||
|
||
(defui main [{:keys [control props]}] | ||
(let [{:keys [fields append remove move]} (jc (hook-form/useFieldArray | ||
(cj {:control control | ||
:name "properties"}))) | ||
inputs (:inputs props)] | ||
|
||
($ :div {:className "flex flex-col gap-2"} | ||
|
||
(when (not-empty fields) | ||
($ :div {:className "rounded-md border"} | ||
|
||
($ SortableList {:items (cj (map :id fields)) | ||
:onDragEnd (fn [e] (handle-drag-end e fields move))} | ||
($ properties-table {:inputs inputs} | ||
(doall | ||
(map-indexed | ||
(fn [index field] | ||
($ Draggable {:key (:id field) | ||
:id (:id field) | ||
:asChild true} | ||
|
||
($ TableRow {:key (:id field)} | ||
|
||
($ TableCell | ||
($ FormField | ||
{:control (cj control) | ||
:name (str "properties." index (-> inputs (nth 0) :name)) | ||
:render #($ FormItem | ||
($ FormControl | ||
($ Textarea (merge | ||
{:className "min-h-[2.5rem]"} | ||
(-> inputs (nth 0) :props) | ||
(:field (jc %))))))} | ||
|
||
($ FormMessage))) | ||
|
||
($ TableCell | ||
($ FormField | ||
{:control (cj control) | ||
:name (str "properties." index (-> inputs (nth 1) :name)) | ||
:render #($ FormItem | ||
($ FormControl | ||
($ Textarea (merge | ||
{:className "min-h-[2.5rem]"} | ||
(-> inputs (nth 1) :props) | ||
(:field (jc %))))))} | ||
|
||
($ FormMessage))) | ||
|
||
($ TableCell | ||
($ :div {:className "ml-auto flex gap-2"} | ||
($ DragHandle {:id (:id field) | ||
:className "cursor-move"}) | ||
|
||
($ Button {:variant "outline" | ||
:size "icon" | ||
:className "cursor-pointer" | ||
:on-click #(remove index)} | ||
($ Trash {:className "p-1"}))))))) | ||
fields)))))) | ||
|
||
($ :div {:className "flex"} | ||
($ Button {:type "button" | ||
:className "" | ||
:variant "outline" | ||
:on-click #(append (cj {:name "" :value ""}))} | ||
|
||
($ CirclePlus {:className "p-1"}) "Eigenschaft hinzufügen"))))) | ||
|
||
(def ModelProperties | ||
(uix/as-react | ||
(fn [props] | ||
(main props)))) |
Oops, something went wrong.