Skip to content

Commit f47a23b

Browse files
authored
feat(Checkbox): [JELLY-8594] add label slot (#514)
1 parent 4b3b183 commit f47a23b

File tree

7 files changed

+62
-16
lines changed

7 files changed

+62
-16
lines changed

package-lock.json

+7-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/core-components/src/components/checkbox/__snapshots__/checkbox.spec.tsx.snap

+18-6
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ exports[`should apply an error style if it is marked as invalid. 1`] = `
77
<div class="b2b-checkbox-items">
88
<input class="b2b-checkbox__input" type="checkbox">
99
<b2b-input-label>
10-
test
10+
<slot name="label">
11+
test
12+
</slot>
1113
</b2b-input-label>
1214
</div>
1315
<span>
@@ -25,7 +27,9 @@ exports[`should disable the checkbox 1`] = `
2527
<div class="b2b-checkbox-items">
2628
<input class="b2b-checkbox__input" disabled="" type="checkbox">
2729
<b2b-input-label disabled="">
28-
test
30+
<slot name="label">
31+
test
32+
</slot>
2933
</b2b-input-label>
3034
</div>
3135
</div>
@@ -40,7 +44,9 @@ exports[`should render a hint if invalid and disabled are true at the same time
4044
<div class="b2b-checkbox-items">
4145
<input class="b2b-checkbox__input" disabled="" type="checkbox">
4246
<b2b-input-label disabled="">
43-
test
47+
<slot name="label">
48+
test
49+
</slot>
4450
</b2b-input-label>
4551
</div>
4652
<span>
@@ -58,7 +64,9 @@ exports[`should render a hint message if a hint string is specified 1`] = `
5864
<div class="b2b-checkbox-items">
5965
<input class="b2b-checkbox__input" type="checkbox">
6066
<b2b-input-label>
61-
test
67+
<slot name="label">
68+
test
69+
</slot>
6270
</b2b-input-label>
6371
</div>
6472
<span>
@@ -76,7 +84,9 @@ exports[`should render an error message if an error is specified and it is marke
7684
<div class="b2b-checkbox-items">
7785
<input class="b2b-checkbox__input" type="checkbox">
7886
<b2b-input-label>
79-
test
87+
<slot name="label">
88+
test
89+
</slot>
8090
</b2b-input-label>
8191
</div>
8292
<span>
@@ -94,7 +104,9 @@ exports[`should render the checkbox 1`] = `
94104
<div class="b2b-checkbox-items">
95105
<input class="b2b-checkbox__input" type="checkbox">
96106
<b2b-input-label>
97-
test
107+
<slot name="label">
108+
test
109+
</slot>
98110
</b2b-input-label>
99111
</div>
100112
</div>

packages/core-components/src/components/checkbox/checkbox.docs.mdx

+9-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import * as CheckboxStories from './checkbox.stories';
2424

2525
The label of the checkbox which appears on the right hand side next to it. This is the only
2626
required property. It is not the same as the checkbox value, which indicates its programmatical
27-
value.
27+
value. If you need custom content like bold text or a link, use the named slot label.
2828

2929
### Required
3030

@@ -85,6 +85,14 @@ checkbox is currently checked.
8585
checked: boolean;
8686
}`
8787

88+
## Slots
89+
90+
### Label
91+
92+
If you need the label to contain something other than plain text you may use the label slot like so:
93+
94+
<Canvas of={CheckboxStories.story070LabelSlot} sourceState="shown" />
95+
8896
## Attributes
8997

9098
<ArgsTable story={PRIMARY_STORY} />

packages/core-components/src/components/checkbox/checkbox.e2e.tsx

+11
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,15 @@ describe('b2b-checkbox', () => {
4444

4545
expect(element).toHaveClass('b2b-checkbox--checked');
4646
});
47+
48+
it('use label slot if provided', async () => {
49+
const page = await newE2EPage();
50+
await page.setContent(
51+
'<b2b-checkbox><span slot="label">Custom label</span></b2b-checkbox>',
52+
);
53+
54+
const element = await page.find('span');
55+
56+
expect(element).toEqualText('Custom label');
57+
});
4758
});

packages/core-components/src/components/checkbox/checkbox.stories.tsx

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Meta, Story } from '@storybook/web-components';
1+
import { Meta, Story, StoryObj } from '@storybook/web-components';
22
import { html } from 'lit-html';
33
import { getArgTypes } from '../../docs/config/utils';
44

@@ -82,6 +82,14 @@ story060Required.args = {
8282
};
8383
story060Required.storyName = 'Required';
8484

85+
export const story070LabelSlot: StoryObj = {
86+
args: {
87+
...defaultArgs,
88+
},
89+
render: ({}) =>
90+
html`<b2b-checkbox><span slot="label">Label Slot</span></b2b-input>`,
91+
};
92+
8593
const controls = {};
8694
const inputCheckboxArgs = getArgTypes('b2b-checkbox', controls);
8795

packages/core-components/src/components/checkbox/checkbox.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ export class CheckboxComponent {
168168
id={this.name}
169169
required={this.required}
170170
disabled={this.disabled}>
171-
{this.label}
171+
<slot name="label">{this.label}</slot>
172172
</b2b-input-label>
173173
)}
174174
</div>

packages/core-components/src/html/form.html

+7
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,13 @@
321321
hint="I am a hint"></b2b-checkbox>
322322
<b2b-checkbox
323323
label="Checkbox with a very long label. Checkbox with a very long label. Checkbox with a very long label. Checkbox with a very long label."></b2b-checkbox>
324+
<b2b-checkbox
325+
><span slot="label"
326+
>This is a custom label
327+
<b2b-icon-100
328+
icon="b2b_icon-paper-plane"
329+
size="24"></b2b-icon-100></span
330+
></b2b-checkbox>
324331
</div>
325332

326333
<div class="content-container">

0 commit comments

Comments
 (0)