@opengovsg/oui 0.0.7 → 0.0.9
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/dist/cjs/badge/badge.cjs +42 -0
- package/dist/cjs/badge/index.cjs +8 -0
- package/dist/cjs/badge/use-badge.cjs +111 -0
- package/dist/cjs/calendar/calendar-base.cjs +347 -0
- package/dist/cjs/calendar/calendar-style-context.cjs +38 -0
- package/dist/cjs/calendar/calendar.cjs +16 -0
- package/dist/cjs/calendar/index.cjs +22 -0
- package/dist/cjs/calendar/types.cjs +3 -0
- package/dist/cjs/calendar/utils.cjs +62 -0
- package/dist/cjs/combo-box/combo-box-fuzzy.cjs +52 -30
- package/dist/cjs/combo-box/combo-box-item.cjs +70 -0
- package/dist/cjs/combo-box/combo-box-variant-context.cjs +13 -0
- package/dist/cjs/combo-box/combo-box.cjs +46 -100
- package/dist/cjs/combo-box/index.cjs +5 -1
- package/dist/cjs/index.cjs +30 -1
- package/dist/cjs/node_modules/.pnpm/lucide-react@0.475.0_react@19.0.0/node_modules/lucide-react/dist/esm/icons/{check.cjs → chevron-left.cjs} +3 -3
- package/dist/cjs/node_modules/.pnpm/lucide-react@0.475.0_react@19.0.0/node_modules/lucide-react/dist/esm/icons/{chevrons-up-down.cjs → chevron-right.cjs} +3 -6
- package/dist/cjs/select/index.cjs +5 -0
- package/dist/cjs/select/select-item.cjs +12 -24
- package/dist/cjs/select/select-variant-context.cjs +1 -0
- package/dist/cjs/select/select.cjs +14 -6
- package/dist/cjs/system/react-utils/index.cjs +2 -0
- package/dist/cjs/system/react-utils/refs.cjs +12 -0
- package/dist/cjs/system/utils.cjs +9 -6
- package/dist/cjs/tag-field/index.cjs +2 -0
- package/dist/cjs/tag-field/tag-field-item.cjs +28 -0
- package/dist/cjs/tag-field/tag-field-list.cjs +11 -21
- package/dist/cjs/tag-field/tag-field-tag-list.cjs +3 -2
- package/dist/cjs/tag-field/tag-field-trigger.cjs +1 -0
- package/dist/cjs/tag-field/tag-field.cjs +2 -1
- package/dist/esm/badge/badge.js +40 -0
- package/dist/esm/badge/index.js +2 -0
- package/dist/esm/badge/use-badge.js +109 -0
- package/dist/esm/calendar/calendar-base.js +343 -0
- package/dist/esm/calendar/calendar-style-context.js +34 -0
- package/dist/esm/calendar/calendar.js +14 -0
- package/dist/esm/calendar/index.js +5 -0
- package/dist/esm/calendar/types.js +1 -0
- package/dist/esm/calendar/utils.js +57 -0
- package/dist/esm/combo-box/combo-box-fuzzy.js +55 -33
- package/dist/esm/combo-box/combo-box-item.js +68 -0
- package/dist/esm/combo-box/combo-box-variant-context.js +10 -0
- package/dist/esm/combo-box/combo-box.js +48 -101
- package/dist/esm/combo-box/index.js +3 -1
- package/dist/esm/index.js +11 -1
- package/dist/esm/node_modules/.pnpm/lucide-react@0.475.0_react@19.0.0/node_modules/lucide-react/dist/esm/icons/{check.js → chevron-left.js} +3 -3
- package/dist/esm/node_modules/.pnpm/lucide-react@0.475.0_react@19.0.0/node_modules/lucide-react/dist/esm/icons/chevron-right.js +14 -0
- package/dist/esm/select/index.js +2 -0
- package/dist/esm/select/select-item.js +13 -25
- package/dist/esm/select/select-variant-context.js +1 -0
- package/dist/esm/select/select.js +15 -7
- package/dist/esm/system/react-utils/index.js +1 -0
- package/dist/esm/system/react-utils/refs.js +10 -0
- package/dist/esm/system/utils.js +9 -6
- package/dist/esm/tag-field/index.js +1 -0
- package/dist/esm/tag-field/tag-field-item.js +26 -0
- package/dist/esm/tag-field/tag-field-list.js +12 -21
- package/dist/esm/tag-field/tag-field-tag-list.js +3 -2
- package/dist/esm/tag-field/tag-field-trigger.js +1 -0
- package/dist/esm/tag-field/tag-field.js +3 -2
- package/dist/types/badge/badge.d.ts +3 -0
- package/dist/types/badge/badge.d.ts.map +1 -0
- package/dist/types/badge/index.d.ts +2 -0
- package/dist/types/badge/index.d.ts.map +1 -0
- package/dist/types/badge/use-badge.d.ts +166 -0
- package/dist/types/badge/use-badge.d.ts.map +1 -0
- package/dist/types/button/button.d.ts +5 -0
- package/dist/types/button/button.d.ts.map +1 -1
- package/dist/types/calendar/calendar-base.d.ts +14 -0
- package/dist/types/calendar/calendar-base.d.ts.map +1 -0
- package/dist/types/calendar/calendar-style-context.d.ts +1032 -0
- package/dist/types/calendar/calendar-style-context.d.ts.map +1 -0
- package/dist/types/calendar/calendar.d.ts +5 -0
- package/dist/types/calendar/calendar.d.ts.map +1 -0
- package/dist/types/calendar/index.d.ts +6 -0
- package/dist/types/calendar/index.d.ts.map +1 -0
- package/dist/types/calendar/types.d.ts +58 -0
- package/dist/types/calendar/types.d.ts.map +1 -0
- package/dist/types/calendar/utils.d.ts +13 -0
- package/dist/types/calendar/utils.d.ts.map +1 -0
- package/dist/types/combo-box/combo-box-fuzzy.d.ts +12 -4
- package/dist/types/combo-box/combo-box-fuzzy.d.ts.map +1 -1
- package/dist/types/combo-box/combo-box-item.d.ts +11 -0
- package/dist/types/combo-box/combo-box-item.d.ts.map +1 -0
- package/dist/types/combo-box/combo-box-variant-context.d.ts +4 -0
- package/dist/types/combo-box/combo-box-variant-context.d.ts.map +1 -0
- package/dist/types/combo-box/combo-box.d.ts +5 -21
- package/dist/types/combo-box/combo-box.d.ts.map +1 -1
- package/dist/types/combo-box/index.d.ts +2 -0
- package/dist/types/combo-box/index.d.ts.map +1 -1
- package/dist/types/index.d.mts +2 -0
- package/dist/types/index.d.ts +2 -0
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/select/index.d.ts +2 -0
- package/dist/types/select/index.d.ts.map +1 -1
- package/dist/types/select/select-item.d.ts.map +1 -1
- package/dist/types/select/select-variant-context.d.ts.map +1 -1
- package/dist/types/select/select.d.ts.map +1 -1
- package/dist/types/system/react-utils/index.d.ts +1 -0
- package/dist/types/system/react-utils/index.d.ts.map +1 -1
- package/dist/types/system/react-utils/refs.d.ts +4 -0
- package/dist/types/system/react-utils/refs.d.ts.map +1 -0
- package/dist/types/system/utils.d.ts.map +1 -1
- package/dist/types/tag-field/index.d.ts +1 -0
- package/dist/types/tag-field/index.d.ts.map +1 -1
- package/dist/types/tag-field/tag-field-item.d.ts +7 -0
- package/dist/types/tag-field/tag-field-item.d.ts.map +1 -0
- package/dist/types/tag-field/tag-field-list.d.ts +5 -9
- package/dist/types/tag-field/tag-field-list.d.ts.map +1 -1
- package/dist/types/tag-field/tag-field-root.d.ts +4 -4
- package/dist/types/tag-field/tag-field-root.d.ts.map +1 -1
- package/dist/types/tag-field/tag-field-state-context.d.ts +1 -2
- package/dist/types/tag-field/tag-field-state-context.d.ts.map +1 -1
- package/dist/types/tag-field/tag-field-tag-list.d.ts +2 -3
- package/dist/types/tag-field/tag-field-tag-list.d.ts.map +1 -1
- package/dist/types/tag-field/tag-field-trigger.d.ts.map +1 -1
- package/dist/types/tag-field/tag-field.d.ts +2 -2
- package/dist/types/tag-field/tag-field.d.ts.map +1 -1
- package/dist/types/tag-field/types.d.ts +6 -9
- package/dist/types/tag-field/types.d.ts.map +1 -1
- package/package.json +8 -6
- package/dist/esm/node_modules/.pnpm/lucide-react@0.475.0_react@19.0.0/node_modules/lucide-react/dist/esm/icons/chevrons-up-down.js +0 -17
|
@@ -8,8 +8,8 @@ var reactAriaComponents = require('react-aria-components');
|
|
|
8
8
|
var ouiTheme = require('@opengovsg/oui-theme');
|
|
9
9
|
var utils = require('../system/utils.cjs');
|
|
10
10
|
var selectVariantContext = require('./select-variant-context.cjs');
|
|
11
|
-
var chevronsUpDown = require('../node_modules/.pnpm/lucide-react@0.475.0_react@19.0.0/node_modules/lucide-react/dist/esm/icons/chevrons-up-down.cjs');
|
|
12
11
|
var field = require('../field/field.cjs');
|
|
12
|
+
var chevronDown = require('../node_modules/.pnpm/lucide-react@0.475.0_react@19.0.0/node_modules/lucide-react/dist/esm/icons/chevron-down.cjs');
|
|
13
13
|
var button = require('../button/button.cjs');
|
|
14
14
|
|
|
15
15
|
const calculateEstimatedRowHeight = (size) => {
|
|
@@ -34,13 +34,13 @@ function Select({
|
|
|
34
34
|
);
|
|
35
35
|
const { items, children, listLayoutOptions, ...props } = _props;
|
|
36
36
|
const styles = ouiTheme.selectStyles(variantProps);
|
|
37
|
-
const
|
|
38
|
-
return
|
|
37
|
+
const layoutOptions = react.useMemo(() => {
|
|
38
|
+
return {
|
|
39
39
|
estimatedRowHeight: calculateEstimatedRowHeight(
|
|
40
40
|
variantProps.size ?? "md"
|
|
41
41
|
),
|
|
42
42
|
...listLayoutOptions
|
|
43
|
-
}
|
|
43
|
+
};
|
|
44
44
|
}, [listLayoutOptions, variantProps.size]);
|
|
45
45
|
return /* @__PURE__ */ jsxRuntime.jsx(reactAriaComponents.Provider, { values: [[selectVariantContext.SelectVariantContext, variantProps]], children: /* @__PURE__ */ jsxRuntime.jsxs(
|
|
46
46
|
reactAriaComponents.Select,
|
|
@@ -71,7 +71,14 @@ function Select({
|
|
|
71
71
|
})
|
|
72
72
|
}
|
|
73
73
|
),
|
|
74
|
-
/* @__PURE__ */ jsxRuntime.jsx(
|
|
74
|
+
/* @__PURE__ */ jsxRuntime.jsx(
|
|
75
|
+
chevronDown.default,
|
|
76
|
+
{
|
|
77
|
+
className: styles.icon({
|
|
78
|
+
className: classNames?.icon
|
|
79
|
+
})
|
|
80
|
+
}
|
|
81
|
+
)
|
|
75
82
|
]
|
|
76
83
|
}
|
|
77
84
|
),
|
|
@@ -83,9 +90,10 @@ function Select({
|
|
|
83
90
|
children: description
|
|
84
91
|
}
|
|
85
92
|
),
|
|
86
|
-
/* @__PURE__ */ jsxRuntime.jsx(reactAriaComponents.Popover, { className: styles.popover({ className: classNames?.popover }), children: /* @__PURE__ */ jsxRuntime.jsx(reactAriaComponents.Virtualizer, { layout, children: /* @__PURE__ */ jsxRuntime.jsx(
|
|
93
|
+
/* @__PURE__ */ jsxRuntime.jsx(reactAriaComponents.Popover, { className: styles.popover({ className: classNames?.popover }), children: /* @__PURE__ */ jsxRuntime.jsx(reactAriaComponents.Virtualizer, { layout: reactAriaComponents.ListLayout, layoutOptions, children: /* @__PURE__ */ jsxRuntime.jsx(
|
|
87
94
|
reactAriaComponents.ListBox,
|
|
88
95
|
{
|
|
96
|
+
autoFocus: true,
|
|
89
97
|
items,
|
|
90
98
|
shouldFocusWrap: true,
|
|
91
99
|
className: ouiTheme.composeRenderProps(
|
|
@@ -13,12 +13,15 @@ const mapPropsVariants = (props, variantKeys, removeVariantProps = true) => {
|
|
|
13
13
|
if (!variantKeys) {
|
|
14
14
|
return [props, {}];
|
|
15
15
|
}
|
|
16
|
-
const picked = variantKeys.reduce(
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
16
|
+
const picked = variantKeys.reduce(
|
|
17
|
+
(acc, key) => {
|
|
18
|
+
if (key in props) {
|
|
19
|
+
return { ...acc, [key]: props[key] };
|
|
20
|
+
}
|
|
21
|
+
return acc;
|
|
22
|
+
},
|
|
23
|
+
{}
|
|
24
|
+
);
|
|
22
25
|
if (removeVariantProps) {
|
|
23
26
|
const omitted = Object.keys(props).filter((key) => !variantKeys.includes(key)).reduce((acc, key) => ({ ...acc, [key]: props[key] }), {});
|
|
24
27
|
return [omitted, picked];
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
"use client";
|
|
3
|
+
'use strict';
|
|
4
|
+
|
|
5
|
+
var jsxRuntime = require('react/jsx-runtime');
|
|
6
|
+
var react = require('react');
|
|
7
|
+
var ouiTheme = require('@opengovsg/oui-theme');
|
|
8
|
+
var utils = require('../system/utils.cjs');
|
|
9
|
+
var tagFieldStateContext = require('./tag-field-state-context.cjs');
|
|
10
|
+
|
|
11
|
+
const TagFieldItemInner = ({ item, isHighlighted, classNames, ...itemProps }, ref) => {
|
|
12
|
+
const { itemToText, size } = react.useContext(tagFieldStateContext.TagFieldStateContext);
|
|
13
|
+
const styles = ouiTheme.tagFieldItemStyles({ size });
|
|
14
|
+
return /* @__PURE__ */ jsxRuntime.jsx(
|
|
15
|
+
"li",
|
|
16
|
+
{
|
|
17
|
+
ref,
|
|
18
|
+
...itemProps,
|
|
19
|
+
className: styles.container({ className: classNames?.container }),
|
|
20
|
+
"data-rac": true,
|
|
21
|
+
"data-hovered": ouiTheme.dataAttr(isHighlighted),
|
|
22
|
+
children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: styles.label({ className: classNames?.label }), children: itemToText(item) })
|
|
23
|
+
}
|
|
24
|
+
);
|
|
25
|
+
};
|
|
26
|
+
const TagFieldItem = utils.forwardRefGeneric(TagFieldItemInner);
|
|
27
|
+
|
|
28
|
+
exports.TagFieldItem = TagFieldItem;
|
|
@@ -1,30 +1,15 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
"use client";
|
|
2
3
|
'use strict';
|
|
3
4
|
|
|
4
5
|
var jsxRuntime = require('react/jsx-runtime');
|
|
5
6
|
var react = require('react');
|
|
6
7
|
var reactAriaComponents = require('react-aria-components');
|
|
7
|
-
var ouiTheme = require('@opengovsg/oui-theme');
|
|
8
8
|
var utils = require('../system/utils.cjs');
|
|
9
|
+
var tagFieldItem = require('./tag-field-item.cjs');
|
|
9
10
|
var tagFieldStateContext = require('./tag-field-state-context.cjs');
|
|
10
11
|
|
|
11
12
|
const TagFieldListContext = react.createContext(null);
|
|
12
|
-
const TagFieldListItemInner = ({ item, isHighlighted, classNames, ...itemProps }, ref) => {
|
|
13
|
-
const { itemToText, size } = react.useContext(tagFieldStateContext.TagFieldStateContext);
|
|
14
|
-
const styles = ouiTheme.tagFieldItemStyles({ size });
|
|
15
|
-
return /* @__PURE__ */ jsxRuntime.jsx(
|
|
16
|
-
"li",
|
|
17
|
-
{
|
|
18
|
-
ref,
|
|
19
|
-
...itemProps,
|
|
20
|
-
className: styles.container({ className: classNames?.container }),
|
|
21
|
-
"data-rac": true,
|
|
22
|
-
"data-hovered": ouiTheme.dataAttr(isHighlighted),
|
|
23
|
-
children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: styles.label({ className: classNames?.label }), children: itemToText(item) })
|
|
24
|
-
}
|
|
25
|
-
);
|
|
26
|
-
};
|
|
27
|
-
const TagFieldListItem = utils.forwardRefGeneric(TagFieldListItemInner);
|
|
28
13
|
const TagFieldListInner = (props, ref) => {
|
|
29
14
|
[props, ref] = reactAriaComponents.useContextProps(props, ref, TagFieldListContext);
|
|
30
15
|
const { items, getItemProps, highlightedIndex } = react.useContext(tagFieldStateContext.TagFieldStateContext);
|
|
@@ -56,13 +41,19 @@ const TagFieldListInner = (props, ref) => {
|
|
|
56
41
|
item,
|
|
57
42
|
isHighlighted: highlightedIndex === virtualRow.index,
|
|
58
43
|
key: virtualRow.key,
|
|
59
|
-
...itemProps,
|
|
60
44
|
classNames: itemClassNames
|
|
61
45
|
};
|
|
62
46
|
if (typeof props.children === "function") {
|
|
63
|
-
return props.children(childProps);
|
|
47
|
+
return props.children({ ...childProps, itemProps });
|
|
64
48
|
}
|
|
65
|
-
return /* @__PURE__ */ react.createElement(
|
|
49
|
+
return /* @__PURE__ */ react.createElement(
|
|
50
|
+
tagFieldItem.TagFieldItem,
|
|
51
|
+
{
|
|
52
|
+
...childProps,
|
|
53
|
+
...itemProps,
|
|
54
|
+
key: childProps.key
|
|
55
|
+
}
|
|
56
|
+
);
|
|
66
57
|
})
|
|
67
58
|
] }) });
|
|
68
59
|
};
|
|
@@ -70,4 +61,3 @@ const TagFieldList = utils.forwardRefGeneric(TagFieldListInner);
|
|
|
70
61
|
|
|
71
62
|
exports.TagFieldList = TagFieldList;
|
|
72
63
|
exports.TagFieldListContext = TagFieldListContext;
|
|
73
|
-
exports.TagFieldListItem = TagFieldListItem;
|
|
@@ -15,7 +15,8 @@ const TagFieldTagList = ({
|
|
|
15
15
|
getSelectedItemProps,
|
|
16
16
|
removeSelectedItem,
|
|
17
17
|
isDisabled,
|
|
18
|
-
isReadOnly
|
|
18
|
+
isReadOnly,
|
|
19
|
+
itemToText
|
|
19
20
|
} = react.useContext(tagFieldStateContext.TagFieldStateContext);
|
|
20
21
|
const handleRemoveSelectedItem = react.useCallback(
|
|
21
22
|
(item) => () => {
|
|
@@ -49,7 +50,7 @@ const TagFieldTagList = ({
|
|
|
49
50
|
className: classNames?.tag,
|
|
50
51
|
...itemProps,
|
|
51
52
|
children: [
|
|
52
|
-
/* @__PURE__ */ jsxRuntime.jsx("span", { className: classNames?.tagText, children: selectedItem
|
|
53
|
+
/* @__PURE__ */ jsxRuntime.jsx("span", { className: classNames?.tagText, children: itemToText(selectedItem) }),
|
|
53
54
|
/* @__PURE__ */ jsxRuntime.jsx(
|
|
54
55
|
x.default,
|
|
55
56
|
{
|
|
@@ -6,6 +6,7 @@ var jsxRuntime = require('react/jsx-runtime');
|
|
|
6
6
|
var react = require('react');
|
|
7
7
|
var reactAriaComponents = require('react-aria-components');
|
|
8
8
|
var ouiTheme = require('@opengovsg/oui-theme');
|
|
9
|
+
var tagFieldItem = require('./tag-field-item.cjs');
|
|
9
10
|
var tagFieldList = require('./tag-field-list.cjs');
|
|
10
11
|
var tagFieldRoot = require('./tag-field-root.cjs');
|
|
11
12
|
var tagFieldTagList = require('./tag-field-tag-list.cjs');
|
|
@@ -97,7 +98,7 @@ function TagField({
|
|
|
97
98
|
{
|
|
98
99
|
className: styles.list({ className: classNames?.list }),
|
|
99
100
|
itemClassNames: props.itemClassNames,
|
|
100
|
-
children: ({ key, ...props2 }) => children ? children({ key, ...props2 }) : /* @__PURE__ */ react.createElement(
|
|
101
|
+
children: ({ key, itemProps, ...props2 }) => children ? children({ key, itemProps, ...props2 }) : /* @__PURE__ */ react.createElement(tagFieldItem.TagFieldItem, { ...props2, ...itemProps, key })
|
|
101
102
|
}
|
|
102
103
|
) })
|
|
103
104
|
] });
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
"use client";
|
|
3
|
+
import { jsx, jsxs } from 'react/jsx-runtime';
|
|
4
|
+
import { forwardRef, useMemo } from 'react';
|
|
5
|
+
import { useBadge } from './use-badge.js';
|
|
6
|
+
import X from '../node_modules/.pnpm/lucide-react@0.475.0_react@19.0.0/node_modules/lucide-react/dist/esm/icons/x.js';
|
|
7
|
+
|
|
8
|
+
const Badge = forwardRef((props, ref) => {
|
|
9
|
+
const {
|
|
10
|
+
Component,
|
|
11
|
+
children,
|
|
12
|
+
slots,
|
|
13
|
+
classNames,
|
|
14
|
+
getChipProps,
|
|
15
|
+
getCloseButtonProps,
|
|
16
|
+
startContent,
|
|
17
|
+
endContent,
|
|
18
|
+
isCloseable
|
|
19
|
+
} = useBadge({ ...props, ref });
|
|
20
|
+
const start = useMemo(() => {
|
|
21
|
+
if (props.variant === "dot" && !startContent) {
|
|
22
|
+
return /* @__PURE__ */ jsx("span", { className: slots.dot({ className: classNames?.dot }) });
|
|
23
|
+
}
|
|
24
|
+
return startContent;
|
|
25
|
+
}, [props.variant, startContent, slots, classNames?.dot]);
|
|
26
|
+
const end = useMemo(() => {
|
|
27
|
+
if (isCloseable) {
|
|
28
|
+
return /* @__PURE__ */ jsx("span", { ...getCloseButtonProps(), children: endContent ?? /* @__PURE__ */ jsx(X, { className: "size-full" }) });
|
|
29
|
+
}
|
|
30
|
+
return endContent;
|
|
31
|
+
}, [endContent, getCloseButtonProps, isCloseable]);
|
|
32
|
+
return /* @__PURE__ */ jsxs(Component, { ...getChipProps(), children: [
|
|
33
|
+
start,
|
|
34
|
+
/* @__PURE__ */ jsx("span", { className: slots.content({ className: classNames?.content }), children }),
|
|
35
|
+
end
|
|
36
|
+
] });
|
|
37
|
+
});
|
|
38
|
+
Badge.displayName = "Badge";
|
|
39
|
+
|
|
40
|
+
export { Badge };
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
import { useMemo, useCallback, cloneElement, isValidElement } from 'react';
|
|
3
|
+
import { mergeProps } from '@react-aria/utils';
|
|
4
|
+
import { useMessageFormatter, useFocusRing, usePress } from 'react-aria';
|
|
5
|
+
import { useDeepCompareMemo } from 'use-deep-compare';
|
|
6
|
+
import { badgeStyles, cn, badgeCloseButtonStyles } from '@opengovsg/oui-theme';
|
|
7
|
+
import { mapPropsVariants } from '../system/utils.js';
|
|
8
|
+
import { useDomRef } from '../system/react-utils/refs.js';
|
|
9
|
+
|
|
10
|
+
const i18nStrings = {
|
|
11
|
+
"en-SG": {
|
|
12
|
+
close: "Close badge"
|
|
13
|
+
},
|
|
14
|
+
"zh-SG": {
|
|
15
|
+
close: "\u5173\u95ED\u5FBD\u7AE0"
|
|
16
|
+
},
|
|
17
|
+
"ms-SG": {
|
|
18
|
+
close: "Tutup lencana"
|
|
19
|
+
},
|
|
20
|
+
"ta-SG": {
|
|
21
|
+
close: "\u0BAA\u0BC7\u0B9F\u0BCD\u0B9C\u0BC8 \u0BAE\u0BC2\u0B9F\u0BC1"
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
function useBadge(originalProps) {
|
|
25
|
+
const [_props, variantProps] = mapPropsVariants(
|
|
26
|
+
originalProps,
|
|
27
|
+
badgeStyles.variantKeys
|
|
28
|
+
);
|
|
29
|
+
const {
|
|
30
|
+
ref,
|
|
31
|
+
as,
|
|
32
|
+
children,
|
|
33
|
+
startContent,
|
|
34
|
+
endContent,
|
|
35
|
+
onClose,
|
|
36
|
+
classNames,
|
|
37
|
+
className,
|
|
38
|
+
...props
|
|
39
|
+
} = _props;
|
|
40
|
+
const domRef = useDomRef(ref);
|
|
41
|
+
const Component = useMemo(() => as || "div", [as]);
|
|
42
|
+
const baseClassName = cn(classNames?.base, className);
|
|
43
|
+
const formatMessage = useMessageFormatter(i18nStrings);
|
|
44
|
+
const isCloseable = variantProps.isCloseable || !!onClose;
|
|
45
|
+
const {
|
|
46
|
+
focusProps: closeFocusProps,
|
|
47
|
+
isFocusVisible: isCloseButtonFocusVisible
|
|
48
|
+
} = useFocusRing();
|
|
49
|
+
const slots = useDeepCompareMemo(
|
|
50
|
+
() => ({
|
|
51
|
+
...badgeStyles({ isCloseable, ...variantProps }),
|
|
52
|
+
closeButton: badgeCloseButtonStyles
|
|
53
|
+
}),
|
|
54
|
+
[variantProps, isCloseable, badgeStyles, badgeCloseButtonStyles]
|
|
55
|
+
);
|
|
56
|
+
const { pressProps: closePressProps } = usePress({
|
|
57
|
+
isDisabled: !!variantProps?.isDisabled,
|
|
58
|
+
onPress: onClose
|
|
59
|
+
});
|
|
60
|
+
const getContentClone = useCallback(
|
|
61
|
+
(content) => isValidElement(content) ? cloneElement(content, {
|
|
62
|
+
// @ts-expect-error types are not full
|
|
63
|
+
className: content.props?.className
|
|
64
|
+
}) : null,
|
|
65
|
+
[]
|
|
66
|
+
);
|
|
67
|
+
const getChipProps = useCallback(() => {
|
|
68
|
+
return {
|
|
69
|
+
ref: domRef,
|
|
70
|
+
className: slots.base({ className: baseClassName }),
|
|
71
|
+
"aria-disabled": variantProps?.isDisabled,
|
|
72
|
+
...props
|
|
73
|
+
};
|
|
74
|
+
}, [baseClassName, domRef, props, slots, variantProps?.isDisabled]);
|
|
75
|
+
const getCloseButtonProps = useCallback(() => {
|
|
76
|
+
return {
|
|
77
|
+
role: "button",
|
|
78
|
+
tabIndex: 0,
|
|
79
|
+
className: slots.closeButton({
|
|
80
|
+
size: variantProps?.size,
|
|
81
|
+
className: classNames?.closeButton,
|
|
82
|
+
isFocusVisible: isCloseButtonFocusVisible
|
|
83
|
+
}),
|
|
84
|
+
"aria-label": formatMessage("close"),
|
|
85
|
+
...mergeProps(closePressProps, closeFocusProps)
|
|
86
|
+
};
|
|
87
|
+
}, [
|
|
88
|
+
classNames?.closeButton,
|
|
89
|
+
closeFocusProps,
|
|
90
|
+
closePressProps,
|
|
91
|
+
formatMessage,
|
|
92
|
+
isCloseButtonFocusVisible,
|
|
93
|
+
slots,
|
|
94
|
+
variantProps?.size
|
|
95
|
+
]);
|
|
96
|
+
return {
|
|
97
|
+
Component,
|
|
98
|
+
children,
|
|
99
|
+
slots,
|
|
100
|
+
classNames,
|
|
101
|
+
isCloseable,
|
|
102
|
+
startContent: getContentClone(startContent),
|
|
103
|
+
endContent: getContentClone(endContent),
|
|
104
|
+
getCloseButtonProps,
|
|
105
|
+
getChipProps
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export { useBadge };
|