Skip to content

Commit 53ee90e

Browse files
authored
Merge pull request #43 from apostrophecms/feature/select-multiple
Feature/select multiple
2 parents 82fe878 + 7d64919 commit 53ee90e

File tree

5 files changed

+84
-22
lines changed

5 files changed

+84
-22
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
### Adds
66

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

910
### Fixes
1011

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,25 @@ module.exports = {
103103
};
104104
```
105105

106+
#### select field widget option
107+
108+
The select field widget has an option `allowMultiple` to allow multiple select options to be selected. The default value is `false`.
109+
110+
Once set to `true`, it will add two new fields to the select field widget schema:
111+
112+
| Property | Type | Description |
113+
|---|---|---|
114+
| `allowMultiple` | Boolean | Set to `true` to enable multiple values to be selected in the select widget options, default value is `false` |
115+
| `size` | Integer | Number of options in the list that should be visible, default value is `0` |
116+
117+
```javascript
118+
// modules/@apostrophecms/form-select-field/index.js
119+
modules.exports = {
120+
options: {
121+
allowMultiple: false
122+
}
123+
}
124+
```
106125

107126
### Supporting file field uploads safely
108127

i18n/en.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,10 @@
8484
"recaptchaValidationError": "There was a problem validating your reCAPTCHA verification submission.",
8585
"requiredError": "This field is required",
8686
"select": "Select input",
87+
"selectAllowMultiple": "Allow multiple options to be selected",
8788
"selectBlank": "",
8889
"selectChoice": "Select input options",
90+
"selectSize": "Number of options in the list that should be visible",
8991
"submitLabel": "Submit button label",
9092
"templateOptional": "(Optional)",
9193
"text": "Text input",

modules/@apostrophecms/form-select-field-widget/index.js

Lines changed: 61 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,55 @@ module.exports = {
22
extend: '@apostrophecms/form-base-field-widget',
33
options: {
44
label: 'aposForm:select',
5-
icon: 'form-select-icon'
5+
icon: 'form-select-icon',
6+
allowMultiple: false
67
},
7-
fields: {
8-
add: {
9-
choices: {
10-
label: 'aposForm:selectChoice',
11-
type: 'array',
12-
titleField: 'label',
13-
required: true,
14-
fields: {
15-
add: {
16-
label: {
17-
type: 'string',
18-
required: true,
19-
label: 'aposForm:checkboxChoicesLabel',
20-
help: 'aposForm:checkboxChoicesLabelHelp'
21-
},
22-
value: {
23-
type: 'string',
24-
label: 'aposForm:checkboxChoicesValue',
25-
help: 'aposForm:checkboxChoicesValueHelp'
26-
}
8+
fields(self) {
9+
const optionalFields = self.options.allowMultiple
10+
? {
11+
allowMultiple: {
12+
label: 'aposForm:selectAllowMultiple',
13+
type: 'boolean',
14+
def: false
15+
},
16+
size: {
17+
label: 'aposForm:selectSize',
18+
type: 'integer',
19+
def: 0,
20+
min: 0,
21+
if: {
22+
allowMultiple: true
2723
}
2824
}
2925
}
30-
}
26+
: {};
27+
28+
return {
29+
add: {
30+
choices: {
31+
label: 'aposForm:selectChoice',
32+
type: 'array',
33+
titleField: 'label',
34+
required: true,
35+
fields: {
36+
add: {
37+
label: {
38+
type: 'string',
39+
required: true,
40+
label: 'aposForm:checkboxChoicesLabel',
41+
help: 'aposForm:checkboxChoicesLabelHelp'
42+
},
43+
value: {
44+
type: 'string',
45+
label: 'aposForm:checkboxChoicesValue',
46+
help: 'aposForm:checkboxChoicesValueHelp'
47+
}
48+
}
49+
}
50+
},
51+
...optionalFields
52+
}
53+
};
3154
},
3255
methods (self) {
3356
return {
@@ -38,5 +61,21 @@ module.exports = {
3861
output[widget.fieldName] = self.apos.launder.select(input[widget.fieldName], choices);
3962
}
4063
};
64+
},
65+
extendMethods (self) {
66+
return {
67+
async output(_super, req, widget, options, _with) {
68+
return _super(
69+
req,
70+
{
71+
...widget,
72+
allowMultiple: (self.options.allowMultiple && widget.allowMultiple) ?? false,
73+
size: widget.size ?? 0
74+
},
75+
options,
76+
_with
77+
);
78+
}
79+
};
4180
}
4281
};

modules/@apostrophecms/form-select-field-widget/views/widget.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
<select
1818
class="apos-form-input {{ prependIfPrefix('__input') }}" id="{{ id }}"
1919
name="{{ widget.fieldName}}" {% if widget.required %}required{% endif %}
20+
{% if widget.allowMultiple %}multiple {% if widget.size %}size="{{ widget.size }}"{% endif %}{% endif %}
2021
>
2122
<option value="">{{ __t("aposForm:selectBlank") }}</option>
2223
{% for choice in widget.choices %}

0 commit comments

Comments
 (0)