Skip to content

Commit 40737a1

Browse files
author
Dongming Huang
committed
Auto refresh select width when the attribute is auto
1 parent b2aa5fa commit 40737a1

4 files changed

Lines changed: 64 additions & 5 deletions

File tree

site/website/static/templates/custom-dropdown-width.html

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<script>
22
init({
33
title: 'Custom Dropdown Width',
4-
desc: 'Use <code>dropWidth: 580</code> option to custom the dropdown width.',
4+
desc: 'Use <code>dropWidth: 580</code> option to custom the dropdown width, or use <code>width: \'auto\'</code> option to auto adjust the width based on the selected text.',
55
links: ['multiple-select.min.css'],
66
scripts: ['multiple-select.min.js']
77
})
@@ -13,7 +13,7 @@
1313
</label>
1414

1515
<div class="col-sm-10">
16-
<select multiple="multiple">
16+
<select name="basic" multiple="multiple">
1717
<option value="1">1</option>
1818
<option value="2">2</option>
1919
<option value="3">3</option>
@@ -47,15 +47,34 @@
4747
</select>
4848
</div>
4949
</div>
50+
51+
<div class="mb-3 row">
52+
<label class="col-sm-2">
53+
Auto width
54+
</label>
55+
56+
<div class="col-sm-10">
57+
<select name="auto-width">
58+
<option value="1">First</option>
59+
<option value="2">Second</option>
60+
<option value="3">Third</option>
61+
<option value="4">Fourth</option>
62+
</select>
63+
</div>
64+
</div>
5065
</div>
5166

5267
<script>
5368
function mounted() {
54-
$('select').multipleSelect({
69+
$('select[name="basic"]').multipleSelect({
5570
multiple: true,
5671
width: 500,
5772
multipleWidth: 70,
5873
dropWidth: 580
5974
})
75+
76+
$('select[name="auto-width"]').multipleSelect({
77+
width: 'auto'
78+
})
6079
}
6180
</script>

src/MultipleSelect.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -739,7 +739,9 @@ class MultipleSelect {
739739
computedWidth = this.$el.outerWidth() + 20
740740
}
741741

742-
this.$parent.css('width', this.options.width || computedWidth)
742+
if (this.options.width !== 'auto') {
743+
this.$parent.css('width', this.options.width || computedWidth)
744+
}
743745

744746
this.$el.show().addClass('ms-offscreen')
745747
}
@@ -861,6 +863,21 @@ class MultipleSelect {
861863
$span.prop('title', this.getSelects('text'))
862864
}
863865

866+
if (this.options.width === 'auto') {
867+
if (!this._autoWidthCanvas) {
868+
this._autoWidthCanvas = document.createElement('canvas')
869+
this._autoWidthContext = this._autoWidthCanvas.getContext('2d')
870+
}
871+
872+
const context = this._autoWidthContext
873+
const styles = window.getComputedStyle ? window.getComputedStyle($span[0]) : $span[0].style
874+
875+
context.font = `${styles.fontWeight || 'normal'} ${styles.fontSize || '16px'} ${styles.fontFamily || 'sans-serif'}`
876+
const textWidth = context.measureText($span.text()).width
877+
878+
this.$parent.css('width', textWidth + 38)
879+
}
880+
864881
// set selects to select
865882
this.$el.val(this.getSelects())
866883

vue-examples/src/assets/MS.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@
100100
{
101101
"title": "Custom Dropdown Width",
102102
"url": "custom-dropdown-width.html",
103-
"description": "Use <code>dropWidth: 580</code> option to custom the dropdown width."
103+
"description": "Use <code>dropWidth: 580</code> option to custom the dropdown width, or use <code>width: 'auto'</code> option to auto adjust the width based on the selected text."
104104
},
105105
{
106106
"title": "Max Height",

vue-examples/src/examples/CustomDropdownWidth.vue

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,29 @@
104104
</MultipleSelect>
105105
</div>
106106
</div>
107+
108+
<div class="form-group row">
109+
<label class="col-sm-2">
110+
Auto width
111+
</label>
112+
113+
<div class="col-sm-10">
114+
<MultipleSelect width="auto">
115+
<option value="1">
116+
First
117+
</option>
118+
<option value="2">
119+
Second
120+
</option>
121+
<option value="3">
122+
Third
123+
</option>
124+
<option value="4">
125+
Fourth
126+
</option>
127+
</MultipleSelect>
128+
</div>
129+
</div>
107130
</div>
108131
</template>
109132

0 commit comments

Comments
 (0)