Add fields when clicking in the dropdown

Remove the additional button
This commit is contained in:
Eike Kettner 2020-11-22 00:07:03 +01:00
parent cc6db61a3a
commit 1ee36cef8f

View File

@ -26,10 +26,7 @@ import Util.Maybe
type alias Model = type alias Model =
{ fieldModels : Dict String Comp.CustomFieldInput.Model { fieldModels : Dict String Comp.CustomFieldInput.Model
, fieldSelect : , fieldSelect : FieldSelect
{ selected : Maybe CustomField
, dropdown : Comp.FixedDropdown.Model CustomField
}
, visibleFields : List CustomField , visibleFields : List CustomField
, availableFields : List CustomField , availableFields : List CustomField
} }
@ -51,13 +48,16 @@ type FieldResult
| FieldCreateNew | FieldCreateNew
type alias FieldSelect =
{ selected : Maybe CustomField
, dropdown : Comp.FixedDropdown.Model CustomField
}
initWith : List CustomField -> Model initWith : List CustomField -> Model
initWith fields = initWith fields =
{ fieldModels = Dict.empty { fieldModels = Dict.empty
, fieldSelect = , fieldSelect = mkFieldSelect fields
{ selected = List.head fields
, dropdown = Comp.FixedDropdown.init (List.map mkItem fields)
}
, visibleFields = [] , visibleFields = []
, availableFields = fields , availableFields = fields
} }
@ -75,6 +75,13 @@ initCmd flags =
Api.getCustomFields flags "" CustomFieldResp Api.getCustomFields flags "" CustomFieldResp
mkFieldSelect : List CustomField -> FieldSelect
mkFieldSelect fields =
{ selected = Nothing
, dropdown = Comp.FixedDropdown.init (List.map mkItem fields)
}
--- Update --- Update
@ -103,10 +110,7 @@ update msg model =
model_ = model_ =
{ model { model
| availableFields = list.items | availableFields = list.items
, fieldSelect = , fieldSelect = mkFieldSelect list.items
{ selected = List.head list.items
, dropdown = Comp.FixedDropdown.init (List.map mkItem list.items)
}
} }
in in
UpdateResult model_ Cmd.none Sub.none NoResult UpdateResult model_ Cmd.none Sub.none NoResult
@ -130,7 +134,12 @@ update msg model =
} }
} }
in in
UpdateResult model_ Cmd.none Sub.none NoResult case sel of
Just field ->
update (ApplyField field) model
Nothing ->
UpdateResult model_ Cmd.none Sub.none NoResult
ApplyField f -> ApplyField f ->
let let
@ -146,12 +155,20 @@ update msg model =
visible = visible =
f :: model.visibleFields f :: model.visibleFields
fSelect =
mkFieldSelect avail
-- have to re-state the open menu when this is invoked
-- from a click in the dropdown
fSelectDropdown =
fSelect.dropdown
dropdownOpen =
{ fSelectDropdown | menuOpen = True }
model_ = model_ =
{ model { model
| fieldSelect = | fieldSelect = { fSelect | dropdown = dropdownOpen }
{ selected = List.head avail
, dropdown = Comp.FixedDropdown.init (List.map mkItem avail)
}
, availableFields = avail , availableFields = avail
, visibleFields = visible , visibleFields = visible
, fieldModels = Dict.insert f.name fm model.fieldModels , fieldModels = Dict.insert f.name fm model.fieldModels
@ -174,10 +191,7 @@ update msg model =
{ model { model
| availableFields = avail | availableFields = avail
, visibleFields = visible , visibleFields = visible
, fieldSelect = , fieldSelect = mkFieldSelect avail
{ selected = List.head avail
, dropdown = Comp.FixedDropdown.init (List.map mkItem avail)
}
} }
in in
UpdateResult model_ Cmd.none Sub.none (FieldValueRemove f) UpdateResult model_ Cmd.none Sub.none (FieldValueRemove f)
@ -237,18 +251,6 @@ viewMenuBar model =
div [ class "ui action input field" ] div [ class "ui action input field" ]
[ Html.map FieldSelectMsg [ Html.map FieldSelectMsg
(Comp.FixedDropdown.viewStyled "fluid" (Maybe.map mkItem selected) dropdown) (Comp.FixedDropdown.viewStyled "fluid" (Maybe.map mkItem selected) dropdown)
, a
[ class "ui primary icon button"
, href "#"
, case selected of
Just f ->
onClick (ApplyField f)
Nothing ->
class "disabled"
]
[ i [ class "check icon" ] []
]
, addFieldLink "" model , addFieldLink "" model
] ]