Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support default values for multi-choices type #137

Merged
merged 2 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/Changelog.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
2.5.0 (unreleased)
------------------

- #137 Support default values for multi-choices type
- #135 Fix non-UID keyed folder items can not be pre-selected by the server
- #134 Fix APIError for non-UID listings
- #133 Multiselect with duplicates support for interim fields
Expand Down

Large diffs are not rendered by default.

52 changes: 42 additions & 10 deletions webpack/app/components/MultiChoice.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@ class MultiChoice extends React.Component
###*
* Multi-Choice Field for the Listing Table
*
* A multi select field is identified by the column type "multichoice" in the listing
* view, e.g. `self.columns = {"Result": {"type": "multichoice"}, ... }`
* A multi choice field is identified by the column type "multichoice" in the
* listing view, e.g. `self.columns = {"Result": {"type": "multichoice"}, ... }`
*
###
constructor: (props) ->
super(props)

# remember the initial value
@state =
value: props.defaultValue

# bind event handler to the current context
@on_change = @on_change.bind @

Expand All @@ -30,28 +34,56 @@ class MultiChoice extends React.Component
uid = el.getAttribute("uid")
# Extract the column_key attribute
name = el.getAttribute("column_key") or el.name
# Prepare a list of UIDs
# Store the new values
value = (input.value for input in checked)
@setState
value: value

console.debug "MultiChoice::on_change: value=#{value}"

# Call the *update* field handler
if @props.update_editable_field
@props.update_editable_field uid, name, value, @props.item

###
* Converts the value to an array
###
to_array: (value) ->
if not value
return []
if Array.isArray(value)
return value
parsed = JSON.parse value
if not Array.isArray(parsed)
# This might happen when a default value is set, e.g. 0
return [parsed]
return parsed


###*
* Select options builder
* Checkboxes list builder. Generates a list of checkboxes made of the
* options passed-in. The values are the ids of the options to be selected
* @param values {array} list of selected ResultValues
* @param options {array} list of option objects, e.g.:
* {"ResultText": ..., "ResultValue": ...}
###
build_options: ->
options = []
build_checkboxes: ->
checkboxes = []

# Convert the result to an array
values = @to_array @state.value

# filter out empties
values = values.filter (value) -> value isnt ""

# ensure safe comparison (strings)
values = values.map (value) -> value.toString()

for option in @props.options
value = option.ResultValue
title = option.ResultText
selected = option.selected or no
options.push(
selected = value.toString() in values
checkboxes.push(
<li key={value}>
<input type="checkbox"
defaultChecked={selected}
Expand All @@ -65,13 +97,13 @@ class MultiChoice extends React.Component
{...@props.attrs}/> {title}
</li>)

return options
return checkboxes

render: ->
<div className={@props.field_css or "multichoice"}>
{@props.before and <span className={@props.before_css or "before_field"} dangerouslySetInnerHTML={{__html: @props.before}}></span>}
<ul className="list-unstyled">
{@build_options()}
{@build_checkboxes()}
</ul>
{@props.after and <span className={@props.after_css or "after_field"} dangerouslySetInnerHTML={{__html: @props.after}}></span>}
</div>
Expand Down
15 changes: 2 additions & 13 deletions webpack/app/components/TableCell.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -690,23 +690,11 @@ class TableCell extends React.Component
column_key = props.column_key or @get_column_key()
item = props.item or @get_item()
name = props.name or @get_name()

value = props.value or @get_value()
# convert value to array
if value.length > 0
value = JSON.parse value

options = props.options or item.choices[column_key] or []
# mark selected options
options.forEach (option) ->
selected = no
if Array.isArray value
selected = value.indexOf(option.ResultValue) > -1
option.selected = selected

formatted_value = props.formatted_value or @get_formatted_value()
uid = props.uid or @get_uid()
title = props.title or @props.column.title or column_key
options = props.options or item.choices[column_key] or []

column = props.column or @get_column()
item.help ?= {}
Expand All @@ -728,6 +716,7 @@ class TableCell extends React.Component
uid={uid}
item={item}
name={fieldname}
defaultValue={value}
column_key={column_key}
title={title}
help={help}
Expand Down