Skip to content
This repository has been archived by the owner on Jun 5, 2023. It is now read-only.

Commit

Permalink
Merge pull request #145 from 5app/grid-table
Browse files Browse the repository at this point in the history
feat: New component SelectableTable, #143
  • Loading branch information
diondiondion authored Aug 2, 2021
2 parents 31a57e4 + 6fa0828 commit 8403397
Show file tree
Hide file tree
Showing 43 changed files with 28,509 additions and 27,181 deletions.
2 changes: 1 addition & 1 deletion .babelrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"presets": ["@babel/env"],
"presets": ["@babel/preset-env"],
"plugins": [
"@babel/transform-react-jsx",
"@babel/proposal-object-rest-spread",
Expand Down
4 changes: 2 additions & 2 deletions .storybook/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ module.exports = {
'../src/**/*.stories.@(js|jsx|ts|tsx)',
],
addons: [
'@storybook/addon-links',
'@storybook/addon-essentials',
{
name: '@storybook/addon-docs',
options: {
transcludeMarkdown: true,
},
},
'@storybook/addon-essentials',
'@storybook/addon-links',
],
};
2 changes: 1 addition & 1 deletion .storybook/preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import theme from '../src/theme';
import GlobalStyles from './GlobalStyles';

const ThemeProvider = ({children}) => (
<ThemeSection name="page" baseTheme={theme}>
<ThemeSection name="card" baseTheme={theme}>
<GlobalStyles />
{children}
</ThemeSection>
Expand Down
1 change: 0 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ module.exports = {
testEnvironment: 'jsdom',
collectCoverageFrom: [
'**/*.{js}',
'!**/docz/**',
'!**/src-icons/**',
'!**/node_modules/**',
'!**/test/**',
Expand Down
52,427 changes: 25,998 additions & 26,429 deletions package-lock.json

Large diffs are not rendered by default.

16 changes: 10 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,15 @@
"@commitlint/config-conventional": "^12.1.4",
"@semantic-release/changelog": "^5.0.0",
"@semantic-release/git": "^9.0.0",
"@storybook/addon-actions": "^6.2.9",
"@storybook/addon-essentials": "^6.2.9",
"@storybook/addon-links": "^6.2.9",
"@storybook/react": "^6.2.9",
"@storybook/addon-actions": "^6.3.4",
"@storybook/addon-essentials": "^6.3.4",
"@storybook/addon-links": "^6.3.4",
"@storybook/react": "^6.3.4",
"@testing-library/jest-dom": "^5.12.0",
"@testing-library/react": "^12.0.0",
"@testing-library/user-event": "^13.1.9",
"babel-eslint": "^10.1.0",
"babel-jest": "^27.0.6",
"babel-loader": "^8.2.2",
"babel-plugin-styled-components": "^1.10.7",
"eslint": "^7.27.0",
Expand All @@ -66,6 +68,7 @@
"prop-types": "^15.7.2",
"react": "^17.0.0",
"react-dom": "^17.0.0",
"regenerator-runtime": "^0.13.7",
"semantic-release": "^17.4.3",
"styled-components": "^5.2.1"
},
Expand All @@ -76,16 +79,17 @@
},
"dependencies": {
"@popperjs/core": "^2.9.2",
"use-item-list": "0.1.2",
"aria-hidden": "^1.1.1",
"chartist": "^0.11.3",
"classnames": "^2.2.6",
"color2k": "^1.1.1",
"focus-visible": "^5.0.2",
"focusable-selectors": "^0.3.0",
"popper-max-size-modifier": "^0.2.0",
"react-focus-lock": "^2.3.1",
"react-merge-refs": "^1.0.0",
"react-popper": "^2.2.5"
"react-popper": "^2.2.5",
"use-item-list": "0.1.2"
},
"commitlint": {
"extends": [
Expand Down
6 changes: 6 additions & 0 deletions src/ButtonCore/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import classNames from 'classnames';

import 'focus-visible';

import {useTabIndexContext} from '../TabIndexContext';

const Clickable = styled.button`
display: inline-block;
margin: 0;
Expand Down Expand Up @@ -73,9 +75,12 @@ const ButtonCore = forwardRef((props, ref) => {
disabled,
onClick: onClickProp,
className,
tabIndex,
...otherProps
} = props;

const tabIndexContext = useTabIndexContext();

const isDisabled = isDisabledProp || disabled;

const classes = classNames(className, {
Expand Down Expand Up @@ -119,6 +124,7 @@ const ButtonCore = forwardRef((props, ref) => {
? 'false'
: null
}
tabIndex={tabIndex || tabIndexContext}
onClick={onClick}
className={classes}
/>
Expand Down
68 changes: 51 additions & 17 deletions src/ButtonCore/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import {render, cleanup} from '@testing-library/react';
import {render, cleanup, screen} from '@testing-library/react';
import Wrapper from '../../test/helper/wrapper';
import {ThemeSectionError} from '../ThemeSection';
import ButtonCore from '.';
Expand All @@ -15,33 +15,67 @@ describe('ButtonCore', () => {
});
});

it('renders as a button by default', () => {
const {container} = render(
it('renders as a button by default, but can render as a link', () => {
const path = 'path/to/whatever';

render(
<Wrapper>
<ButtonCore id="ok">Ok</ButtonCore>
<ButtonCore>Button label</ButtonCore>
<ButtonCore as="a" href={path} target="_blank">
Regular Link label
</ButtonCore>
<ButtonCore as={RouterLink} to={path}>
Router Link label
</ButtonCore>
</Wrapper>
);

const ok = container.querySelector('#ok');
expect(ok.tagName).toBe('BUTTON');
screen.getByRole('button', {name: /Button label/});

const regularLink = screen.getByRole('link', {
name: /Regular Link label/,
});
expect(regularLink).toHaveAttribute('href', path);
expect(regularLink).toHaveAttribute('target', '_blank');

const routerLink = screen.getByRole('link', {
name: /Router Link label/,
});
expect(routerLink).toHaveAttribute('href', path);
});

it('can render as a RouterLink', () => {
const path = 'path/to/whatever';
const {container} = render(
it('can be disabled both as a button and a link', () => {
render(
<Wrapper>
<ButtonCore id="ok" as={RouterLink} to={path}>
Ok
<ButtonCore isDisabled>Disabled button</ButtonCore>
<ButtonCore
isDisabled
download
as="a"
href="http://example.com/download"
target="_blank"
>
Disabled link
</ButtonCore>
</Wrapper>
);

const ok = container.querySelector('#ok');
expect(ok.tagName).toBe('A');
expect(ok.href).toContain(path);
});
const button = screen.getByRole('button', {name: /Disabled button/});

it('can render as RouterLink', () => {
expect(true).toBe(true);
expect(button).toHaveAttribute('aria-disabled');
expect(button).not.toHaveAttribute('disabled');
expect(button).toHaveAttribute(
'class',
expect.stringContaining('is-disabled')
);

const regularLink = screen.getByText(/Disabled link/);
expect(regularLink).toHaveAttribute(
'class',
expect.stringContaining('is-disabled')
);
expect(regularLink).not.toHaveAttribute('href');
expect(regularLink).not.toHaveAttribute('target');
expect(regularLink).not.toHaveAttribute('download');
});
});
4 changes: 4 additions & 0 deletions src/Checkbox/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
} from '../utils';
import {fillParent} from '../mixins';
import Icon from '../Icon';
import {useTabIndexContext} from '../TabIndexContext';

const noop = () => {};

Expand Down Expand Up @@ -128,10 +129,12 @@ const Checkbox = forwardRef((props, ref) => {
onClick,
scale,
className,
tabIndex,
...otherProps
} = props;

const inputRef = useRef();
const tabIndexContext = useTabIndexContext();

useEffect(() => {
inputRef.current.indeterminate = indeterminate || false;
Expand Down Expand Up @@ -166,6 +169,7 @@ const Checkbox = forwardRef((props, ref) => {
indeterminate={indeterminate}
onChange={indeterminate ? noop : onChange}
onClick={handleClick}
tabIndex={tabIndex || tabIndexContext}
{...otherProps}
/>
<MockBox
Expand Down
11 changes: 10 additions & 1 deletion src/Combobox/Combobox.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import usePopover from '../usePopover';
import usePopoverState from '../usePopoverState';
import useUniqueId from '../useUniqueId';

import {useTabIndexContext} from '../TabIndexContext';
import Portal from '../Portal';
import Status from '../Status';
import VisuallyHidden from '../VisuallyHidden';
Expand Down Expand Up @@ -48,6 +49,7 @@ function Combobox({
const popoverRef = useRef();
const id = useUniqueId(idProp);
const statusMessage = getStatusMessage({resultCount, inputValue});
const tabIndexContext = useTabIndexContext();

const popover = usePopover({
ref: popoverRef,
Expand Down Expand Up @@ -141,7 +143,13 @@ function Combobox({

const highlightedItemId = itemList.useHighlightedItemId();

const getInputProps = ({onChange, onFocus, onKeyDown, ...otherProps}) => ({
const getInputProps = ({
onChange,
onFocus,
onKeyDown,
tabIndex,
...otherProps
}) => ({
...otherProps,
id,
value: inputValue,
Expand All @@ -153,6 +161,7 @@ function Combobox({
'aria-autocomplete': 'list',
autoComplete: 'off',
spellCheck: 'false',
tabIndex: tabIndex || tabIndexContext,
onKeyDown: mergeCallbacks(onKeyDown, handleInputKeyEvents),
onChange: mergeCallbacks(onChange, handleInputChange),
onFocus: mergeCallbacks(onFocus, () => {
Expand Down
1 change: 1 addition & 0 deletions src/Menu/README.stories.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {Meta, Story, Canvas, ArgsTable} from '@storybook/addon-docs/blocks';
import LinkTo from '@storybook/addon-links/react';

import {Menu, MenuButton, MenuList, MenuItem} from './';
import Status from '../Status';

<Meta title="Components/Menu" component={Menu} />

Expand Down
4 changes: 3 additions & 1 deletion src/Select/Select.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import usePopover from '../usePopover';
import usePopoverState from '../usePopoverState';
import useUniqueId from '../useUniqueId';

import {useTabIndexContext} from '../TabIndexContext';
import Button from '../Button';
import * as MenuListUI from '../MenuList';
import PopoverCard from '../PopoverCard';
Expand All @@ -31,6 +32,7 @@ function Select({
const popoverRef = useRef();
const [selectedItem, setSelectedItem] = useState();
const id = useUniqueId(idProp);
const tabIndexContext = useTabIndexContext();

const popover = usePopover({
ref: popoverRef,
Expand Down Expand Up @@ -122,7 +124,7 @@ function Select({
role="combobox"
ref={popover.setReferenceRef}
id={itemList.controllerId}
tabIndex="0"
tabIndex={tabIndexContext || '0'}
aria-labelledby={`${labelledById} ${itemList.controllerId}`}
aria-haspopup="listbox"
aria-autocomplete="none"
Expand Down
51 changes: 28 additions & 23 deletions src/Switch/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import styled, {css} from 'styled-components';
import {alpha, contrast, getDimmedTextColor, pxToRem} from '../utils';

import useMergedRefs from '../useMergedRefs';
import {useTabIndexContext} from '../TabIndexContext';
import OkIcon from '../icons/Ok';

const Input = styled.input`
Expand Down Expand Up @@ -81,30 +82,34 @@ const SwitchIcon = styled(OkIcon)`
`}
`;

const Switch = forwardRef(({checked, disabled, id, ...otherProps}, ref) => {
const inputRef = useRef();
const mergedRefs = useMergedRefs([ref, inputRef]);

function forwardClick(e) {
inputRef.current.click(e);
const Switch = forwardRef(
({checked, disabled, id, tabIndex, ...otherProps}, ref) => {
const inputRef = useRef();
const mergedRefs = useMergedRefs([ref, inputRef]);
const tabIndexContext = useTabIndexContext();

function forwardClick(e) {
inputRef.current.click(e);
}

return (
<Wrapper disabled={disabled} onClick={forwardClick}>
<Input
type="checkbox"
ref={mergedRefs}
id={id}
checked={checked}
disabled={disabled}
tabIndex={tabIndex || tabIndexContext}
{...otherProps}
/>
<Handle>
<SwitchIcon scale={0.8} checked={checked} />
</Handle>
</Wrapper>
);
}

return (
<Wrapper disabled={disabled} onClick={forwardClick}>
<Input
type="checkbox"
ref={mergedRefs}
id={id}
checked={checked}
disabled={disabled}
{...otherProps}
/>
<Handle>
<SwitchIcon scale={0.8} checked={checked} />
</Handle>
</Wrapper>
);
});
);

Switch.displayName = 'Switch';

Expand Down
Loading

0 comments on commit 8403397

Please sign in to comment.