Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #1757 - Create @clay/label #1758

Merged
merged 9 commits into from
Apr 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions migrate-the-clay-components-from-v2-to-v3.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,13 @@ Using a radio by itself doesn't make much sense, only when 2+ exist does the fun
<ClayRadioGroup.Radio label="Three" value="three" />
</ClayRadioGroup>
```

## ClayLabel

### API Changes

- `style` is now `displayType`
- Removed `size` in favor of `large` since there is only default and large options.
- Removed `label` in favor of utilizing `children` prop
- Added `closeButtonProps` which allows you to add attributes to the nested button.
- This is where you would pass a callback for `onClick`.
2 changes: 1 addition & 1 deletion packages/clay-icon/src/ClayIcon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const ClayIcon: React.FunctionComponent<Props> = ({
)}
role="presentation"
>
<use data-href={`${spriteMapVal}#${symbol}`} />
<use xlinkHref={`${spriteMapVal}#${symbol}`} />
</svg>
);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ exports[`ClayIcon renders 1`] = `
role="presentation"
>
<use
data-href="/path/to/some/resource.svg#cool-icon"
xlinkHref="/path/to/some/resource.svg#cool-icon"
/>
</svg>
`;
Expand All @@ -17,7 +17,7 @@ exports[`ClayIcon renders with context spritemap 1`] = `
role="presentation"
>
<use
data-href="foo/bar.svg#cool-icon"
xlinkHref="foo/bar.svg#cool-icon"
/>
</svg>
`;
25 changes: 25 additions & 0 deletions packages/clay-label/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# clay-label

React implementation for label from clay-css

## Setup

1. Install `yarn`

2. Install local dependencies:

```
yarn
```

3. Build the code:

```
yarn start
```

4. Open the demo at `localhost:8080` on your browser.

## Contribute

We'd love to get contributions from you! Please, check our [Contributing Guidelines](https://github.com/liferay/clay/blob/master/CONTRIBUTING.md) to see how you can help us improve.
44 changes: 44 additions & 0 deletions packages/clay-label/demo/App.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* © 2019 Liferay, Inc. <https://liferay.com>
*
* SPDX-License-Identifier: BSD-3-Clause
*/

import * as React from 'react';
import * as ReactDOM from 'react-dom';
import ClayLabel from '../src/ClayLabel';
import {ClayIconSpriteContext} from '@clayui/icon';

import 'clay-css/lib/css/atlas.css';

const App: React.FunctionComponent = () => {
const [active, setActive] = React.useState(true);

return (
<ClayIconSpriteContext.Provider value="node_modules/clay-css/lib/images/icons/icons.svg">
<div>
<ClayLabel>{'Default Label'}</ClayLabel>
<ClayLabel displayType="info">{'Info Label'}</ClayLabel>
<ClayLabel displayType="warning">{'Warning Label'}</ClayLabel>
<ClayLabel displayType="danger">{'Danger Label'}</ClayLabel>
<ClayLabel displayType="success">{'Success Label'}</ClayLabel>

{active && (
<ClayLabel
closeButtonProps={{
children: 'x',
onClick: () => setActive(false),
}}
displayType="success"
>
{'Closable'}
</ClayLabel>
)}

<ClayLabel href="#/foo/bar">{'Label w/ link'}</ClayLabel>
</div>
</ClayIconSpriteContext.Provider>
);
};

ReactDOM.render(<App />, document.getElementById('root'));
11 changes: 11 additions & 0 deletions packages/clay-label/demo/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<img
style="display: none;"
src="../node_modules/clay-css/lib/images/icons/icons.svg"
/>

