diff --git a/package.json b/package.json index 07e041254..b6851ddaa 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@netdata/netdata-ui", - "version": "5.6.0", + "version": "5.6.1", "description": "netdata UI kit", "main": "dist/index.js", "module": "dist/es6/index.js", @@ -23,6 +23,7 @@ "build": "yarn build:umd && yarn build:cjs && yarn build:es6", "clean": "rimraf lib dist es coverage", "test": "jest --config ./jest/config.js", + "lint": "eslint src", "cp-cloud": "cp -R ./dist ../cloud-frontend/node_modules/@netdata/netdata-ui/", "to-cloud": "yarn build:cjs && yarn build:es6 && yarn cp-cloud", "cp-charts": "cp -R ./dist ../charts/node_modules/@netdata/netdata-ui/", @@ -73,6 +74,7 @@ "@babel/plugin-transform-runtime": "^7.29.0", "@babel/preset-env": "^7.29.0", "@babel/preset-react": "^7.28.5", + "@eslint/js": "^9.39.4", "@playwright/test": "1.62.1", "@storybook/addon-docs": "10.2.10", "@storybook/addon-links": "10.2.10", @@ -87,7 +89,7 @@ "babel-plugin-styled-components": "^2.1.4", "clean-webpack-plugin": "^4.0.0", "copyfiles": "^2.4.1", - "eslint": "^10.0.0", + "eslint": "^9.39.4", "eslint-plugin-react": "^7.37.5", "eslint-plugin-react-hooks": "^7.0.0", "eslint-plugin-storybook": "10.2.10", diff --git a/src/components/button/README.md b/src/components/button/README.md index ca9749fbb..95856be47 100644 --- a/src/components/button/README.md +++ b/src/components/button/README.md @@ -48,3 +48,43 @@ const myButton: Button = ( + +``` + +### Surface appearance + +` + ) + const button = screen.getByRole("button", { name: "Filter 3" }) + expect(button.children).toHaveLength(2) + expect(button).toHaveStyleRule("font-weight", "400", { modifier: "&&" }) + expect(button).toHaveStyleRule("height", "26px", { modifier: "&&" }) + expect(button).toHaveStyleRule("min-height", "26px", { modifier: "&&" }) + expect(button).toHaveStyleRule("width", "80px", { modifier: "&&" }) + expect(button).toHaveStyleRule("font-size", "12px", { modifier: "&&" }) + expect(button).toHaveStyleRule("gap", "6px", { modifier: "&&" }) + expect(button).toHaveStyleRule("justify-content", "start", { modifier: "&&" }) + expect(button).toHaveStyleRule( + "background-color", + DefaultTheme.colors.buttonNeutralHollowPressed, + { + modifier: '&&[aria-pressed="true"]', + } + ) + }) +}) + +it.each([true, false])( + "preserves its element label during loading (wrapChildren: %s)", + wrapChildren => { + const label = Save + const { rerender } = renderWithProviders() + const labelElement = screen.getByTestId("loading-label") + + rerender( + + ) + expect(screen.getByTestId("loading-label")).toBe(labelElement) + + rerender() + expect(screen.getByTestId("loading-label")).toBe(labelElement) + } +) + +it("updates a text-only composed action after page translation", () => { + const { rerender } = renderWithProviders() + const button = screen.getByRole("button") + const translated = document.createElement("font") + translated.textContent = button.textContent + button.firstChild.replaceWith(translated) + rerender() + expect(button.textContent).toBe("Restore default") +}) + +it("keeps dimension objects for minimum widths and bounded heights", () => { + renderWithProviders( + , + { theme } + ) + const button = screen.getByRole("button", { name: "Action" }) + const modifier = '&&:hover:not(:disabled):not([aria-disabled="true"])' + expect(button).toHaveStyleRule("filter", "brightness(1.08)", { modifier }) + expect(button).toHaveStyleRule("border-color", theme.colors.textFocus, { modifier }) + expect(button).toHaveStyleRule("border-color", theme.colors.controlBorder, { + modifier: "&&:active", + }) + } +) + +it("lets a disabled native control retain its own pointer target without activating", async () => { + const onClick = jest.fn() + renderWithProviders( + + ) + const button = screen.getByRole("button", { name: "Disabled" }) + expect(button).toHaveStyleRule("pointer-events", "auto", { modifier: "&&" }) + expect(button).toBeDisabled() + await userEvent.setup().click(button) + expect(onClick).not.toHaveBeenCalled() +}) + +it("keeps an aria-disabled pointer-blocked control in keyboard navigation", async () => { + renderWithProviders( + + ) + const button = screen.getByRole("button", { name: "Blocked" }) + expect(button).toHaveStyleRule("pointer-events", "none", { modifier: "&&" }) + await userEvent.setup().tab() + expect(button).toHaveFocus() +}) + +it("preserves default pointer behavior without adding hover effects", () => { + const { rerender } = renderWithProviders() + const button = screen.getByRole("button", { name: "Default" }) + expect(button).toHaveStyleRule("pointer-events", "auto", { modifier: "&&" }) + expect(button).not.toHaveStyleRule("filter", undefined, { + modifier: '&&:hover:not(:disabled):not([aria-disabled="true"])', + }) + rerender() + expect(button).toHaveStyleRule("pointer-events", "none", { modifier: "&&" }) +}) + +it.each([DefaultTheme, DarkTheme])( + "keeps pressed semantics separate from persistent painting in $name", + theme => { + renderWithProviders( + <> + + + + + , + { theme } + ) + const semantic = screen.getByRole("button", { name: "Semantic pressed" }) + expect(semantic).toHaveAttribute("aria-pressed", "true") + expect(semantic).toHaveStyle({ backgroundColor: theme.colors.primary }) + expect(semantic).not.toHaveStyleRule("background-color", undefined, { + modifier: '&&[aria-pressed="true"]', + }) + expect(semantic).toHaveStyleRule("background-color", theme.colors.primaryPressed, { + modifier: "&&:active", + }) + expect(screen.getByRole("button", { name: "Default pressed" })).toHaveStyle({ + backgroundColor: theme.colors.primaryPressed, + }) + expect(screen.getByRole("button", { name: "Explicit active" })).toHaveStyle({ + backgroundColor: theme.colors.primaryPressed, + }) + expect(screen.getByRole("button", { name: "Expanded" })).toHaveStyle({ + backgroundColor: theme.colors.primaryPressed, + }) + } +) diff --git a/src/components/button/layoutProps.test.js b/src/components/button/layoutProps.test.js new file mode 100644 index 000000000..9816ef3e3 --- /dev/null +++ b/src/components/button/layoutProps.test.js @@ -0,0 +1,93 @@ +import React from "react" +import styled from "styled-components" +import { renderWithProviders, screen } from "testUtilities" +import { Button } from "./button" +import IconButton from "./iconButton" + +it("keeps unwrapped styling metadata off default buttons without unknown props", () => { + const errors = jest.spyOn(console, "error") + renderWithProviders( + <> + + ) + expect(screen.getByTestId("attention-count")).toHaveStyle({ marginLeft: "2px" }) + expect(screen.getByRole("button")).not.toHaveStyleRule("margin-left", expect.any(String), { + modifier: "&&>span", + }) +}) + +it("retains the existing icon-to-label gap for default and explicit wrapped labels", () => { + renderWithProviders( + <> + + ))} + + ) + } + + renderWithProviders() + const keyboard = user() + await keyboard.tab() + await keyboard.keyboard("{ArrowUp}{ArrowLeft}") + expect(screen.getByRole("radio", { name: "Filter", checked: true })).toHaveFocus() +}) diff --git a/src/components/checkbox/README.md b/src/components/checkbox/README.md index cd7f791db..f65a14fcc 100644 --- a/src/components/checkbox/README.md +++ b/src/components/checkbox/README.md @@ -15,6 +15,8 @@ interface CheckboxProps { className?: string disabled?: boolean indeterminate?: boolean + variant?: "default" | "filled" + inputProps?: React.InputHTMLAttributes } ``` @@ -67,3 +69,35 @@ type UseCheckboxesList = ( handlers: Array ) => [boolean, boolean, () => void] ``` + +### Filled row-selection checkboxes + +`variant="filled"` preserves the 16px row-selection control: a 1.5px border, 4px radius, +9px checkmark, and a 9×2px indeterminate dash. Dimensions derive from the theme size unit. +The off state uses `inputBg` and `controlBorder`; checked and mixed states use +`buttonNeutralFill` and `buttonNeutralText`. Hover uses `buttonNeutralFill` and keyboard +focus uses `segmentedFocus`. The default appearance is unchanged. + +`inputProps` passes native input attributes and events to the real checkbox, including +`id`, `aria-label`, `aria-invalid`, `aria-describedby`, `title`, `autoFocus`, and test IDs. +The controlled `checked`, `disabled`, `readOnly` and internal ref remain owned by Checkbox. +`onChange(nextChecked, event)` retains the existing boolean-first callback contract. +Indeterminate state remains the native `input.indeterminate` property. + +For a control inside an existing label, use `as="span"` on the Checkbox wrapper to avoid +nested labels. The input remains a native checkbox and Space toggles it. Clicking the filled +visual box also focuses its native input. + +```jsx + +``` diff --git a/src/components/checkbox/checkbox.d.ts b/src/components/checkbox/checkbox.d.ts new file mode 100644 index 000000000..5421577c7 --- /dev/null +++ b/src/components/checkbox/checkbox.d.ts @@ -0,0 +1,34 @@ +import { + ElementType, + FC, + HTMLAttributes, + InputHTMLAttributes, + ReactNode, + SyntheticEvent, +} from "react" +import { FlexProps } from "@/components/templates/flex" + +export type CheckboxProps = FlexProps & + Omit, keyof FlexProps | "onChange" | "onClick"> & { + checked?: boolean + disabled?: boolean + indeterminate?: boolean + variant?: "default" | "filled" + label?: ReactNode + Label?: ElementType + labelPosition?: "left" | "right" + labelProps?: Record + checkboxProps?: Record + iconProps?: Record + inputProps?: Omit< + InputHTMLAttributes, + "checked" | "disabled" | "readOnly" + > & { + "data-testid"?: string + } + onChange?: (checked: boolean, event: SyntheticEvent) => void + } + +declare const Checkbox: FC + +export { Checkbox } diff --git a/src/components/checkbox/checkbox.js b/src/components/checkbox/checkbox.js index 8c3b27062..27afa468c 100644 --- a/src/components/checkbox/checkbox.js +++ b/src/components/checkbox/checkbox.js @@ -12,6 +12,8 @@ export const Checkbox = ( disabled, iconProps, checkboxProps = {}, + inputProps = {}, + variant = "default", indeterminate, Label = Text, label, @@ -34,6 +36,7 @@ export const Checkbox = ( if (e.target !== inputRef.current) e.preventDefault() if (disabled) return + if (variant === "filled" && e.target !== inputRef.current) inputRef.current?.focus() onChange?.(!checked, e) } @@ -55,6 +58,7 @@ export const Checkbox = ( - {(!!checked || !!indeterminate) && ( + {variant !== "filled" && (!!checked || !!indeterminate) && ( screen.getByTestId("checkbox-input") describe("Checkbox visuals", () => { it("draws the unchecked box as a hairline with no fill", () => { renderWithProviders() - expect(box()).toHaveStyleRule("border", "1px solid #708585") + expect(box()).toHaveStyleRule("border", `1px solid ${DefaultTheme.colors.textLite}`) expect(box()).toHaveStyleRule("background-color", "rgba(255,255,255,0.0)") expect(box()).toHaveStyleRule("border-radius", "2px") }) @@ -74,7 +74,7 @@ describe("Checkbox visuals", () => { it("steps the border when the row is hovered", () => { renderWithProviders() - expect(box()).toHaveStyleRule("border-color", "#526161", { modifier: "label:hover &" }) + expect(box()).toHaveStyleRule("border-color", DefaultTheme.colors.text, { modifier: "label:hover &" }) }) it("keeps the box still on hover when disabled", () => { diff --git a/src/components/checkbox/filled.test.js b/src/components/checkbox/filled.test.js new file mode 100644 index 000000000..c15932a53 --- /dev/null +++ b/src/components/checkbox/filled.test.js @@ -0,0 +1,100 @@ +import React, { useState } from "react" +import { renderWithProviders, screen, user } from "testUtilities" +import { DefaultTheme } from "@/theme/default" +import { DarkTheme } from "@/theme/dark" +import { Checkbox } from "." + +const FilledCheckbox = props => +const box = () => screen.getByTestId("styled-checkbox") + +it("places native names, validation and focus callbacks on the input", async () => { + const onFocus = jest.fn() + const onBlur = jest.fn() + renderWithProviders( + + ) + const input = screen.getByRole("checkbox", { name: "Select jobs" }) + expect(input).toHaveAttribute("id", "select_jobs") + expect(input).toHaveAttribute("aria-invalid", "true") + expect(input).toHaveAttribute("aria-describedby", "selection_hint") + expect(input).toHaveAttribute("title", "Select jobs") + expect(input).toHaveAttribute("data-testid", "row-checkbox") + expect(input.parentElement.parentElement.tagName).toBe("SPAN") + await user().tab() + expect(input).toHaveFocus() + expect(onFocus).toHaveBeenCalledTimes(1) + await user().tab() + expect(onBlur).toHaveBeenCalledTimes(1) +}) + +it.each([ + ["light", DefaultTheme], + ["dark", DarkTheme], +])("preserves filled checkbox geometry and palette in %s", (_, theme) => { + const { rerender } = renderWithProviders(, { theme }) + expect(box()).toHaveStyleRule("width", "16px") + expect(box()).toHaveStyleRule("height", "16px") + expect(box()).toHaveStyleRule("border-radius", "4px") + expect(box()).toHaveStyleRule("border", `1.5px solid ${theme.colors.controlBorder}`) + expect(box()).toHaveStyleRule("background-color", theme.colors.inputBg) + rerender() + expect(box()).toHaveStyleRule("background-color", theme.colors.buttonNeutralFill) + expect(box()).toHaveStyleRule("border", `1.5px solid ${theme.colors.buttonNeutralFill}`) + expect(box()).toHaveStyleRule("width", "9px", { modifier: ":after" }) + expect(box()).toHaveStyleRule("height", "9px", { modifier: ":after" }) + expect(box()).toHaveStyleRule("background-color", theme.colors.buttonNeutralText, { + modifier: ":after", + }) + rerender() + expect(screen.getByRole("checkbox")).toBePartiallyChecked() + expect(box()).toHaveStyleRule("height", "2px", { modifier: ":after" }) + expect(box()).toHaveStyleRule("clip-path", "none", { modifier: ":after" }) +}) + +const Controlled = props => { + const [checked, setChecked] = useState(false) + return ( + + ) +} + +it("keeps an outer label clickable and supports Space once per action", async () => { + renderWithProviders() + const input = screen.getByRole("checkbox", { name: "Choose all" }) + await user().click(screen.getByText("Choose all")) + expect(input).toBeChecked() + await user().tab() + input.focus() + await user().keyboard(" ") + expect(input).not.toBeChecked() +}) + +it("focuses the filled native checkbox when its visible box is clicked", async () => { + renderWithProviders() + await user().click(box()) + expect(screen.getByRole("checkbox", { name: "Select job" })).toHaveFocus() +}) + +it("hovers a standalone filled box without requiring a label ancestor", () => { + const { rerender } = renderWithProviders() + expect(box().closest("label")).toBeNull() + expect(box()).toHaveStyleRule("border-color", DefaultTheme.colors.buttonNeutralFill, { + modifier: ":hover", + }) + rerender() + expect(box()).not.toHaveStyleRule("border-color", expect.anything(), { modifier: ":hover" }) +}) diff --git a/src/components/checkbox/index.d.ts b/src/components/checkbox/index.d.ts new file mode 100644 index 000000000..094334036 --- /dev/null +++ b/src/components/checkbox/index.d.ts @@ -0,0 +1,18 @@ +import { ComponentPropsWithRef, ReactElement } from "react" + +export { Checkbox } from "./checkbox" +export type { CheckboxProps } from "./checkbox" + +export declare function useCheckboxesList( + values: boolean[], + handlers: Array<(checked: boolean) => void> +): [boolean, boolean, () => void] + +export type CheckboxIndicatorProps = Omit, "children"> & { + checked?: boolean + shape?: "checkbox" | "radio" + size?: number | string + markSize?: number +} + +export declare function CheckboxIndicator(props: CheckboxIndicatorProps): ReactElement diff --git a/src/components/checkbox/index.js b/src/components/checkbox/index.js index c3f37cba1..c8920e53f 100644 --- a/src/components/checkbox/index.js +++ b/src/components/checkbox/index.js @@ -1,2 +1,3 @@ export { Checkbox } from "./checkbox" export { useCheckboxesList } from "./hooks" +export { CheckboxIndicator } from "./indicator" diff --git a/src/components/checkbox/indicator.js b/src/components/checkbox/indicator.js new file mode 100644 index 000000000..0a66d35d0 --- /dev/null +++ b/src/components/checkbox/indicator.js @@ -0,0 +1,25 @@ +import React, { forwardRef } from "react" +import { Checkbox as StyledCheckbox } from "./styled" + +export const CheckboxIndicator = forwardRef(function CheckboxIndicator( + { checked, shape = "checkbox", size = 4, markSize, ...rest }, + ref +) { + return ( +