Skip to content

Commit

Permalink
Workaround for :has CSS selector that nwsapi/jest can't parse (#7981)
Browse files Browse the repository at this point in the history
  • Loading branch information
cee-chen authored Aug 26, 2024
1 parent b69409b commit 0ae8fc8
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 8 deletions.
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

0 comments on commit 0ae8fc8

Please sign in to comment.