<body style="margin: 48px;">
<div id="root"></div>
</body>
</html>
35 changes: 35 additions & 0 deletions packages/clay-label/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"name": "@clayui/label",
"version": "3.0.0",
"description": "ClayLabel component",
"license": "BSD-3-Clause",
"repository": "https://github.com/liferay/clay/tree/master/packages/clay-label",
"engines": {
"node": ">=0.12.0",
"npm": ">=3.0.0"
},
"main": "lib/ClayLabel.js",
"esnext:main": "src/ClayLabel.js",
"jsnext:main": "src/ClayLabel.js",
"files": ["lib", "src"],
"scripts": {
"build": "babel src --root-mode upward --out-dir lib --extensions .ts,.tsx",
"build:types": "tsc --project ./tsconfig.declarations.json",
"prepublishOnly": "npm run build && npm run build:types",
"start": "webpack-dev-server --mode development",
"test": "jest --config ../../jest.config.js"
},
"keywords": ["clay", "react"],
"dependencies": {
"@clayui/icon": "^3.0.0",
"classnames": "^2.2.6"
},
"devDependencies": {
"clay-css": "^2.12.0"
},
"peerDependencies": {
"react": "^16.8.1",
"react-dom": "^16.8.1"
},
"browserslist": ["extends browserslist-config-clay"]
}
75 changes: 75 additions & 0 deletions packages/clay-label/src/ClayLabel.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/**
* © 2019 Liferay, Inc. <https://liferay.com>
*
* SPDX-License-Identifier: BSD-3-Clause
*/

import * as React from 'react';
import classNames from 'classnames';
import Icon from '@clayui/icon';

type DisplayType = 'secondary' | 'info' | 'warning' | 'danger' | 'success';

interface Props
extends React.BaseHTMLAttributes<HTMLAnchorElement | HTMLSpanElement> {
/**
* HTML properties that are applied to the 'x' button.
*/
closeButtonProps?: React.ButtonHTMLAttributes<HTMLButtonElement>;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had forgotten about this, but wanted us to continue adding the API descriptions here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh good call, I had forgotten too. I'll send a new PR to update all the existing attributes that were merged and then I will update this PR and any others that I have open.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, thanks Bryce! I'll update the clay-link as well.


/**
* Determines the style of the label.
*/
displayType?: DisplayType;

/**
* Flag to indicate if the label should be of the `large` variant.
*/
large?: boolean;

/**
* Path to the location of the spritemap resource used for Icon.
*/
spritemap?: string;
}

