Skip to content

Commit 4cc835a

Browse files
committed
Adds support for text inputs that do not use v-model
1 parent 6140503 commit 4cc835a

File tree

6 files changed

+75
-44
lines changed

6 files changed

+75
-44
lines changed

.github/workflows/tests.yml

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
name: Tests
2+
3+
on: [push]
4+
5+
jobs:
6+
build:
7+
runs-on: ubuntu-latest
8+
9+
strategy:
10+
matrix:
11+
node-version: [10.x, 12.x]
12+
13+
steps:
14+
- uses: actions/checkout@v1
15+
- name: Use Node.js ${{ matrix.node-version }}
16+
uses: actions/setup-node@v1
17+
with:
18+
node-version: ${{ matrix.node-version }}
19+
- name: npm install and test
20+
run: |
21+
npm install
22+
npm test
23+
env:
24+
CI: true

.travis.yml

-12
This file was deleted.

README.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# vue-input-autowidth [![Build Status](https://travis-ci.org/syropian/vue-input-autowidth.svg?branch=master)](https://travis-ci.org/syropian/vue-input-autowidth)
1+
# vue-input-autowidth [![Actions Status](https://github.com/syropian/vue-input-autowidth/workflows/Tests/badge.svg)](https://github.com/syropian/vue-input-autowidth/actions)
22

33
> A Vue.js directive for adjusting a text input's width to fit its content.
44
@@ -39,18 +39,21 @@ Vue.use(VueInputAutowidth)
3939
### Options
4040

4141
#### maxWidth
42+
4243
Type: `String`
4344
Default: `'none'`
4445

4546
The maximum width the input field will grow to.
4647

4748
#### minWidth
49+
4850
Type: `String`
4951
Default: `'none'`
5052

5153
The minimum width the input field will shrink to.
5254

5355
#### comfortZone
56+
5457
Type: `Number`
5558
Default: `0`
5659

__tests__/VueInputAutowidth.test.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { shallowMount } from "@vue/test-utils"
2-
import VueInputAutowidth from "../src"
1+
import { shallowMount } from "@vue/test-utils";
2+
import VueInputAutowidth from "../src";
33

4-
describe("VueTribute", () => {
4+
describe("vue-input-autowidth", () => {
55
it("mounts", () => {
66
const TextInput = {
77
template: `<input type="text" v-model="text" v-autowidth="{maxWidth: '960px', minWidth: '20px', comfortZone: 0}" />`,
@@ -11,11 +11,11 @@ describe("VueTribute", () => {
1111
data() {
1212
return {
1313
text: "Hello World"
14-
}
14+
};
1515
}
16-
}
17-
const wrapper = shallowMount(TextInput)
16+
};
17+
const wrapper = shallowMount(TextInput);
1818

19-
expect(wrapper.find('input[type=text]').element.value).toBe("Hello World")
20-
})
21-
})
19+
expect(wrapper.find("input[type=text]").element.value).toBe("Hello World");
20+
});
21+
});

example/App.vue

+14-5
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,21 @@
1515
<pre v-highlightjs><code class="shell">$ yarn add vue-input-autowidth</code></pre>
1616
<p>or</p>
1717
<pre v-highlightjs><code class="shell">$ npm install --save vue-input-autowidth</code></pre>
18-
<p>or grab it from Unpkg: <a href="https://unpkg.com/vue-input-autowidth">https://unpkg.com/vue-input-autowidth</a></p>
19-
<br>
18+
<p>
19+
or grab it from Unpkg:
20+
<a href="https://unpkg.com/vue-input-autowidth">https://unpkg.com/vue-input-autowidth</a>
21+
</p>
22+
<br />
2023
<h2>Options</h2>
21-
<p><strong>maxWidth | String:</strong> Sets the maximum width the input element will stretch to.</p>
22-
<p><strong>minWidth | String:</strong> Sets the minimum width the input element will shrink to.</p>
23-
<p><strong>comfortZone | Number:</strong> Additional space in pixels to add to the far side of the input's content.</p>
24+
<p>
25+
<strong>maxWidth | String:</strong> Sets the maximum width the input element will stretch to.
26+
</p>
27+
<p>
28+
<strong>minWidth | String:</strong> Sets the minimum width the input element will shrink to.
29+
</p>
30+
<p>
31+
<strong>comfortZone | Number:</strong> Additional space in pixels to add to the far side of the input's content.
32+
</p>
2433
</div>
2534
</div>
2635
</template>

src/vue-input-autowidth.js

+24-17
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
1-
import "es6-object-assign/auto"
2-
import checkWidth from "./check-width"
1+
import "es6-object-assign/auto";
2+
import checkWidth from "./check-width";
33

44
export default {
55
bind: function(el) {
66
if (el.tagName.toLocaleUpperCase() !== "INPUT") {
7-
throw new Error("v-input-autowidth can only be used on input elements.")
7+
throw new Error("v-input-autowidth can only be used on input elements.");
88
}
99
el.dataset.uuid = Math.random()
1010
.toString(36)
11-
.slice(-5)
12-
el.style.boxSizing = "content-box"
11+
.slice(-5);
12+
el.style.boxSizing = "content-box";
1313
},
14-
inserted: function(el, binding) {
15-
const styles = window.getComputedStyle(el)
16-
17-
el.mirror = document.createElement("span")
14+
inserted: function(el, binding, vnode) {
15+
const hasVModel = vnode.data.directives.some(
16+
directive => directive.name === "model"
17+
);
18+
const styles = window.getComputedStyle(el);
19+
el.mirror = document.createElement("div");
1820

1921
Object.assign(el.mirror.style, {
2022
position: "absolute",
@@ -30,19 +32,24 @@ export default {
3032
fontStyle: styles.fontStyle,
3133
letterSpacing: styles.letterSpacing,
3234
textTransform: styles.textTransform
33-
})
35+
});
36+
37+
el.mirror.classList.add(`vue-input-autowidth-mirror-${el.dataset.uuid}`);
38+
el.mirror.setAttribute("aria-hidden", "true");
3439

35-
el.mirror.classList.add(`vue-input-autowidth-mirror-${el.dataset.uuid}`)
36-
el.mirror.setAttribute("aria-hidden", "true")
40+
document.body.appendChild(el.mirror);
3741

38-
document.body.appendChild(el.mirror)
42+
checkWidth(el, binding);
3943

40-
checkWidth(el, binding)
44+
if (!hasVModel) {
45+
el.addEventListener("input", checkWidth.bind(null, el, binding));
46+
}
4147
},
4248
componentUpdated: function(el, binding) {
43-
checkWidth(el, binding)
49+
checkWidth(el, binding);
4450
},
4551
unbind: function(el) {
46-
document.body.removeChild(el.mirror)
52+
document.body.removeChild(el.mirror);
53+
el.removeEventListener("input", checkWidth.bind(null, el, binding));
4754
}
48-
}
55+
};

0 commit comments

Comments
 (0)