From 7b372926289d0c1cdab76dbea9cb298e7594dfa9 Mon Sep 17 00:00:00 2001 From: Mikkel Laursen Date: Sat, 11 Jul 2020 18:19:46 -0600 Subject: [PATCH] fix: ListItem disabled states The ListItem will now correctly apply the disabled states as needed and also prevent the item from being clicked while disabled. --- packages/list/src/SimpleListItem.tsx | 10 +++++++ packages/list/src/__tests__/ListItem.tsx | 20 ++++++++++++- .../list/src/__tests__/SimpleListItem.tsx | 28 ++++++++++++++++++- packages/list/src/_mixins.scss | 4 ++- 4 files changed, 59 insertions(+), 3 deletions(-) diff --git a/packages/list/src/SimpleListItem.tsx b/packages/list/src/SimpleListItem.tsx index 2e882f5aba..973ca8af31 100644 --- a/packages/list/src/SimpleListItem.tsx +++ b/packages/list/src/SimpleListItem.tsx @@ -1,3 +1,4 @@ +/* eslint-disable jsx-a11y/click-events-have-key-events, jsx-a11y/role-supports-aria-props */ import React, { forwardRef } from "react"; import cn from "classnames"; import { bem } from "@react-md/utils"; @@ -36,6 +37,8 @@ const SimpleListItem = forwardRef( height: propHeight = "auto", threeLines = false, clickable = false, + onClick, + disabled = false, ...props }, ref @@ -48,19 +51,25 @@ const SimpleListItem = forwardRef( rightAddonType, secondaryText, }); + const { "aria-disabled": ariaDisabled } = props; + const isDisabled = + disabled || ariaDisabled === "true" || ariaDisabled === true; return (
  • { rerender(Left Icon}>Content); expect(container).toMatchSnapshot(); }); + + it('should correctly "polyfill" the disabled behavior by preventing click events when disabled', () => { + const onClick = jest.fn(); + const props = { children: "Content", onClick }; + const { rerender, getByRole } = render(); + + const item = getByRole("button"); + expect(item).toHaveAttribute("aria-disabled", "true"); + expect(item.className).toContain("rmd-list-item--disabled"); + fireEvent.click(item); + expect(onClick).not.toBeCalled(); + + rerender(); + expect(item).toHaveAttribute("aria-disabled", "true"); + expect(item.className).toContain("rmd-list-item--disabled"); + fireEvent.click(item); + expect(onClick).not.toBeCalled(); + }); }); diff --git a/packages/list/src/__tests__/SimpleListItem.tsx b/packages/list/src/__tests__/SimpleListItem.tsx index dabca683a3..85a52a9c92 100644 --- a/packages/list/src/__tests__/SimpleListItem.tsx +++ b/packages/list/src/__tests__/SimpleListItem.tsx @@ -1,5 +1,5 @@ import React from "react"; -import { render } from "@testing-library/react"; +import { render, fireEvent } from "@testing-library/react"; import SimpleListItem from "../SimpleListItem"; @@ -13,4 +13,30 @@ describe("SimpleListItem", () => { rerender(Left} />); expect(container).toMatchSnapshot(); }); + + it('should correctly "polyfill" the disabled behavior by preventing click events when disabled', () => { + const onClick = jest.fn(); + const props = { children: "Content", onClick, clickable: true }; + const { rerender, getByText } = render( + + ); + + const item = getByText(props.children); + expect(item).toHaveAttribute("aria-disabled", "true"); + expect(item.className).toContain("rmd-list-item--disabled"); + fireEvent.click(item); + expect(onClick).not.toBeCalled(); + + rerender(); + expect(item).toHaveAttribute("aria-disabled", "true"); + expect(item.className).toContain("rmd-list-item--disabled"); + fireEvent.click(item); + expect(onClick).not.toBeCalled(); + + rerender(); + expect(item).not.toHaveAttribute("aria-disabled"); + expect(item.className).not.toContain("rmd-list-item--disabled"); + fireEvent.click(item); + expect(onClick).toBeCalled(); + }); }); diff --git a/packages/list/src/_mixins.scss b/packages/list/src/_mixins.scss index 3d2d900106..c517e86a58 100644 --- a/packages/list/src/_mixins.scss +++ b/packages/list/src/_mixins.scss @@ -153,8 +153,10 @@ @include rmd-states-surface; } - &[aria-disabled] { + &--disabled { @include rmd-theme(color, text-disabled-on-background); + + pointer-events: none; } &--link {