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

Feature/select multiple #43

Merged
merged 6 commits into from
Oct 31, 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 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Adds

* Add group widget which is a fieldset container for other form widgets.
* Add multiple and size fields to select widget.

### Fixes

Expand Down
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,25 @@ module.exports = {
};
```

#### select field widget option

The select field widget has an option `allowMultiple` to allow multiple select options to be selected. The default value is `false`.

Once set to `true`, it will add two new fields to the select field widget schema:

| Property | Type | Description |
|---|---|---|
| `allowMultiple` | Boolean | Set to `true` to enable multiple values to be selected in the select widget options, default value is `false` |
| `size` | Integer | Number of options in the list that should be visible, default value is `0` |

```javascript
// modules/@apostrophecms/form-select-field/index.js
modules.exports = {
options: {
allowMultiple: false
}
}
```

### Supporting file field uploads safely

Expand Down
2 changes: 2 additions & 0 deletions i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,10 @@
"recaptchaValidationError": "There was a problem validating your reCAPTCHA verification submission.",
"requiredError": "This field is required",
"select": "Select input",
"selectAllowMultiple": "Allow multiple options to be selected",
"selectBlank": "",
"selectChoice": "Select input options",
"selectSize": "Number of options in the list that should be visible",
"submitLabel": "Submit button label",
"templateOptional": "(Optional)",
"text": "Text input",
Expand Down
83 changes: 61 additions & 22 deletions modules/@apostrophecms/form-select-field-widget/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,55 @@ module.exports = {
extend: '@apostrophecms/form-base-field-widget',
options: {
label: 'aposForm:select',
icon: 'form-select-icon'
icon: 'form-select-icon',
allowMultiple: false
},
fields: {
add: {
choices: {
label: 'aposForm:selectChoice',
type: 'array',
titleField: 'label',
required: true,
fields: {
add: {
label: {
type: 'string',
required: true,
label: 'aposForm:checkboxChoicesLabel',
help: 'aposForm:checkboxChoicesLabelHelp'
},
value: {
type: 'string',
label: 'aposForm:checkboxChoicesValue',
help: 'aposForm:checkboxChoicesValueHelp'
}
fields(self) {
const optionalFields = self.options.allowMultiple
? {
allowMultiple: {
label: 'aposForm:selectAllowMultiple',
type: 'boolean',
def: false
},
size: {
label: 'aposForm:selectSize',
type: 'integer',
def: 0,
min: 0,
if: {
allowMultiple: true
}
}
}
}
: {};

return {
add: {
choices: {
label: 'aposForm:selectChoice',
type: 'array',
titleField: 'label',
required: true,
fields: {
add: {
label: {
type: 'string',
required: true,
label: 'aposForm:checkboxChoicesLabel',
help: 'aposForm:checkboxChoicesLabelHelp'
},
value: {
type: 'string',
label: 'aposForm:checkboxChoicesValue',
help: 'aposForm:checkboxChoicesValueHelp'
}
}
}
},
...optionalFields
}
};
},
methods (self) {
return {
Expand All @@ -38,5 +61,21 @@ module.exports = {
output[widget.fieldName] = self.apos.launder.select(input[widget.fieldName], choices);
}
};
},
extendMethods (self) {
return {
async output(_super, req, widget, options, _with) {
return _super(
req,
{
...widget,
allowMultiple: (self.options.allowMultiple && widget.allowMultiple) ?? false,
size: widget.size ?? 0
},
options,
_with
);
}
};
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<select
class="apos-form-input {{ prependIfPrefix('__input') }}" id="{{ id }}"
name="{{ widget.fieldName}}" {% if widget.required %}required{% endif %}
{% if widget.allowMultiple %}multiple {% if widget.size %}size="{{ widget.size }}"{% endif %}{% endif %}
>
<option value="">{{ __t("aposForm:selectBlank") }}</option>
{% for choice in widget.choices %}
Expand Down