Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Checkbox): react/wc parity #18329

Merged
merged 10 commits into from
Feb 12, 2025
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ import WarningAltFilled16 from '@carbon/icons/lib/warning--alt--filled/16.js';
import CDSCheckbox from './checkbox';
import styles from './checkbox.scss?lit';
import { carbonElement as customElement } from '../../globals/decorators/carbon-element';
import { CHECKBOX_ORIENTATION } from './defs';

export { CHECKBOX_ORIENTATION };
/**
* Check box.
*
Expand Down Expand Up @@ -70,6 +72,12 @@ class CDSCheckboxGroup extends LitElement {
@property({ type: String, reflect: true, attribute: 'legend-text' })
legendText;

/**
* Provide the text to be rendered inside of the fieldset <legend>
*/
@property({ type: String, reflect: true, attribute: 'orientation' })
orientation = CHECKBOX_ORIENTATION.VERTICAL;

/**
* Whether the CheckboxGroup should be read-only
*/
Expand Down Expand Up @@ -119,7 +127,7 @@ class CDSCheckboxGroup extends LitElement {
updated(changedProperties) {
const { selectorCheckbox } = this.constructor as typeof CDSCheckboxGroup;
const checkboxes = this.querySelectorAll(selectorCheckbox);
['disabled', 'readonly'].forEach((name) => {
['disabled', 'readonly', 'orientation'].forEach((name) => {
if (changedProperties.has(name)) {
const { [name as keyof CDSCheckboxGroup]: value } = this;
// Propagate the property to descendants until `:host-context()` gets supported in all major browsers
Expand Down Expand Up @@ -149,6 +157,7 @@ class CDSCheckboxGroup extends LitElement {
invalidText,
legendId,
legendText,
orientation,
readonly,
warn,
warnText,
Expand Down Expand Up @@ -177,6 +186,8 @@ class CDSCheckboxGroup extends LitElement {
[`${prefix}--checkbox-group--invalid`]: !readonly && invalid,
[`${prefix}--checkbox-group--warning`]: showWarning,
[`${prefix}--checkbox-group--slug`]: hasAILabel,
[`${prefix}--checkbox-group--${orientation}`]:
orientation === 'horizontal',
});

return html`
Expand All @@ -186,7 +197,8 @@ class CDSCheckboxGroup extends LitElement {
?disabled=${disabled}
aria-readonly=${readonly}
?aria-labelledby=${ariaLabelledBy || legendId}
?aria-describedby=${!invalid && !warn && helper ? helperId : undefined}>
?aria-describedby=${!invalid && !warn && helper ? helperId : undefined}
orientation=${orientation}>
<legend class="${prefix}--label" id=${legendId || ariaLabelledBy}>
${legendText}
<slot name="ai-label" @slotchange="${handleSlotChange}"></slot>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ $css--plex: true !default;

:host(#{$prefix}-checkbox-group) {
display: flex;
inline-size: 100%;
}

:host(#{$prefix}-checkbox-group) .#{$prefix}--checkbox-group--slug,
Expand All @@ -90,3 +91,10 @@ $css--plex: true !default;
:host(#{$prefix}-checkbox[ai-label]) ::slotted(#{$prefix}-slug) {
align-self: center;
}

:host(#{$prefix}-checkbox-group)
.#{$prefix}--checkbox-group--horizontal
::slotted(#{$prefix}-checkbox) {
flex: none;
margin-inline-end: 1rem;
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import FolderOpen16 from '@carbon/icons/lib/folder--open/16.js';
import Folders16 from '@carbon/icons/lib/folders/16.js';
import '../ai-label/index';
import './index';
import { CHECKBOX_ORIENTATION } from './defs';

const checkboxLabel = 'Checkbox label';

Expand All @@ -26,6 +27,7 @@ const defaultArgs = {
readonly: false,
warn: false,
warnText: 'Warn message goes here',
orientation: 'vertical',
};

const controls = {
Expand All @@ -50,6 +52,11 @@ const controls = {
control: 'textNullable',
description: 'Provide the text to be rendered inside of the fieldset.',
},
orientation: {
control: 'select',
description: 'Provide how checkbox should be displayed.',
options: CHECKBOX_ORIENTATION,
},
readonly: {
control: 'boolean',
description: 'Specify whether the checkbox group is read-only.',
Expand Down Expand Up @@ -135,7 +142,7 @@ const actions = html`
export const WithAILabel = {
render: () => html`
<div style="width: 400px">
<cds-checkbox-group legend-text="Group label">
<cds-checkbox-group legend-text="Group label" orientation="vertical">
<cds-ai-label alignment="bottom-left">
${content}${actions}</cds-ai-label
>
Expand All @@ -144,7 +151,7 @@ export const WithAILabel = {
<cds-checkbox>Checkbox label</cds-checkbox>
</cds-checkbox-group>

<cds-checkbox-group legend-text="Group label">
<cds-checkbox-group legend-text="Group label" orientation="vertical">
<cds-checkbox>
Checkbox label
<cds-ai-label alignment="bottom-left">
Expand All @@ -160,7 +167,7 @@ export const WithAILabel = {
<cds-checkbox>Checkbox label</cds-checkbox>
</cds-checkbox-group>

<cds-checkbox-group legend-text="Group label">
<cds-checkbox-group legend-text="Group label" orientation="vertical">
<cds-checkbox>
Checkbox label
<cds-ai-label alignment="bottom-left" kind="inline">
Expand Down Expand Up @@ -190,6 +197,7 @@ export const Playground = {
invalid,
invalidText,
legendText,
orientation,
warn,
warnText,
}) => html`
Expand All @@ -199,6 +207,7 @@ export const Playground = {
?invalid="${invalid}"
invalid-text="${invalidText}"
legend-text="${legendText}"
orientation="${orientation}"
?readonly="${readonly}"
?warn="${warn}"
warn-text="${warnText}">
Expand Down
22 changes: 22 additions & 0 deletions packages/web-components/src/components/checkbox/defs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* @license
*
* Copyright IBM Corp. 2025
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
/**
* How to lay out radio buttons.
*/
export enum CHECKBOX_ORIENTATION {
/**
* Laying out radio buttons horizontally.
*/
HORIZONTAL = 'horizontal',

/**
* Laying out radio buttons vertically.
*/
VERTICAL = 'vertical',
}
Loading