Skip to content

Commit

Permalink
feat: adds checkbox (#202)
Browse files Browse the repository at this point in the history
  • Loading branch information
eltongarbin committed Oct 1, 2020
1 parent 291dcd6 commit 72c5dce
Show file tree
Hide file tree
Showing 8 changed files with 990 additions and 1,144 deletions.
54 changes: 27 additions & 27 deletions packages/ocean-components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,36 +34,36 @@
},
"devDependencies": {
"@rollup/plugin-commonjs": "^14.0.0",
"@rollup/plugin-node-resolve": "^8.1.0",
"@storybook/addon-a11y": "^6.0.0-rc.24",
"@storybook/addon-backgrounds": "^6.0.0-rc.24",
"@storybook/addon-controls": "^6.0.0-rc.24",
"@storybook/addon-docs": "^6.0.0-rc.24",
"@storybook/addon-viewport": "^6.0.0-rc.24",
"@storybook/react": "^6.0.0-rc.24",
"@rollup/plugin-node-resolve": "^8.4.0",
"@storybook/addon-a11y": "^6.0.1",
"@storybook/addon-backgrounds": "^6.0.1",
"@storybook/addon-controls": "^6.0.1",
"@storybook/addon-docs": "^6.0.1",
"@storybook/addon-viewport": "^6.0.1",
"@storybook/react": "^6.0.1",
"@storybook/storybook-deployer": "^2.8.6",
"@testing-library/jest-dom": "^5.10.1",
"@testing-library/react": "^10.3.0",
"@testing-library/jest-dom": "^5.11.2",
"@testing-library/react": "^10.4.8",
"@types/classnames": "2.2.10",
"@types/jest": "^26.0.0",
"@types/node": "^14.0.14",
"@types/react": "^16.9.38",
"@types/jest": "^26.0.9",
"@types/node": "^14.0.27",
"@types/react": "^16.9.46",
"@types/react-dom": "^16.9.8",
"@types/react-router-dom": "^5.1.5",
"@types/rollup-plugin-postcss": "^2.0.0",
"@typescript-eslint/eslint-plugin": "^3.3.0",
"@typescript-eslint/parser": "^3.3.0",
"@typescript-eslint/eslint-plugin": "^3.9.0",
"@typescript-eslint/parser": "^3.9.0",
"@useblu/tokens": "^2.0.0",
"eslint": "^7.5.0",
"eslint": "^7.6.0",
"eslint-config-prettier": "^6.11.0",
"eslint-plugin-jest": "^23.14.0",
"eslint-plugin-jest-dom": "^3.0.1",
"eslint-plugin-react": "^7.20.0",
"eslint-plugin-react-hooks": "^4.0.4",
"eslint-plugin-testing-library": "^3.3.2",
"eslint-plugin-jest": "^23.20.0",
"eslint-plugin-jest-dom": "^3.1.7",
"eslint-plugin-react": "^7.20.5",
"eslint-plugin-react-hooks": "^4.0.8",
"eslint-plugin-testing-library": "^3.6.0",
"husky": "^4.2.5",
"identity-obj-proxy": "^3.0.0",
"jest": "^26.0.1",
"jest": "^26.3.0",
"jest-sonar-reporter": "^2.0.0",
"npm-run-all": "^4.1.5",
"prettier": "^2.0.5",
Expand All @@ -72,13 +72,13 @@
"react-dom": "^16.13.1",
"react-router-dom": "^5.2.0",
"rimraf": "^3.0.2",
"rollup": "^2.18.1",
"rollup-plugin-peer-deps-external": "^2.2.2",
"rollup-plugin-postcss": "^3.1.2",
"rollup": "^2.23.1",
"rollup-plugin-peer-deps-external": "^2.2.3",
"rollup-plugin-postcss": "^3.1.5",
"rollup-plugin-sass": "^1.2.2",
"rollup-plugin-typescript2": "^0.27.1",
"sass-loader": "^9.0.2",
"ts-jest": "^26.1.3",
"rollup-plugin-typescript2": "^0.27.2",
"sass-loader": "^9.0.3",
"ts-jest": "^26.2.0",
"typescript": "^3.9.7"
},
"peerDependencies": {
Expand Down
35 changes: 35 additions & 0 deletions packages/ocean-components/src/Checkbox/Checkbox.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import React from 'react';
import classNames from 'classnames';

import './styles/checkbox.scss';

type CheckboxProps = {
/**
* The label content.
*/
label?: React.ReactNode;
} & React.ComponentPropsWithoutRef<'input'>;

const Checkbox = React.forwardRef<HTMLInputElement, CheckboxProps>(
function Checkbox({ className, label, id, ...rest }, ref) {
return (
<label className="ods-checkbox__root" htmlFor={id}>
<input
ref={ref}
id={id}
className={classNames('ods-checkbox', className)}
{...rest}
type="checkbox"
/>
<span className="ods-checkbox__checkmark"></span>
{label && (
<span className="ods-typography ods-typography__paragraph ods-checkbox__label">
{label}
</span>
)}
</label>
);
}
);

export default Checkbox;
31 changes: 31 additions & 0 deletions packages/ocean-components/src/Checkbox/__tests__/Checkbox.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import React from 'react';
import { render } from '@testing-library/react';

import Checkbox from '../Checkbox';

test('renders element properly', () => {
const { container, getByTestId } = render(
<Checkbox data-testid="checkbox-test" className="custom-class" />
);

expect(container.firstChild).toMatchInlineSnapshot(`
<label
class="ods-checkbox__root"
>
<input
class="ods-checkbox custom-class"
data-testid="checkbox-test"
type="checkbox"
/>
<span
class="ods-checkbox__checkmark"
/>
</label>
`);
expect(getByTestId('checkbox-test')).toHaveAttribute('type', 'checkbox');
});

test('renders a label for the checkbox', () => {
const { getByText } = render(<Checkbox label="My label" />);
expect(getByText('My label')).toHaveClass('ods-checkbox__label');
});
1 change: 1 addition & 0 deletions packages/ocean-components/src/Checkbox/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from './Checkbox';
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { Meta, Story, Canvas, ArgsTable } from '@storybook/addon-docs/blocks';
import Checkbox from '../Checkbox';

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

# Checkbox API

The API documentation of the Checkbox React component. Learn more about the props and the CSS customization points.

## Import

```javascript
import { Checkbox } from '@useblu/ocean-components';
```

## Usage

<Canvas withSource="open" withToolbar>
<Story name="Usage">
<Checkbox id="checkbox-usage" label="Label" checked readOnly />
</Story>
</Canvas>

## Props

<ArgsTable of="." />

The ref is forwarded to the root element.

Any other props supplied will be provided to the root element (native element).

## CSS

| Global class | Description |
| -------------------------- | --------------------------------------- |
| .ods-checkbox\_\_root | Styles applied to the root element. |
| .ods-checkbox | Styles applied to the checkbox element. |
| .ods-checkbox\_\_checkmark | Styles applied to the span element. |
| .ods-checkbox\_\_label | Styles applied to the span element. |

If that's not sufficient, you can check the [implementation of the component](https://github.com/Pagnet/ocean-ds-web/blob/master/src/Checkbox/Checkbox.tsx) for more detail.

## Playground

<Canvas>
<Story
name="Playground"
args={{
label: 'Label',
disabled: false,
}}
argTypes={{
label: {
control: 'text',
},
disabled: {
control: 'boolean',
},
}}
>
{(props) => <Checkbox id="checkbox-playground" {...props} />}
</Story>
</Canvas>

<ArgsTable story="Playground" of="." />
70 changes: 70 additions & 0 deletions packages/ocean-components/src/Checkbox/styles/checkbox.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
@import '~@useblu/tokens/dist/web/tokens.scss';

.ods-checkbox__root {
display: flex;
user-select: none;
}

.ods-checkbox {
opacity: 0;
height: 0;
width: 0;
margin: 0;

&__label {
cursor: pointer;
padding-left: $spacing-inline-xxs;
}

&__checkmark {
cursor: pointer;
width: 24px;
height: 24px;
border: solid 2px $color-interface-dark-up;
border-radius: $border-radius-sm;
background-color: $color-interface-light-pure;
box-sizing: border-box;

&:hover {
background-color: $color-interface-light-up;
border-color: $color-interface-dark-up;
}

&:after {
content: '';
display: none;
margin-top: 2px;
margin-left: 6px;
width: 6px;
height: 10px;
border: solid white;
border-width: 0 2px 2px 0;
transform: rotate(40deg);
}
}

&:checked ~ &__checkmark:after {
display: block;
}

&:disabled ~ &__checkmark,
&:disabled ~ &__label {
cursor: not-allowed;
color: $color-interface-light-deep;
}

&:checked:not(:disabled) ~ &__checkmark {
border-color: $color-highlight-pure;
background-color: $color-highlight-pure;
}

&:checked:disabled ~ &__checkmark {
background-color: $color-interface-light-deep;
border-color: $color-interface-light-deep;
}

&:disabled:not(:checked) ~ &__checkmark {
background-color: $color-interface-light-pure;
border-color: $color-interface-light-deep;
}
}
3 changes: 3 additions & 0 deletions packages/ocean-components/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ export * from './Button';

export { default as Input } from './Input';
export * from './Input';

export { default as Checkbox } from './Checkbox';
export * from './Checkbox';
Loading

0 comments on commit 72c5dce

Please sign in to comment.