@clayui/label 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +73 -0
- package/README.mdx +49 -0
- package/lib/index.d.ts +22 -0
- package/lib/index.js +123 -0
- package/package.json +42 -0
- package/src/__tests__/__snapshots__/index.tsx.snap +133 -0
- package/src/__tests__/index.tsx +111 -0
- package/src/index.tsx +200 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
# Change Log
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
|
+
|
|
6
|
+
# [4.0.0](https://github.com/liferay/clay/compare/v3.36.0...v4.0.0) (2021-10-06)
|
|
7
|
+
|
|
8
|
+
**Note:** Version bump only for package @clayui/label
|
|
9
|
+
|
|
10
|
+
# [3.32.0](https://github.com/liferay/clay/compare/v3.31.0...v3.32.0) (2021-07-28)
|
|
11
|
+
|
|
12
|
+
**Note:** Version bump only for package @clayui/label
|
|
13
|
+
|
|
14
|
+
## [3.4.1](https://github.com/liferay/clay/compare/@clayui/label@3.4.0...@clayui/label@3.4.1) (2021-01-13)
|
|
15
|
+
|
|
16
|
+
### Bug Fixes
|
|
17
|
+
|
|
18
|
+
- **@clayui/label:** close icon should be times-small ([5181335](https://github.com/liferay/clay/commit/5181335))
|
|
19
|
+
|
|
20
|
+
# [3.4.0](https://github.com/liferay/clay/compare/@clayui/label@3.3.1...@clayui/label@3.4.0) (2020-10-01)
|
|
21
|
+
|
|
22
|
+
### Features
|
|
23
|
+
|
|
24
|
+
- **clayui.com:** Add JSP code example to Label ([e90068e](https://github.com/liferay/clay/commit/e90068e))
|
|
25
|
+
- **clayui.com:** Move API tables to new files and update document tabs, rename files to use singular naming instead of plural, change mainTabURL of pagination-bar to a proper one ([d812ee9](https://github.com/liferay/clay/commit/d812ee9))
|
|
26
|
+
- add displayName for all components ([cc3211d](https://github.com/liferay/clay/commit/cc3211d))
|
|
27
|
+
|
|
28
|
+
## [3.3.1](https://github.com/liferay/clay/compare/@clayui/label@3.3.0...@clayui/label@3.3.1) (2020-06-18)
|
|
29
|
+
|
|
30
|
+
**Note:** Version bump only for package @clayui/label
|
|
31
|
+
|
|
32
|
+
# [3.3.0](https://github.com/liferay/clay/compare/@clayui/label@3.2.0...@clayui/label@3.3.0) (2020-04-24)
|
|
33
|
+
|
|
34
|
+
### Features
|
|
35
|
+
|
|
36
|
+
- **@clayui/label:** Attempt at ClayLabel contentBefore prop ([cd634e2](https://github.com/liferay/clay/commit/cd634e2))
|
|
37
|
+
- **@clayui/label:** use a hybrid API appraoch to expose low-level components ([deffd0e](https://github.com/liferay/clay/commit/deffd0e))
|
|
38
|
+
|
|
39
|
+
# [3.2.0](https://github.com/liferay/clay/tree/master/packages/clay-label/compare/@clayui/label@3.1.1...@clayui/label@3.2.0) (2020-03-26)
|
|
40
|
+
|
|
41
|
+
### Features
|
|
42
|
+
|
|
43
|
+
- **@clayui/label:** add prop for inner element attributes ([2d1f489](https://github.com/liferay/clay/commit/2d1f489))
|
|
44
|
+
|
|
45
|
+
## [3.1.1](https://github.com/liferay/clay/tree/master/packages/clay-label/compare/@clayui/label@3.1.0...@clayui/label@3.1.1) (2020-03-12)
|
|
46
|
+
|
|
47
|
+
### Bug Fixes
|
|
48
|
+
|
|
49
|
+
- **@clayui/label:** use ClayLink over anchor tag ([746487f](https://github.com/liferay/clay/commit/746487f))
|
|
50
|
+
|
|
51
|
+
# 3.1.0 (2020-02-28)
|
|
52
|
+
|
|
53
|
+
### Bug Fixes
|
|
54
|
+
|
|
55
|
+
- update components to include 'types' and 'ts:main' package keys ([9e24b59](https://github.com/liferay/clay/commit/9e24b59))
|
|
56
|
+
|
|
57
|
+
### Features
|
|
58
|
+
|
|
59
|
+
- **clay-form:** create high-level multi-select component ([feb3abc](https://github.com/liferay/clay/commit/feb3abc))
|
|
60
|
+
- added template demos ([c2f1064](https://github.com/liferay/clay/commit/c2f1064))
|
|
61
|
+
- update react-testing-library to be @testing-library/react ([bd78145](https://github.com/liferay/clay/commit/bd78145))
|
|
62
|
+
|
|
63
|
+
## [3.0.3](https://github.com/liferay/clay/tree/master/packages/clay-label/compare/@clayui/label@3.0.1...@clayui/label@3.0.3) (2020-01-31)
|
|
64
|
+
|
|
65
|
+
**Note:** Version bump only for package @clayui/label
|
|
66
|
+
|
|
67
|
+
## [3.0.2](https://github.com/liferay/clay/tree/master/packages/clay-label/compare/@clayui/label@3.0.1...@clayui/label@3.0.2) (2020-01-20)
|
|
68
|
+
|
|
69
|
+
**Note:** Version bump only for package @clayui/label
|
|
70
|
+
|
|
71
|
+
## [3.0.1](https://github.com/liferay/clay/tree/master/packages/clay-label/compare/@clayui/label@3.0.0...@clayui/label@3.0.1) (2019-10-28)
|
|
72
|
+
|
|
73
|
+
**Note:** Version bump only for package @clayui/label
|
package/README.mdx
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: 'Label'
|
|
3
|
+
description: 'Labels are a visual pattern used to categorize information providing quick and easy recognition.'
|
|
4
|
+
lexiconDefinition: 'https://liferay.design/lexicon/core-components/labels/'
|
|
5
|
+
packageNpm: '@clayui/label'
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
import {
|
|
9
|
+
Label,
|
|
10
|
+
LabelClosingActions,
|
|
11
|
+
LabelDisplayTypes,
|
|
12
|
+
} from '$packages/clay-label/docs/index';
|
|
13
|
+
|
|
14
|
+
<div class="nav-toc-absolute">
|
|
15
|
+
<div class="nav-toc">
|
|
16
|
+
|
|
17
|
+
- [Display Types](#display-types)
|
|
18
|
+
- [Closing Actions](#closing-actions)
|
|
19
|
+
|
|
20
|
+
</div>
|
|
21
|
+
</div>
|
|
22
|
+
|
|
23
|
+
## Overview
|
|
24
|
+
|
|
25
|
+
`ClayLabel` offers two different APIs for use by toggling the prop `withClose`. By default(`withClose={true}`), `ClayLabel` behaves like a high-level component. If you want to use the lower-level components and compose multiple parts to your label, all you need to do is set `withClose={false}`.
|
|
26
|
+
|
|
27
|
+
Check out [this storybook example](storybook.clayui.com/?path=/story/components-claylabel--w-content-before) for a demo.
|
|
28
|
+
|
|
29
|
+
## Display Types
|
|
30
|
+
|
|
31
|
+
Using [`displayType`](#api-displayType) property you can use these color variations on Label component:
|
|
32
|
+
|
|
33
|
+
<LabelDisplayTypes />
|
|
34
|
+
|
|
35
|
+
## Closing Actions
|
|
36
|
+
|
|
37
|
+
<div class="clay-site-alert alert alert-warning">
|
|
38
|
+
This property is mandatory if you want make your label dismissible.
|
|
39
|
+
</div>
|
|
40
|
+
|
|
41
|
+
Use [`closeButtonProps`](#api-closeButtonProps) property for applying custom properties to the button that wraps the closing icon.
|
|
42
|
+
|
|
43
|
+
In this example, an Id was settled for the closing element:
|
|
44
|
+
|
|
45
|
+
<LabelClosingActions />
|
|
46
|
+
|
|
47
|
+
You can also set a state for the visibility of the ClayLabel for example, handling `onClick` property on the closing element.
|
|
48
|
+
|
|
49
|
+
<Label />
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
declare const ClayLabelItemExpand: React.ForwardRefExoticComponent<React.BaseHTMLAttributes<HTMLAnchorElement | HTMLSpanElement> & React.RefAttributes<HTMLAnchorElement | HTMLSpanElement>>;
|
|
3
|
+
declare type DisplayType = 'secondary' | 'info' | 'warning' | 'danger' | 'success' | 'unstyled';
|
|
4
|
+
interface IBaseProps extends React.BaseHTMLAttributes<HTMLSpanElement> {
|
|
5
|
+
dismissible?: boolean;
|
|
6
|
+
displayType?: DisplayType;
|
|
7
|
+
large?: boolean;
|
|
8
|
+
}
|
|
9
|
+
interface IProps extends IBaseProps {
|
|
10
|
+
closeButtonProps?: React.ButtonHTMLAttributes<HTMLButtonElement> & {
|
|
11
|
+
ref?: (instance: HTMLButtonElement | null) => void;
|
|
12
|
+
};
|
|
13
|
+
innerElementProps?: React.ComponentProps<typeof ClayLabelItemExpand>;
|
|
14
|
+
spritemap?: string;
|
|
15
|
+
withClose?: boolean;
|
|
16
|
+
}
|
|
17
|
+
declare const _default: React.ForwardRefExoticComponent<IProps & React.RefAttributes<HTMLAnchorElement | HTMLSpanElement>> & {
|
|
18
|
+
ItemAfter: React.ForwardRefExoticComponent<React.HTMLAttributes<HTMLSpanElement> & React.RefAttributes<HTMLSpanElement>>;
|
|
19
|
+
ItemBefore: React.ForwardRefExoticComponent<React.HTMLAttributes<HTMLSpanElement> & React.RefAttributes<HTMLSpanElement>>;
|
|
20
|
+
ItemExpand: React.ForwardRefExoticComponent<React.BaseHTMLAttributes<HTMLAnchorElement | HTMLSpanElement> & React.RefAttributes<HTMLAnchorElement | HTMLSpanElement>>;
|
|
21
|
+
};
|
|
22
|
+
export default _default;
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
|
|
8
|
+
var _icon = _interopRequireDefault(require("@clayui/icon"));
|
|
9
|
+
|
|
10
|
+
var _link = _interopRequireDefault(require("@clayui/link"));
|
|
11
|
+
|
|
12
|
+
var _classnames = _interopRequireDefault(require("classnames"));
|
|
13
|
+
|
|
14
|
+
var _react = _interopRequireDefault(require("react"));
|
|
15
|
+
|
|
16
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
17
|
+
|
|
18
|
+
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
19
|
+
|
|
20
|
+
function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
|
|
21
|
+
|
|
22
|
+
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
|
|
23
|
+
|
|
24
|
+
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
|
|
25
|
+
|
|
26
|
+
var ClayLabelItemAfter = /*#__PURE__*/_react.default.forwardRef(function (_ref, ref) {
|
|
27
|
+
var children = _ref.children,
|
|
28
|
+
className = _ref.className,
|
|
29
|
+
otherProps = _objectWithoutProperties(_ref, ["children", "className"]);
|
|
30
|
+
|
|
31
|
+
return /*#__PURE__*/_react.default.createElement("span", _extends({}, otherProps, {
|
|
32
|
+
className: (0, _classnames.default)(className, 'label-item label-item-after'),
|
|
33
|
+
ref: ref
|
|
34
|
+
}), children);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
ClayLabelItemAfter.displayName = 'ClayLabelItemAfter';
|
|
38
|
+
|
|
39
|
+
var ClayLabelItemBefore = /*#__PURE__*/_react.default.forwardRef(function (_ref2, ref) {
|
|
40
|
+
var children = _ref2.children,
|
|
41
|
+
className = _ref2.className,
|
|
42
|
+
otherProps = _objectWithoutProperties(_ref2, ["children", "className"]);
|
|
43
|
+
|
|
44
|
+
return /*#__PURE__*/_react.default.createElement("span", _extends({}, otherProps, {
|
|
45
|
+
className: (0, _classnames.default)(className, 'label-item label-item-before'),
|
|
46
|
+
ref: ref
|
|
47
|
+
}), children);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
ClayLabelItemBefore.displayName = 'ClayLabelItemBefore';
|
|
51
|
+
|
|
52
|
+
var ClayLabelItemExpand = /*#__PURE__*/_react.default.forwardRef(function (_ref3, ref) {
|
|
53
|
+
var children = _ref3.children,
|
|
54
|
+
className = _ref3.className,
|
|
55
|
+
href = _ref3.href,
|
|
56
|
+
otherProps = _objectWithoutProperties(_ref3, ["children", "className", "href"]);
|
|
57
|
+
|
|
58
|
+
var TagName = href ? _link.default : 'span';
|
|
59
|
+
return /*#__PURE__*/_react.default.createElement(TagName, _extends({}, otherProps, {
|
|
60
|
+
className: (0, _classnames.default)(className, 'label-item label-item-expand'),
|
|
61
|
+
href: href,
|
|
62
|
+
ref: ref
|
|
63
|
+
}), children);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
ClayLabelItemExpand.displayName = 'ClayLabelItemExpand';
|
|
67
|
+
|
|
68
|
+
var ClayLabel = /*#__PURE__*/_react.default.forwardRef(function (_ref4, ref) {
|
|
69
|
+
var children = _ref4.children,
|
|
70
|
+
className = _ref4.className,
|
|
71
|
+
dismissible = _ref4.dismissible,
|
|
72
|
+
_ref4$displayType = _ref4.displayType,
|
|
73
|
+
displayType = _ref4$displayType === void 0 ? 'secondary' : _ref4$displayType,
|
|
74
|
+
_ref4$large = _ref4.large,
|
|
75
|
+
large = _ref4$large === void 0 ? false : _ref4$large,
|
|
76
|
+
otherProps = _objectWithoutProperties(_ref4, ["children", "className", "dismissible", "displayType", "large"]);
|
|
77
|
+
|
|
78
|
+
return /*#__PURE__*/_react.default.createElement("span", _extends({}, otherProps, {
|
|
79
|
+
className: (0, _classnames.default)('label', className, _defineProperty({
|
|
80
|
+
'label-dismissible': dismissible,
|
|
81
|
+
'label-lg': large
|
|
82
|
+
}, "label-".concat(displayType), displayType)),
|
|
83
|
+
ref: ref
|
|
84
|
+
}), children);
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
ClayLabel.displayName = 'ClayLabel';
|
|
88
|
+
|
|
89
|
+
var ClayLabelHybrid = /*#__PURE__*/_react.default.forwardRef(function (_ref5, ref) {
|
|
90
|
+
var children = _ref5.children,
|
|
91
|
+
closeButtonProps = _ref5.closeButtonProps,
|
|
92
|
+
href = _ref5.href,
|
|
93
|
+
_ref5$innerElementPro = _ref5.innerElementProps,
|
|
94
|
+
innerElementProps = _ref5$innerElementPro === void 0 ? {} : _ref5$innerElementPro,
|
|
95
|
+
_ref5$withClose = _ref5.withClose,
|
|
96
|
+
withClose = _ref5$withClose === void 0 ? true : _ref5$withClose,
|
|
97
|
+
spritemap = _ref5.spritemap,
|
|
98
|
+
otherProps = _objectWithoutProperties(_ref5, ["children", "closeButtonProps", "href", "innerElementProps", "withClose", "spritemap"]);
|
|
99
|
+
|
|
100
|
+
return /*#__PURE__*/_react.default.createElement(ClayLabel, _extends({
|
|
101
|
+
dismissible: withClose && !!closeButtonProps
|
|
102
|
+
}, otherProps, {
|
|
103
|
+
ref: ref
|
|
104
|
+
}), !withClose && children, withClose && /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(ClayLabelItemExpand, _extends({}, innerElementProps, {
|
|
105
|
+
href: href
|
|
106
|
+
}), children), closeButtonProps && /*#__PURE__*/_react.default.createElement(ClayLabelItemAfter, null, /*#__PURE__*/_react.default.createElement("button", _extends({}, closeButtonProps, {
|
|
107
|
+
className: (0, _classnames.default)(closeButtonProps.className, 'close'),
|
|
108
|
+
type: "button"
|
|
109
|
+
}), /*#__PURE__*/_react.default.createElement(_icon.default, {
|
|
110
|
+
spritemap: spritemap,
|
|
111
|
+
symbol: "times-small"
|
|
112
|
+
})))));
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
ClayLabelHybrid.displayName = 'ClayLabel';
|
|
116
|
+
|
|
117
|
+
var _default = Object.assign(ClayLabelHybrid, {
|
|
118
|
+
ItemAfter: ClayLabelItemAfter,
|
|
119
|
+
ItemBefore: ClayLabelItemBefore,
|
|
120
|
+
ItemExpand: ClayLabelItemExpand
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
exports.default = _default;
|
package/package.json
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@clayui/label",
|
|
3
|
+
"version": "4.0.0",
|
|
4
|
+
"description": "ClayLabel component",
|
|
5
|
+
"license": "BSD-3-Clause",
|
|
6
|
+
"repository": "https://github.com/liferay/clay",
|
|
7
|
+
"engines": {
|
|
8
|
+
"node": ">=0.12.0",
|
|
9
|
+
"npm": ">=3.0.0"
|
|
10
|
+
},
|
|
11
|
+
"main": "lib/index.js",
|
|
12
|
+
"types": "lib/index.d.ts",
|
|
13
|
+
"ts:main": "src/index.tsx",
|
|
14
|
+
"files": [
|
|
15
|
+
"lib",
|
|
16
|
+
"src"
|
|
17
|
+
],
|
|
18
|
+
"scripts": {
|
|
19
|
+
"build": "babel src --root-mode upward --out-dir lib --extensions .ts,.tsx",
|
|
20
|
+
"build:types": "tsc --project ./tsconfig.declarations.json",
|
|
21
|
+
"prepublishOnly": "yarn build && yarn build:types",
|
|
22
|
+
"test": "jest --config ../../jest.config.js"
|
|
23
|
+
},
|
|
24
|
+
"keywords": [
|
|
25
|
+
"clay",
|
|
26
|
+
"react"
|
|
27
|
+
],
|
|
28
|
+
"dependencies": {
|
|
29
|
+
"@clayui/icon": "^4.0.0",
|
|
30
|
+
"@clayui/link": "^4.0.0",
|
|
31
|
+
"classnames": "^2.2.6"
|
|
32
|
+
},
|
|
33
|
+
"peerDependencies": {
|
|
34
|
+
"@clayui/css": "3.x",
|
|
35
|
+
"react": "^16.12.0",
|
|
36
|
+
"react-dom": "^16.12.0"
|
|
37
|
+
},
|
|
38
|
+
"browserslist": [
|
|
39
|
+
"extends browserslist-config-clay"
|
|
40
|
+
],
|
|
41
|
+
"gitHead": "0d285c26936b985e7be90e61b6c21e04734a0ad9"
|
|
42
|
+
}
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
|
2
|
+
|
|
3
|
+
exports[`Rendering as a link 1`] = `
|
|
4
|
+
<span
|
|
5
|
+
className="label label-secondary"
|
|
6
|
+
>
|
|
7
|
+
<a
|
|
8
|
+
className="label-item label-item-expand"
|
|
9
|
+
href="#/foo/bar"
|
|
10
|
+
>
|
|
11
|
+
Label w/ link
|
|
12
|
+
</a>
|
|
13
|
+
</span>
|
|
14
|
+
`;
|
|
15
|
+
|
|
16
|
+
exports[`Rendering default 1`] = `
|
|
17
|
+
<span
|
|
18
|
+
className="label label-secondary"
|
|
19
|
+
>
|
|
20
|
+
<span
|
|
21
|
+
className="label-item label-item-expand"
|
|
22
|
+
>
|
|
23
|
+
Default Label
|
|
24
|
+
</span>
|
|
25
|
+
</span>
|
|
26
|
+
`;
|
|
27
|
+
|
|
28
|
+
exports[`Rendering renders as a link and closable 1`] = `
|
|
29
|
+
<span
|
|
30
|
+
className="label label-dismissible label-secondary"
|
|
31
|
+
>
|
|
32
|
+
<a
|
|
33
|
+
className="label-item label-item-expand"
|
|
34
|
+
href="#/foo/bar"
|
|
35
|
+
>
|
|
36
|
+
Label Closable
|
|
37
|
+
</a>
|
|
38
|
+
<span
|
|
39
|
+
className="label-item label-item-after"
|
|
40
|
+
>
|
|
41
|
+
<button
|
|
42
|
+
className="close"
|
|
43
|
+
onClick={[Function]}
|
|
44
|
+
type="button"
|
|
45
|
+
>
|
|
46
|
+
<svg
|
|
47
|
+
className="lexicon-icon lexicon-icon-times-small"
|
|
48
|
+
role="presentation"
|
|
49
|
+
>
|
|
50
|
+
<use
|
|
51
|
+
xlinkHref="path/to/spritemap#times-small"
|
|
52
|
+
/>
|
|
53
|
+
</svg>
|
|
54
|
+
</button>
|
|
55
|
+
</span>
|
|
56
|
+
</span>
|
|
57
|
+
`;
|
|
58
|
+
|
|
59
|
+
exports[`Rendering renders as closable 1`] = `
|
|
60
|
+
<span
|
|
61
|
+
className="label label-dismissible label-secondary"
|
|
62
|
+
>
|
|
63
|
+
<span
|
|
64
|
+
className="label-item label-item-expand"
|
|
65
|
+
>
|
|
66
|
+
Label Closable
|
|
67
|
+
</span>
|
|
68
|
+
<span
|
|
69
|
+
className="label-item label-item-after"
|
|
70
|
+
>
|
|
71
|
+
<button
|
|
72
|
+
className="close"
|
|
73
|
+
onClick={[Function]}
|
|
74
|
+
type="button"
|
|
75
|
+
>
|
|
76
|
+
<svg
|
|
77
|
+
className="lexicon-icon lexicon-icon-times-small"
|
|
78
|
+
role="presentation"
|
|
79
|
+
>
|
|
80
|
+
<use
|
|
81
|
+
xlinkHref="path/to/spritemap#times-small"
|
|
82
|
+
/>
|
|
83
|
+
</svg>
|
|
84
|
+
</button>
|
|
85
|
+
</span>
|
|
86
|
+
</span>
|
|
87
|
+
`;
|
|
88
|
+
|
|
89
|
+
exports[`Rendering renders with ItemAfter 1`] = `
|
|
90
|
+
<span
|
|
91
|
+
className="label label-secondary"
|
|
92
|
+
>
|
|
93
|
+
<span
|
|
94
|
+
className="label-item label-item-expand"
|
|
95
|
+
>
|
|
96
|
+
Label
|
|
97
|
+
</span>
|
|
98
|
+
<span
|
|
99
|
+
className="label-item label-item-after"
|
|
100
|
+
>
|
|
101
|
+
Content after
|
|
102
|
+
</span>
|
|
103
|
+
</span>
|
|
104
|
+
`;
|
|
105
|
+
|
|
106
|
+
exports[`Rendering renders with ItemBefore 1`] = `
|
|
107
|
+
<span
|
|
108
|
+
className="label label-secondary"
|
|
109
|
+
>
|
|
110
|
+
<span
|
|
111
|
+
className="label-item label-item-before"
|
|
112
|
+
>
|
|
113
|
+
Content before
|
|
114
|
+
</span>
|
|
115
|
+
<span
|
|
116
|
+
className="label-item label-item-expand"
|
|
117
|
+
>
|
|
118
|
+
Label
|
|
119
|
+
</span>
|
|
120
|
+
</span>
|
|
121
|
+
`;
|
|
122
|
+
|
|
123
|
+
exports[`Rendering with a different displayType 1`] = `
|
|
124
|
+
<span
|
|
125
|
+
className="label label-success"
|
|
126
|
+
>
|
|
127
|
+
<span
|
|
128
|
+
className="label-item label-item-expand"
|
|
129
|
+
>
|
|
130
|
+
Success Label
|
|
131
|
+
</span>
|
|
132
|
+
</span>
|
|
133
|
+
`;
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SPDX-FileCopyrightText: © 2019 Liferay, Inc. <https://liferay.com>
|
|
3
|
+
* SPDX-License-Identifier: BSD-3-Clause
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import ClayLabel from '..';
|
|
7
|
+
import {cleanup, fireEvent, render} from '@testing-library/react';
|
|
8
|
+
import React from 'react';
|
|
9
|
+
import TestRenderer from 'react-test-renderer';
|
|
10
|
+
|
|
11
|
+
const spritemap = 'path/to/spritemap';
|
|
12
|
+
|
|
13
|
+
describe('Rendering', () => {
|
|
14
|
+
it('default', () => {
|
|
15
|
+
const testRenderer = TestRenderer.create(
|
|
16
|
+
<ClayLabel>{'Default Label'}</ClayLabel>
|
|
17
|
+
);
|
|
18
|
+
|
|
19
|
+
expect(testRenderer.toJSON()).toMatchSnapshot();
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it('with a different displayType ', () => {
|
|
23
|
+
const testRenderer = TestRenderer.create(
|
|
24
|
+
<ClayLabel displayType="success">{'Success Label'}</ClayLabel>
|
|
25
|
+
);
|
|
26
|
+
|
|
27
|
+
expect(testRenderer.toJSON()).toMatchSnapshot();
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it('as a link ', () => {
|
|
31
|
+
const testRenderer = TestRenderer.create(
|
|
32
|
+
<ClayLabel href="#/foo/bar">{'Label w/ link'}</ClayLabel>
|
|
33
|
+
);
|
|
34
|
+
|
|
35
|
+
expect(testRenderer.toJSON()).toMatchSnapshot();
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it('renders as closable', () => {
|
|
39
|
+
const testRenderer = TestRenderer.create(
|
|
40
|
+
<ClayLabel
|
|
41
|
+
closeButtonProps={{
|
|
42
|
+
onClick: () => {},
|
|
43
|
+
}}
|
|
44
|
+
spritemap={spritemap}
|
|
45
|
+
>
|
|
46
|
+
{'Label Closable'}
|
|
47
|
+
</ClayLabel>
|
|
48
|
+
);
|
|
49
|
+
|
|
50
|
+
expect(testRenderer.toJSON()).toMatchSnapshot();
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it('renders as a link and closable', () => {
|
|
54
|
+
const testRenderer = TestRenderer.create(
|
|
55
|
+
<ClayLabel
|
|
56
|
+
closeButtonProps={{
|
|
57
|
+
onClick: () => {},
|
|
58
|
+
}}
|
|
59
|
+
href="#/foo/bar"
|
|
60
|
+
spritemap={spritemap}
|
|
61
|
+
>
|
|
62
|
+
{'Label Closable'}
|
|
63
|
+
</ClayLabel>
|
|
64
|
+
);
|
|
65
|
+
|
|
66
|
+
expect(testRenderer.toJSON()).toMatchSnapshot();
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it('renders with ItemBefore', () => {
|
|
70
|
+
const testRenderer = TestRenderer.create(
|
|
71
|
+
<ClayLabel withClose={false}>
|
|
72
|
+
<ClayLabel.ItemBefore>{'Content before'}</ClayLabel.ItemBefore>
|
|
73
|
+
<ClayLabel.ItemExpand>{'Label'}</ClayLabel.ItemExpand>
|
|
74
|
+
</ClayLabel>
|
|
75
|
+
);
|
|
76
|
+
|
|
77
|
+
expect(testRenderer.toJSON()).toMatchSnapshot();
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it('renders with ItemAfter', () => {
|
|
81
|
+
const testRenderer = TestRenderer.create(
|
|
82
|
+
<ClayLabel withClose={false}>
|
|
83
|
+
<ClayLabel.ItemExpand>{'Label'}</ClayLabel.ItemExpand>
|
|
84
|
+
<ClayLabel.ItemAfter>{'Content after'}</ClayLabel.ItemAfter>
|
|
85
|
+
</ClayLabel>
|
|
86
|
+
);
|
|
87
|
+
|
|
88
|
+
expect(testRenderer.toJSON()).toMatchSnapshot();
|
|
89
|
+
});
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
describe('Interactions', () => {
|
|
93
|
+
afterEach(cleanup);
|
|
94
|
+
|
|
95
|
+
it('clicking the close button should call closeButtonProps.onClick', () => {
|
|
96
|
+
const handleClose = jest.fn();
|
|
97
|
+
|
|
98
|
+
const {container} = render(
|
|
99
|
+
<ClayLabel
|
|
100
|
+
closeButtonProps={{onClick: handleClose}}
|
|
101
|
+
spritemap="foo/bar"
|
|
102
|
+
/>
|
|
103
|
+
);
|
|
104
|
+
|
|
105
|
+
const closeButton = container.querySelector('button');
|
|
106
|
+
|
|
107
|
+
fireEvent.click(closeButton as HTMLButtonElement, {});
|
|
108
|
+
|
|
109
|
+
expect(handleClose).toHaveBeenCalledTimes(1);
|
|
110
|
+
});
|
|
111
|
+
});
|
package/src/index.tsx
ADDED
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SPDX-FileCopyrightText: © 2019 Liferay, Inc. <https://liferay.com>
|
|
3
|
+
* SPDX-License-Identifier: BSD-3-Clause
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import ClayIcon from '@clayui/icon';
|
|
7
|
+
import ClayLink from '@clayui/link';
|
|
8
|
+
import classNames from 'classnames';
|
|
9
|
+
import React from 'react';
|
|
10
|
+
|
|
11
|
+
const ClayLabelItemAfter = React.forwardRef<
|
|
12
|
+
HTMLSpanElement,
|
|
13
|
+
React.HTMLAttributes<HTMLSpanElement>
|
|
14
|
+
>(({children, className, ...otherProps}, ref) => (
|
|
15
|
+
<span
|
|
16
|
+
{...otherProps}
|
|
17
|
+
className={classNames(className, 'label-item label-item-after')}
|
|
18
|
+
ref={ref}
|
|
19
|
+
>
|
|
20
|
+
{children}
|
|
21
|
+
</span>
|
|
22
|
+
));
|
|
23
|
+
|
|
24
|
+
ClayLabelItemAfter.displayName = 'ClayLabelItemAfter';
|
|
25
|
+
|
|
26
|
+
const ClayLabelItemBefore = React.forwardRef<
|
|
27
|
+
HTMLSpanElement,
|
|
28
|
+
React.HTMLAttributes<HTMLSpanElement>
|
|
29
|
+
>(({children, className, ...otherProps}, ref) => (
|
|
30
|
+
<span
|
|
31
|
+
{...otherProps}
|
|
32
|
+
className={classNames(className, 'label-item label-item-before')}
|
|
33
|
+
ref={ref}
|
|
34
|
+
>
|
|
35
|
+
{children}
|
|
36
|
+
</span>
|
|
37
|
+
));
|
|
38
|
+
|
|
39
|
+
ClayLabelItemBefore.displayName = 'ClayLabelItemBefore';
|
|
40
|
+
|
|
41
|
+
const ClayLabelItemExpand = React.forwardRef<
|
|
42
|
+
HTMLAnchorElement | HTMLSpanElement,
|
|
43
|
+
React.BaseHTMLAttributes<HTMLAnchorElement | HTMLSpanElement>
|
|
44
|
+
>(({children, className, href, ...otherProps}, ref) => {
|
|
45
|
+
const TagName = href ? ClayLink : 'span';
|
|
46
|
+
|
|
47
|
+
return (
|
|
48
|
+
<TagName
|
|
49
|
+
{...otherProps}
|
|
50
|
+
className={classNames(className, 'label-item label-item-expand')}
|
|
51
|
+
href={href}
|
|
52
|
+
ref={ref as React.ComponentProps<typeof ClayLink>['ref']}
|
|
53
|
+
>
|
|
54
|
+
{children}
|
|
55
|
+
</TagName>
|
|
56
|
+
);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
ClayLabelItemExpand.displayName = 'ClayLabelItemExpand';
|
|
60
|
+
|
|
61
|
+
type DisplayType =
|
|
62
|
+
| 'secondary'
|
|
63
|
+
| 'info'
|
|
64
|
+
| 'warning'
|
|
65
|
+
| 'danger'
|
|
66
|
+
| 'success'
|
|
67
|
+
| 'unstyled';
|
|
68
|
+
|
|
69
|
+
interface IBaseProps extends React.BaseHTMLAttributes<HTMLSpanElement> {
|
|
70
|
+
/**
|
|
71
|
+
* Flag to indicate if `label-dismissible` class should be applied.
|
|
72
|
+
*/
|
|
73
|
+
dismissible?: boolean;
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Determines the style of the label.
|
|
77
|
+
*/
|
|
78
|
+
displayType?: DisplayType;
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Flag to indicate if the label should be of the `large` variant.
|
|
82
|
+
*/
|
|
83
|
+
large?: boolean;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const ClayLabel = React.forwardRef<HTMLSpanElement, IBaseProps>(
|
|
87
|
+
(
|
|
88
|
+
{
|
|
89
|
+
children,
|
|
90
|
+
className,
|
|
91
|
+
dismissible,
|
|
92
|
+
displayType = 'secondary',
|
|
93
|
+
large = false,
|
|
94
|
+
...otherProps
|
|
95
|
+
},
|
|
96
|
+
ref
|
|
97
|
+
) => {
|
|
98
|
+
return (
|
|
99
|
+
<span
|
|
100
|
+
{...otherProps}
|
|
101
|
+
className={classNames('label', className, {
|
|
102
|
+
'label-dismissible': dismissible,
|
|
103
|
+
'label-lg': large,
|
|
104
|
+
[`label-${displayType}`]: displayType,
|
|
105
|
+
})}
|
|
106
|
+
ref={ref}
|
|
107
|
+
>
|
|
108
|
+
{children}
|
|
109
|
+
</span>
|
|
110
|
+
);
|
|
111
|
+
}
|
|
112
|
+
);
|
|
113
|
+
|
|
114
|
+
ClayLabel.displayName = 'ClayLabel';
|
|
115
|
+
|
|
116
|
+
interface IProps extends IBaseProps {
|
|
117
|
+
/**
|
|
118
|
+
* HTML properties that are applied to the 'x' button.
|
|
119
|
+
*/
|
|
120
|
+
closeButtonProps?: React.ButtonHTMLAttributes<HTMLButtonElement> & {
|
|
121
|
+
ref?: (instance: HTMLButtonElement | null) => void;
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Pros to add to the inner label item
|
|
126
|
+
*/
|
|
127
|
+
innerElementProps?: React.ComponentProps<typeof ClayLabelItemExpand>;
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Path to the location of the spritemap resource used for Icon.
|
|
131
|
+
*/
|
|
132
|
+
spritemap?: string;
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Flag to indicate if component should include the close button
|
|
136
|
+
*/
|
|
137
|
+
withClose?: boolean;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
const ClayLabelHybrid = React.forwardRef<
|
|
141
|
+
HTMLAnchorElement | HTMLSpanElement,
|
|
142
|
+
IProps
|
|
143
|
+
>(
|
|
144
|
+
(
|
|
145
|
+
{
|
|
146
|
+
children,
|
|
147
|
+
closeButtonProps,
|
|
148
|
+
href,
|
|
149
|
+
innerElementProps = {},
|
|
150
|
+
withClose = true,
|
|
151
|
+
spritemap,
|
|
152
|
+
...otherProps
|
|
153
|
+
}: IProps,
|
|
154
|
+
ref
|
|
155
|
+
) => {
|
|
156
|
+
return (
|
|
157
|
+
<ClayLabel
|
|
158
|
+
dismissible={withClose && !!closeButtonProps}
|
|
159
|
+
{...otherProps}
|
|
160
|
+
ref={ref}
|
|
161
|
+
>
|
|
162
|
+
{!withClose && children}
|
|
163
|
+
|
|
164
|
+
{withClose && (
|
|
165
|
+
<>
|
|
166
|
+
<ClayLabelItemExpand {...innerElementProps} href={href}>
|
|
167
|
+
{children}
|
|
168
|
+
</ClayLabelItemExpand>
|
|
169
|
+
|
|
170
|
+
{closeButtonProps && (
|
|
171
|
+
<ClayLabelItemAfter>
|
|
172
|
+
<button
|
|
173
|
+
{...closeButtonProps}
|
|
174
|
+
className={classNames(
|
|
175
|
+
closeButtonProps.className,
|
|
176
|
+
'close'
|
|
177
|
+
)}
|
|
178
|
+
type="button"
|
|
179
|
+
>
|
|
180
|
+
<ClayIcon
|
|
181
|
+
spritemap={spritemap}
|
|
182
|
+
symbol="times-small"
|
|
183
|
+
/>
|
|
184
|
+
</button>
|
|
185
|
+
</ClayLabelItemAfter>
|
|
186
|
+
)}
|
|
187
|
+
</>
|
|
188
|
+
)}
|
|
189
|
+
</ClayLabel>
|
|
190
|
+
);
|
|
191
|
+
}
|
|
192
|
+
);
|
|
193
|
+
|
|
194
|
+
ClayLabelHybrid.displayName = 'ClayLabel';
|
|
195
|
+
|
|
196
|
+
export default Object.assign(ClayLabelHybrid, {
|
|
197
|
+
ItemAfter: ClayLabelItemAfter,
|
|
198
|
+
ItemBefore: ClayLabelItemBefore,
|
|
199
|
+
ItemExpand: ClayLabelItemExpand,
|
|
200
|
+
});
|