const ClayLabel: React.FunctionComponent<Props> = ({
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm finding this component a little tricky and maybe we do not need to call React.createElement directly. We can simplify a little:

Just a suggestion, what do you think? 🙂

const ClayLabelContent = ({
	children,
	closeButtonProps,
}) => (
	<>
		<span className="label-item label-item-expand">{children}</span>
		{closeButtonProps && (
			<span className="label-item label-item-after">
				<button
					{...closeButtonProps}
					className="close"
					type="button"
				>
					{closeButtonProps.children}
				</button>
			</span>
		)}
	</>
);

const ClayLabel = ({
	children,
	className,
	closeButtonProps,
	displayType = 'secondary',
	href,
	large = false,
	...otherProps
}) => {
	const classNames = cN('label', className, {
		'label-dismissible': closeButtonProps,
		'label-lg': large,
		[`label-${displayType}`]: displayType,
	});

	if (href) {
		return (
			<a
				href={href}
                                 className={classNames}
				{...otherProps}
			>
				<ClayLabelContent closeButtonProps={closeButtonProps}>
					{children}
				</ClayLabelContent>
			</a>
		);
	}

	return (
		<span {...otherProps} className={classNames}>
			<ClayLabelContent closeButtonProps={closeButtonProps}>
				{children}
			</ClayLabelContent>
		</span>
	);
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey Matu, what exactly do you find confusing? Is it particularly the call to React.createElement? I was trying to avoid multiple returns and also avoiding duplicating the markup for the content.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey Bryce, yeah I understand what you wanted to do, the big reason is that I do not really like having to call React.createElement directly, I'd rather let the babel presets to react transform to React.createElement, in this case, and I find it a bit strange that we have a condition that removes the href from the above object (it seems a bit hack), I think we have other ways of doing this without having to compromise a little beauty of our code in the development environment.

I think we do not need to focus as much on eliminating as many rows (we can then improve our build steps for distribution) by just getting in the middle so we do not remove some beauty from the code.

Just making sure that we can have other ways of doing this.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. I actually was able to simplify it by simply using const TagName = href ? 'a' : 'span'; I think this way makes most sense to me.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh it looks much better now, good move with TagName.

children,
className,
closeButtonProps,
displayType = 'secondary',
href,
large = false,
spritemap,
...otherProps
}) => {
const TagName = href ? 'a' : 'span';

return (
<TagName
{...otherProps}
className={classNames('label', className, {
'label-dismissible': closeButtonProps,
'label-lg': large,
[`label-${displayType}`]: displayType,
})}
href={href}
>
<span className="label-item label-item-expand">{children}</span>

{closeButtonProps && (
<span className="label-item label-item-after">
<button
{...closeButtonProps}
className="close"
type="button"
>
<Icon spritemap={spritemap} symbol="times" />
</button>
</span>
)}
</TagName>
);
};

export default ClayLabel;
35 changes: 35 additions & 0 deletions packages/clay-label/src/__tests__/ClayLabel.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* © 2019 Liferay, Inc. <https://liferay.com>
*
* SPDX-License-Identifier: BSD-3-Clause
*/

import * as React from 'react';
import * as TestRenderer from 'react-test-renderer';
import ClayLabel from '../ClayLabel';

describe('ClayLabel', () => {
it('renders', () => {
const testRenderer = TestRenderer.create(
<ClayLabel>{'Default Label'}</ClayLabel>
);

expect(testRenderer.toJSON()).toMatchSnapshot();
});

it('renders with a different displayType ', () => {
const testRenderer = TestRenderer.create(
<ClayLabel displayType="success">{'Success Label'}</ClayLabel>
);

expect(testRenderer.toJSON()).toMatchSnapshot();
});

it('renders as a link ', () => {
const testRenderer = TestRenderer.create(
<ClayLabel href="#/foo/bar">{'Label w/ link'}</ClayLabel>
);

expect(testRenderer.toJSON()).toMatchSnapshot();
});
});
38 changes: 38 additions & 0 deletions packages/clay-label/src/__tests__/__snapshots__/ClayLabel.tsx.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`ClayLabel renders 1`] = `
<span
className="label label-secondary"
>
<span
className="label-item label-item-expand"
>
Default Label
</span>
</span>
`;

exports[`ClayLabel renders as a link 1`] = `
<a
className="label label-secondary"
href="#/foo/bar"
>
<span
className="label-item label-item-expand"
>
Label w/ link
</span>
</a>
`;

exports[`ClayLabel renders with a different displayType 1`] = `
<span
className="label label-success"
>
<span
className="label-item label-item-expand"
>
Success Label
</span>
</span>
`;
8 changes: 8 additions & 0 deletions packages/clay-label/tsconfig.declarations.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"compilerOptions": {
"declarationDir": "./lib"
},
"extends": "../../tsconfig.declarations.json",
"include": ["src"],
"exclude": ["**/__tests__/**"]
}
4 changes: 4 additions & 0 deletions packages/clay-label/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "../../tsconfig.json",
"include": ["src"]
}
26 changes: 26 additions & 0 deletions packages/clay-label/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* © 2019 Liferay, Inc. <https://liferay.com>
*
* SPDX-License-Identifier: BSD-3-Clause
*/

const path = require('path');
const HWP = require('html-webpack-plugin');

module.exports = {
entry: path.join(__dirname, 'demo/App.tsx'),
module: {
rules: [
{loader: 'awesome-typescript-loader', test: /\.tsx?$/},
{enforce: 'pre', loader: 'source-map-loader', test: /\.js$/},
{
test: /\.css$/,
use: ['style-loader', 'css-loader'],
},
],
},
plugins: [new HWP({template: path.join(__dirname, './demo/index.html')})],
resolve: {
extensions: ['.ts', '.tsx', '.js', '.json'],
},
};