Skip to content

Commit 66bf3a7

Browse files
authored
feat: support target to be set dynamically (#379)
1 parent 8e1aa28 commit 66bf3a7

File tree

5 files changed

+48
-3
lines changed

5 files changed

+48
-3
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ http://jkusa.github.io/ember-cli-clipboard
5151
### Arguments
5252

5353
- `text` - string value or action that returns a string to be copied
54-
- `target` - selector string of element from which to copy text
54+
- `target` - selector string of element or action that returns an element from which to copy text
5555
- `action` - string value of operation: `copy` or `cut` (default is copy)
5656
- `container` - selector string or element object of containing element. "For use in Bootstrap Modals or with any other library that changes the focus you'll want to set the focused element as the container value".
5757
- `delegateClickEvent` - clipboard.js defaults event listeners to the body in order to reduce memory footprint if there are hundreds of event listeners on a page. If you want to scope the event listener to the copy button, set this property to `false`

tests/dummy/app/controllers/application.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ export default class ApplicationController extends Controller {
88
return uuidv1();
99
}
1010

11+
@action
12+
getTarget() {
13+
return document.querySelector('#url-text');
14+
}
15+
1116
@action
1217
getSuccessMessage(type) {
1318
return {

tests/dummy/app/templates/application.hbs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,27 @@
8080
&lt;/CopyButton&gt;</CodeBlock>
8181
</DocSection>
8282

83+
<DocSection @title="Get Text From Dynamic Target" as |showMessage|>
84+
<input id="url-text" class="application__textfield" type="text" value="https://jkusa.github.io/ember-cli-clipboard">
85+
<CopyButton
86+
class="application__copy-button"
87+
@target={{this.getTarget}}
88+
@onSuccess={{pipe (fn this.getSuccessMessage "copy") showMessage}}
89+
@onError={{pipe (fn this.getErrorMessage "copy") showMessage}}
90+
>
91+
Copy URL
92+
</CopyButton>
93+
{{! template-lint-disable block-indentation no-unbalanced-curlies}}
94+
<CodeBlock>&lt;input id="url" type="text" value="https://jkusa.github.io/ember-cli-clipboard/"&gt;
95+
&lt;CopyButton
96+
@target=\{{this.getTarget}}
97+
@onSuccess=\{{this.onSuccess}}
98+
@onError=\{{this.onError}}
99+
&gt;
100+
Copy URL
101+
&lt;/CopyButton&gt;</CodeBlock>
102+
</DocSection>
103+
83104
<DocSection @title="Cut Text From Target Element" as |showMessage|>
84105
<textarea id="textarea" class="application__textfield" rows="3">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. </textarea>
85106
<CopyButton

tests/fastboot/fastboot-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module('FastBoot | fastboot', function (hooks) {
88
await visit('/');
99
assert
1010
.dom('button.copy-btn')
11-
.exists({ count: 4 }, '`<CopyButton>` renders in FastBoot');
11+
.exists({ count: 5 }, '`<CopyButton>` renders in FastBoot');
1212
assert
1313
.dom('.application__supported-text')
1414
.hasText(

tests/integration/components/copy-button-test.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ module('Integration | Component | copy button', function (hooks) {
5656
test('components renders text', async function (assert) {
5757
assert.expect(2);
5858

59-
await render(hbs`{{copy-button}}`);
59+
await render(hbs`<CopyButton />`);
6060

6161
assert.dom('*').hasText('', 'Component renders no text without block');
6262

@@ -177,6 +177,25 @@ module('Integration | Component | copy button', function (hooks) {
177177
.doesNotHaveAttribute('disabled', 'disabled correctly bound to type');
178178
});
179179

180+
test('dynamic target', async function (assert) {
181+
assert.expect(1);
182+
183+
this.getTarget = () => {
184+
assert.ok(true);
185+
return document.querySelector('#url-text');
186+
};
187+
188+
await render(hbs`
189+
<input id="url-text" type="text" value="https://github.com/jkusa/ember-cli-clipboard">
190+
<CopyButton
191+
@target={{this.getTarget}}
192+
>
193+
Click To Copy
194+
</CopyButton>
195+
`);
196+
await click('.copy-btn');
197+
});
198+
180199
test('attributeBindings', async function (assert) {
181200
assert.expect(8);
182201

0 commit comments

Comments
 (0)