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

Workaround for :has CSS selector that nwsapi/jest can't parse #7981

Merged
merged 2 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export const euiCheckboxStyles = (euiThemeContext: UseEuiTheme) => {
${controlStyles.input.fauxInput}
border-radius: ${euiTheme.border.radius.small};
`,
hasLabel: controlStyles.input.hasLabel, // Skip css`` className generation
enabled: {
selected: css(controlStyles.input.enabled.selected),
unselected: css(controlStyles.input.enabled.unselected),
Expand Down
1 change: 1 addition & 0 deletions packages/eui/src/components/form/checkbox/checkbox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export const EuiCheckbox: FunctionComponent<EuiCheckboxProps> = ({
const styles = useEuiMemoizedStyles(euiCheckboxStyles);
const inputStyles = [
styles.input.euiCheckbox__square,
!!label && styles.input.hasLabel,
disabled
? checked || indeterminate
? styles.input.disabled.selected
Expand Down
7 changes: 3 additions & 4 deletions packages/eui/src/components/form/form.styles.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -258,10 +258,6 @@ describe('euiFormCustomControlStyles', () => {
justify-content: center;
align-items: center;

&:has(+ label) {
margin-block-start: 4px;
}

&:has(input:focus-visible) {
outline: 2px solid #07C;
outline-offset: 2px;
Expand All @@ -273,6 +269,9 @@ describe('euiFormCustomControlStyles', () => {
transition-timing-function: ease-in;
}
",
"hasLabel": "
margin-block-start: 4px;
",
"hiddenInput": "
position: absolute;
inset: 0;
Expand Down
10 changes: 6 additions & 4 deletions packages/eui/src/components/form/form.styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -388,10 +388,6 @@ export const euiFormCustomControlStyles = (euiThemeContext: UseEuiTheme) => {
justify-content: center;
align-items: center;

&:has(+ label) {
${logicalCSS('margin-top', centerWithLabel)}
}

&:has(input:focus-visible) {
outline: ${euiTheme.focus.width} solid ${controlVars.colors.selected};
outline-offset: ${euiTheme.focus.width};
Expand All @@ -403,6 +399,12 @@ export const euiFormCustomControlStyles = (euiThemeContext: UseEuiTheme) => {
transition-timing-function: ${controlVars.animation.easing};
}
`,
// TODO: Revert https://github.com/elastic/eui/pull/7981
// once https://github.com/dperini/nwsapi/issues/123
// has been fixed, and restore `&:has(+ label)` selector
hasLabel: `
${logicalCSS('margin-top', centerWithLabel)}
`,
enabled: {
selected: `
color: ${controlVars.colors.selectedIcon};
Expand Down
1 change: 1 addition & 0 deletions packages/eui/src/components/form/radio/radio.styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export const euiRadioStyles = (euiThemeContext: UseEuiTheme) => {
${controlStyles.input.fauxInput}
border-radius: 50%;
`,
hasLabel: controlStyles.input.hasLabel, // Skip css`` className generation
enabled: {
selected: css(controlStyles.input.enabled.selected),
unselected: css(controlStyles.input.enabled.unselected),
Expand Down
11 changes: 11 additions & 0 deletions packages/eui/src/components/form/radio/radio.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/

import React from 'react';
import { fireEvent } from '@testing-library/react';
import { requiredProps } from '../../../test';
import { shouldRenderCustomStyles } from '../../../test/internal';
import { render } from '../../../test/rtl';
Expand Down Expand Up @@ -72,5 +73,15 @@ describe('EuiRadio', () => {

expect(container.firstChild).toMatchSnapshot();
});

test('onChange is fired', () => {
const onChange = jest.fn();
const { getByRole } = render(
<EuiRadio id="id" onChange={onChange} label="test" />
);
fireEvent.click(getByRole('radio', { name: 'test' }));

expect(onChange).toHaveBeenCalledTimes(1);
});
});
});
1 change: 1 addition & 0 deletions packages/eui/src/components/form/radio/radio.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export const EuiRadio: FunctionComponent<EuiRadioProps> = ({
const styles = useEuiMemoizedStyles(euiRadioStyles);
const inputStyles = [
styles.input.euiRadio__circle,
!!label && styles.input.hasLabel,
disabled
? checked
? styles.input.disabled.selected
Expand Down
Loading