-
Notifications
You must be signed in to change notification settings - Fork 487
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
Changes from all commits
4f98707
c3fcdad
41cc31b
ae26d79
a9e9d9c
608a5cb
00a8286
33b424a
0172ed5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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. |
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')); |
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> |
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"] | ||
} |
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>; | ||
|
||
/** | ||
* 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> = ({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 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>
);
} There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 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.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point. I actually was able to simplify it by simply using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh it looks much better now, good move with |
||
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; |
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(); | ||
}); | ||
}); |
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> | ||
`; |
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__/**"] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"include": ["src"] | ||
} |
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'], | ||
}, | ||
}; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.