Skip to content

Commit

Permalink
feat: support target to be set dynamically (#379)
Browse files Browse the repository at this point in the history
  • Loading branch information
jkusa authored Oct 31, 2022
1 parent 8e1aa28 commit 66bf3a7
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ http://jkusa.github.io/ember-cli-clipboard
### Arguments

- `text` - string value or action that returns a string to be copied
- `target` - selector string of element from which to copy text
- `target` - selector string of element or action that returns an element from which to copy text
- `action` - string value of operation: `copy` or `cut` (default is copy)
- `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".
- `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`
Expand Down
5 changes: 5 additions & 0 deletions tests/dummy/app/controllers/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ export default class ApplicationController extends Controller {
return uuidv1();
}

@action
getTarget() {
return document.querySelector('#url-text');
}

@action
getSuccessMessage(type) {
return {
Expand Down
21 changes: 21 additions & 0 deletions tests/dummy/app/templates/application.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,27 @@
&lt;/CopyButton&gt;</CodeBlock>
</DocSection>

<DocSection @title="Get Text From Dynamic Target" as |showMessage|>
<input id="url-text" class="application__textfield" type="text" value="https://jkusa.github.io/ember-cli-clipboard">
<CopyButton
class="application__copy-button"
@target={{this.getTarget}}
@onSuccess={{pipe (fn this.getSuccessMessage "copy") showMessage}}
@onError={{pipe (fn this.getErrorMessage "copy") showMessage}}
>
Copy URL
</CopyButton>
{{! template-lint-disable block-indentation no-unbalanced-curlies}}
<CodeBlock>&lt;input id="url" type="text" value="https://jkusa.github.io/ember-cli-clipboard/"&gt;
&lt;CopyButton
@target=\{{this.getTarget}}
@onSuccess=\{{this.onSuccess}}
@onError=\{{this.onError}}
&gt;
Copy URL
&lt;/CopyButton&gt;</CodeBlock>
</DocSection>

<DocSection @title="Cut Text From Target Element" as |showMessage|>
<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>
<CopyButton
Expand Down
2 changes: 1 addition & 1 deletion tests/fastboot/fastboot-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module('FastBoot | fastboot', function (hooks) {
await visit('/');
assert
.dom('button.copy-btn')
.exists({ count: 4 }, '`<CopyButton>` renders in FastBoot');
.exists({ count: 5 }, '`<CopyButton>` renders in FastBoot');
assert
.dom('.application__supported-text')
.hasText(
Expand Down
21 changes: 20 additions & 1 deletion tests/integration/components/copy-button-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ module('Integration | Component | copy button', function (hooks) {
test('components renders text', async function (assert) {
assert.expect(2);

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

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

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

test('dynamic target', async function (assert) {
assert.expect(1);

this.getTarget = () => {
assert.ok(true);
return document.querySelector('#url-text');
};

await render(hbs`
<input id="url-text" type="text" value="https://github.com/jkusa/ember-cli-clipboard">
<CopyButton
@target={{this.getTarget}}
>
Click To Copy
</CopyButton>
`);
await click('.copy-btn');
});

test('attributeBindings', async function (assert) {
assert.expect(8);

Expand Down

0 comments on commit 66bf3a7

Please sign in to comment.