Skip to content

Commit

Permalink
[O2B-532] Minor improvements to selection dropdown
Browse files Browse the repository at this point in the history
  • Loading branch information
martinboulais committed Jan 7, 2025
1 parent d467d32 commit c7b05e2
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
import { SelectionDropdownModel } from '../../../common/selection/dropdown/SelectionDropdownModel.js';

const numericalComparisonOptions = Object.freeze([
{ label: '<', value: 'lt' },
{ label: '<=', value: 'le' },
{ label: '=', value: 'eq' },
{ label: '>=', value: 'ge' },
{ label: '>', value: 'gt' },
{ value: '<', selector: 'lt' },
{ value: '<=', selector: 'le' },
{ value: '=', selector: 'eq' },
{ value: '>=', selector: 'ge' },
{ value: '>', selector: 'gt' },
]);

/**
Expand Down
10 changes: 10 additions & 0 deletions lib/public/components/common/selection/SelectionModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { Observable, RemoteData } from '/js/src/index.js';
* @property {number|string} value The id of the object this is used to see if it is checked.
* @property {Component} [label] The representation of the option (if null, value is used as label)
* @property {string} [rawLabel] The string only representation of the option, useful if the label is not a string
* @property {string} [selector] If the value of the option is not a valid CSS, this is used to define option's id
*/

/**
Expand Down Expand Up @@ -277,6 +278,15 @@ export class SelectionModel extends Observable {
return this.selected[0];
}

/**
* States if the selection allows for multiple options to be chosen at the same time
*
* @return {boolean} true if multiple options are allowed
*/
get multiple() {
return this._multiple;

Check warning on line 287 in lib/public/components/common/selection/SelectionModel.js

View check run for this annotation

Codecov / codecov/patch

lib/public/components/common/selection/SelectionModel.js#L286-L287

Added lines #L286 - L287 were not covered by tests
}

/**
* Return the list of currently selected options
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,21 +132,26 @@ export const selectionDropdown = (selectionDropdownModel, configuration) => {
const selectorPrefix = cleanPrefix(configuration.selectorPrefix);

if (displayOption === null) {
displayOption = (option, checked, onChange, selectorPrefix) => h(
'label.dropdown-option.form-check-label.flex-row.g2.ph2.pv1',
{ key: option.value },
[
h(
`input#${selectorPrefix}dropdown-option-${option.value}`,
{
type: 'checkbox',
checked,
onchange: (e) => onChange(option, e.target.checked),
},
),
option.label || option.value,
],
);
displayOption = (option, checked, onChange, selectorPrefix) => {
const selector = option.selector ?? option.value;

Check warning on line 136 in lib/public/components/common/selection/dropdown/selectionDropdown.js

View check run for this annotation

Codecov / codecov/patch

lib/public/components/common/selection/dropdown/selectionDropdown.js#L135-L136

Added lines #L135 - L136 were not covered by tests

return h(

Check warning on line 138 in lib/public/components/common/selection/dropdown/selectionDropdown.js

View check run for this annotation

Codecov / codecov/patch

lib/public/components/common/selection/dropdown/selectionDropdown.js#L138

Added line #L138 was not covered by tests
'label.dropdown-option.form-check-label.flex-row.g2.ph2.pv1',
{ key: selector },
[
h(
`input#${selectorPrefix}dropdown-option-${selector}`,
{
type: selectionDropdownModel.multiple ? 'checkbox' : 'radio',
name: `${selectorPrefix}dropdown-option-${selectionDropdownModel.multiple ? selector : 'group'}`,

Check warning on line 146 in lib/public/components/common/selection/dropdown/selectionDropdown.js

View check run for this annotation

Codecov / codecov/patch

lib/public/components/common/selection/dropdown/selectionDropdown.js#L145-L146

Added lines #L145 - L146 were not covered by tests
checked,
onchange: (e) => onChange(option, e.target.checked),

Check warning on line 148 in lib/public/components/common/selection/dropdown/selectionDropdown.js

View check run for this annotation

Codecov / codecov/patch

lib/public/components/common/selection/dropdown/selectionDropdown.js#L148

Added line #L148 was not covered by tests
},
),
option.label || option.value,

Check warning on line 151 in lib/public/components/common/selection/dropdown/selectionDropdown.js

View check run for this annotation

Codecov / codecov/patch

lib/public/components/common/selection/dropdown/selectionDropdown.js#L151

Added line #L151 was not covered by tests
],
);
};
}

if (displaySelectionItem === null) {
Expand Down

0 comments on commit c7b05e2

Please sign in to comment.