Skip to content

Commit 52cc160

Browse files
HDS-4447 Handle empty string and zero values, add examples to show case and tests
1 parent 0116f9c commit 52cc160

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

packages/components/src/modifiers/hds-clipboard.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export const getTextToCopy = (text: TextToCopy): string => {
3737
textToCopy = text.toString();
3838
} else {
3939
assert(
40-
`\`hds-clipboard\` modifier - \`text\` argument must be a string - provided: ${typeof text}`
40+
`\`hds-clipboard\` modifier - \`text\` argument must be a string or number - provided: ${typeof text}`
4141
);
4242
}
4343
}
@@ -109,7 +109,7 @@ export const writeTextToClipboard = async (
109109
): Promise<boolean> => {
110110
// finally copy the text to the clipboard using the Clipboard API
111111
// https://developer.mozilla.org/en-US/docs/Web/API/Clipboard_API
112-
if (textToCopy) {
112+
if (textToCopy || textToCopy === '') {
113113
try {
114114
// notice: the "clipboard-write" permission is granted automatically to pages when they are in the active tab
115115
// https://developer.mozilla.org/en-US/docs/Web/API/Clipboard/write
@@ -151,7 +151,11 @@ export const copyToClipboard = async (
151151
): Promise<boolean> => {
152152
let textToCopy: string = '';
153153

154-
if (text) {
154+
if (text === '') {
155+
textToCopy = '';
156+
} else if (text === 0) {
157+
textToCopy = '0';
158+
} else if (text) {
155159
textToCopy = getTextToCopy(text);
156160
} else if (target) {
157161
const targetElement = getTargetElement(target);

showcase/app/templates/components/copy/button.hbs

+11
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,17 @@
142142
</SF.Item>
143143
</Shw::Flex>
144144

145+
<Shw::Text::H4>Edge cases</Shw::Text::H4>
146+
147+
<Shw::Flex as |SF|>
148+
<SF.Item>
149+
<Hds::Copy::Button @text="Copy an empty string" @textToCopy="" />
150+
</SF.Item>
151+
<SF.Item>
152+
<Hds::Copy::Button @text="Copy the number '0'" @textToCopy={{0}} />
153+
</SF.Item>
154+
</Shw::Flex>
155+
145156
<Shw::Divider @level={{2}} />
146157

147158
<Shw::Text::H3>With <code>target</code> element</Shw::Text::H3>

showcase/tests/integration/components/hds/copy/button/index-test.js

+16
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,22 @@ module('Integration | Component | hds/copy/button/index', function (hooks) {
4646
assert.true(this.success);
4747
});
4848

49+
test('it should copy an empty string value passed in', async function (assert) {
50+
await render(
51+
hbs`<Hds::Copy::Button id="test-copy-button" @text="Copy empty string" @textToCopy="" @onSuccess={{this.onSuccess}} @onError={{this.onError}} />`
52+
);
53+
await click('button#test-copy-button');
54+
assert.true(this.success);
55+
});
56+
57+
test('it should copy a zero number value passed in', async function (assert) {
58+
await render(
59+
hbs`<Hds::Copy::Button id="test-copy-button" @text="Copy zero number value" @textToCopy={{0}} @onSuccess={{this.onSuccess}} @onError={{this.onError}} />`
60+
);
61+
await click('button#test-copy-button');
62+
assert.true(this.success);
63+
});
64+
4965
// @TARGET ARGUMENT
5066

5167
test('it should allow to target an element using a `string` selector for the `@target` argument', async function (assert) {

0 commit comments

Comments
 (0)