@pushwoosh/dumb-components 4.17.2 → 4.18.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/PillSelector/PillSelector.d.ts +4 -0
- package/PillSelector/PillSelector.js +86 -0
- package/PillSelector/components/Pill/Pill.d.ts +7 -0
- package/PillSelector/components/Pill/Pill.js +27 -0
- package/PillSelector/components/Pill/index.d.ts +2 -0
- package/PillSelector/components/Pill/index.js +1 -0
- package/PillSelector/components/Pill/styles.d.ts +4 -0
- package/PillSelector/components/Pill/styles.js +52 -0
- package/PillSelector/components/Pill/types.d.ts +18 -0
- package/PillSelector/components/Pill/types.js +1 -0
- package/PillSelector/index.d.ts +3 -0
- package/PillSelector/index.js +2 -0
- package/PillSelector/styles.d.ts +6 -0
- package/PillSelector/styles.js +9 -0
- package/PillSelector/types.d.ts +53 -0
- package/PillSelector/types.js +1 -0
- package/index.d.ts +1 -0
- package/index.js +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { type ReactElement } from 'react';
|
|
2
|
+
import type { PillSelectorProps } from './types';
|
|
3
|
+
/** Row of pills with an optional field-box header; `mode="radio"` picks one value, `mode="checkbox"` any number. */
|
|
4
|
+
export declare function PillSelector(props: PillSelectorProps): ReactElement;
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { useId, useRef } from 'react';
|
|
3
|
+
import { Text } from '@pushwoosh/kit-typography';
|
|
4
|
+
import { Pill } from './components/Pill';
|
|
5
|
+
import { Container, PillRow } from './styles';
|
|
6
|
+
const STEP_BY_KEY = {
|
|
7
|
+
ArrowRight: 1,
|
|
8
|
+
ArrowDown: 1,
|
|
9
|
+
ArrowLeft: -1,
|
|
10
|
+
ArrowUp: -1
|
|
11
|
+
};
|
|
12
|
+
/** Row of pills with an optional field-box header; `mode="radio"` picks one value, `mode="checkbox"` any number. */
|
|
13
|
+
export function PillSelector(props) {
|
|
14
|
+
const {
|
|
15
|
+
title,
|
|
16
|
+
items,
|
|
17
|
+
isDisabled = false,
|
|
18
|
+
isReadOnly = false,
|
|
19
|
+
className,
|
|
20
|
+
'aria-label': ariaLabel
|
|
21
|
+
} = props;
|
|
22
|
+
const isRadio = props.mode !== 'checkbox';
|
|
23
|
+
const hasTitle = title !== undefined && title !== null && title !== false;
|
|
24
|
+
const titleId = useId();
|
|
25
|
+
const pillsRef = useRef([]);
|
|
26
|
+
const isSelected = value => props.mode === 'checkbox' ? props.value.includes(value) : props.value === value;
|
|
27
|
+
const isItemDisabled = item => isDisabled || !!item.isDisabled;
|
|
28
|
+
const select = value => {
|
|
29
|
+
if (isReadOnly) {
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
if (props.mode === 'checkbox') {
|
|
33
|
+
props.onChange(isSelected(value) ? props.value.filter(selected => selected !== value) : [...props.value, value]);
|
|
34
|
+
} else if (props.value !== value) {
|
|
35
|
+
props.onChange(value);
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
const enabledIndexes = items.flatMap((item, index) => isItemDisabled(item) ? [] : [index]);
|
|
39
|
+
const selectedIndex = enabledIndexes.find(index => isSelected(items[index].value));
|
|
40
|
+
const tabStopIndex = selectedIndex ?? enabledIndexes[0];
|
|
41
|
+
const moveSelection = (event, index) => {
|
|
42
|
+
const position = enabledIndexes.indexOf(index);
|
|
43
|
+
let target;
|
|
44
|
+
if (event.key in STEP_BY_KEY) {
|
|
45
|
+
const count = enabledIndexes.length;
|
|
46
|
+
target = enabledIndexes[(position + STEP_BY_KEY[event.key] + count) % count];
|
|
47
|
+
} else if (event.key === 'Home') {
|
|
48
|
+
target = enabledIndexes[0];
|
|
49
|
+
} else if (event.key === 'End') {
|
|
50
|
+
target = enabledIndexes[enabledIndexes.length - 1];
|
|
51
|
+
}
|
|
52
|
+
if (target === undefined) {
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
event.preventDefault();
|
|
56
|
+
pillsRef.current[target]?.focus();
|
|
57
|
+
select(items[target].value);
|
|
58
|
+
};
|
|
59
|
+
return _jsxs(Container, {
|
|
60
|
+
className: className,
|
|
61
|
+
children: [hasTitle && _jsx(Text, {
|
|
62
|
+
id: titleId,
|
|
63
|
+
"$variant": "h4",
|
|
64
|
+
children: title
|
|
65
|
+
}), _jsx(PillRow, {
|
|
66
|
+
role: isRadio ? 'radiogroup' : 'group',
|
|
67
|
+
"aria-labelledby": hasTitle ? titleId : undefined,
|
|
68
|
+
"aria-label": ariaLabel,
|
|
69
|
+
"aria-disabled": isDisabled || undefined,
|
|
70
|
+
"aria-readonly": isRadio && isReadOnly ? true : undefined,
|
|
71
|
+
children: items.map((item, index) => _jsx(Pill, {
|
|
72
|
+
ref: element => {
|
|
73
|
+
pillsRef.current[index] = element;
|
|
74
|
+
},
|
|
75
|
+
role: isRadio ? 'radio' : undefined,
|
|
76
|
+
tabIndex: isRadio && index !== tabStopIndex ? -1 : 0,
|
|
77
|
+
isSelected: isSelected(item.value),
|
|
78
|
+
isDisabled: isItemDisabled(item),
|
|
79
|
+
isReadOnly: isReadOnly,
|
|
80
|
+
onClick: () => select(item.value),
|
|
81
|
+
onKeyDown: isRadio ? event => moveSelection(event, index) : undefined,
|
|
82
|
+
children: item.label
|
|
83
|
+
}, item.value))
|
|
84
|
+
})]
|
|
85
|
+
});
|
|
86
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/** Selectable pill: a toggle `<button>`, filled when `isSelected`; usually rendered by `PillSelector`. */
|
|
2
|
+
export declare const Pill: import("react").ForwardRefExoticComponent<Omit<import("react").ButtonHTMLAttributes<HTMLButtonElement>, "role" | "disabled"> & {
|
|
3
|
+
isSelected?: boolean;
|
|
4
|
+
isDisabled?: boolean;
|
|
5
|
+
isReadOnly?: boolean;
|
|
6
|
+
role?: "radio";
|
|
7
|
+
} & import("react").RefAttributes<HTMLButtonElement>>;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { forwardRef } from 'react';
|
|
3
|
+
import { PillStyled } from './styles';
|
|
4
|
+
/** Selectable pill: a toggle `<button>`, filled when `isSelected`; usually rendered by `PillSelector`. */
|
|
5
|
+
export const Pill = forwardRef(({
|
|
6
|
+
isSelected = false,
|
|
7
|
+
isDisabled = false,
|
|
8
|
+
isReadOnly = false,
|
|
9
|
+
role,
|
|
10
|
+
onClick,
|
|
11
|
+
children,
|
|
12
|
+
...rest
|
|
13
|
+
}, ref) => _jsx(PillStyled, {
|
|
14
|
+
...rest,
|
|
15
|
+
ref: ref,
|
|
16
|
+
type: "button",
|
|
17
|
+
role: role,
|
|
18
|
+
"aria-checked": role === 'radio' ? isSelected : undefined,
|
|
19
|
+
"aria-pressed": role === 'radio' ? undefined : isSelected,
|
|
20
|
+
disabled: isDisabled,
|
|
21
|
+
"$isSelected": isSelected,
|
|
22
|
+
"$isMuted": isDisabled || isReadOnly,
|
|
23
|
+
"$isDisabled": isDisabled,
|
|
24
|
+
onClick: isReadOnly ? undefined : onClick,
|
|
25
|
+
children: children
|
|
26
|
+
}));
|
|
27
|
+
Pill.displayName = 'Pill';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { Pill } from './Pill';
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { PillStyledProps } from './types';
|
|
2
|
+
export declare const PillStyled: import("styled-components/dist/types").IStyledComponentBase<"web", import("styled-components/dist/types").Merged<Omit<import("react").DetailedHTMLProps<import("react").ButtonHTMLAttributes<HTMLButtonElement>, HTMLButtonElement>, "style"> & {
|
|
3
|
+
style?: import("react").CSSProperties | import("styled-components/dist/types").CSSPropertiesWithVars | undefined;
|
|
4
|
+
}, PillStyledProps>> & string;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import styled, { css } from 'styled-components';
|
|
2
|
+
import { Color, FontFamily, FontSize, FontWeight, LineHeight, ShapeRadius } from '@pushwoosh/kit-constants';
|
|
3
|
+
const palette = {
|
|
4
|
+
active: {
|
|
5
|
+
on: {
|
|
6
|
+
color: Color.CLEAR,
|
|
7
|
+
background: Color.PRIMARY
|
|
8
|
+
},
|
|
9
|
+
off: {
|
|
10
|
+
color: Color.PRIMARY,
|
|
11
|
+
background: Color.BRIGHT_LIGHT
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
hover: {
|
|
15
|
+
on: {
|
|
16
|
+
color: Color.CLEAR,
|
|
17
|
+
background: Color.PRIMARY_HOVER
|
|
18
|
+
},
|
|
19
|
+
off: {
|
|
20
|
+
color: Color.PRIMARY_HOVER,
|
|
21
|
+
background: Color.BRIGHT_LIGHT_HOVER
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
muted: {
|
|
25
|
+
on: {
|
|
26
|
+
color: Color.CLEAR,
|
|
27
|
+
background: Color.MAIN
|
|
28
|
+
},
|
|
29
|
+
off: {
|
|
30
|
+
color: Color.MAIN,
|
|
31
|
+
background: Color.SOFT_LIGHT
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
const colors = kind => ({
|
|
36
|
+
$isSelected
|
|
37
|
+
}) => {
|
|
38
|
+
const {
|
|
39
|
+
color,
|
|
40
|
+
background
|
|
41
|
+
} = palette[kind][$isSelected ? 'on' : 'off'];
|
|
42
|
+
return `color: ${color}; background: ${background};`;
|
|
43
|
+
};
|
|
44
|
+
const interactive = css(["cursor:pointer;&:hover,&:active{", "}&:active{opacity:0.5;}"], colors('hover'));
|
|
45
|
+
export const PillStyled = styled.button.withConfig({
|
|
46
|
+
displayName: "PillStyled",
|
|
47
|
+
componentId: "sc-e4iqh8-0"
|
|
48
|
+
})(["appearance:none;display:inline-flex;align-items:center;flex-shrink:0;min-height:24px;margin:0;padding:0 8px;border:0;border-radius:", ";font-family:", ";font-size:", ";font-weight:", ";line-height:", ";white-space:nowrap;cursor:default;", " opacity:", ";", " &:disabled{cursor:not-allowed;}&:focus-visible{outline:1px solid ", ";outline-offset:2px;}"], ShapeRadius.CONTROL, FontFamily.MAIN, FontSize.REGULAR, FontWeight.REGULAR, LineHeight.REGULAR, props => colors(props.$isMuted ? 'muted' : 'active')(props), ({
|
|
49
|
+
$isDisabled
|
|
50
|
+
}) => $isDisabled ? 0.5 : 1, ({
|
|
51
|
+
$isMuted
|
|
52
|
+
}) => !$isMuted && interactive, Color.BRIGHT);
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { ButtonHTMLAttributes } from 'react';
|
|
2
|
+
/** Props of {@link Pill} — `<button>` attributes plus the pill state. */
|
|
3
|
+
export type PillProps = Omit<ButtonHTMLAttributes<HTMLButtonElement>, 'disabled' | 'role'> & {
|
|
4
|
+
/** Selected (on) state: filled primary pill, reported as `aria-checked` for `role="radio"`, else `aria-pressed`. */
|
|
5
|
+
isSelected?: boolean;
|
|
6
|
+
/** Disabled: read-only colors at 50% opacity, no interaction. */
|
|
7
|
+
isDisabled?: boolean;
|
|
8
|
+
/** Read-only: muted colors, keeps focus but ignores clicks. */
|
|
9
|
+
isReadOnly?: boolean;
|
|
10
|
+
/** `radio` for a pill inside a radiogroup; a toggle button otherwise. */
|
|
11
|
+
role?: 'radio';
|
|
12
|
+
};
|
|
13
|
+
/** Internal style props of the pill. */
|
|
14
|
+
export type PillStyledProps = {
|
|
15
|
+
$isSelected: boolean;
|
|
16
|
+
$isMuted: boolean;
|
|
17
|
+
$isDisabled: boolean;
|
|
18
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export declare const Container: import("styled-components/dist/types").IStyledComponentBase<"web", Omit<import("react").DetailedHTMLProps<import("react").HTMLAttributes<HTMLDivElement>, HTMLDivElement>, "style"> & {
|
|
2
|
+
style?: import("react").CSSProperties | import("styled-components/dist/types").CSSPropertiesWithVars | undefined;
|
|
3
|
+
}> & string;
|
|
4
|
+
export declare const PillRow: import("styled-components/dist/types").IStyledComponentBase<"web", Omit<import("react").DetailedHTMLProps<import("react").HTMLAttributes<HTMLDivElement>, HTMLDivElement>, "style"> & {
|
|
5
|
+
style?: import("react").CSSProperties | import("styled-components/dist/types").CSSPropertiesWithVars | undefined;
|
|
6
|
+
}> & string;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import styled from 'styled-components';
|
|
2
|
+
export const Container = styled.div.withConfig({
|
|
3
|
+
displayName: "Container",
|
|
4
|
+
componentId: "sc-1mbz7ub-0"
|
|
5
|
+
})(["display:flex;flex-direction:column;align-items:flex-start;gap:8px;"]);
|
|
6
|
+
export const PillRow = styled.div.withConfig({
|
|
7
|
+
displayName: "PillRow",
|
|
8
|
+
componentId: "sc-1mbz7ub-1"
|
|
9
|
+
})(["display:flex;flex-wrap:wrap;align-items:center;gap:4px;width:100%;"]);
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import type { ReactNode } from 'react';
|
|
2
|
+
/** One option of a {@link PillSelector}. */
|
|
3
|
+
export type PillSelectorItem = {
|
|
4
|
+
/** Value reported to `onChange`. */
|
|
5
|
+
value: string;
|
|
6
|
+
/** Pill text. */
|
|
7
|
+
label: ReactNode;
|
|
8
|
+
/** Disable this pill only. */
|
|
9
|
+
isDisabled?: boolean;
|
|
10
|
+
};
|
|
11
|
+
/** Accessible name of the group: the visible `title`, or `aria-label` for a bare row. */
|
|
12
|
+
type PillSelectorNameProps = {
|
|
13
|
+
/** Field-box header above the pills; it also names the group. */
|
|
14
|
+
title: ReactNode;
|
|
15
|
+
/** Accessible name; overridden by `title` for screen readers. */
|
|
16
|
+
'aria-label'?: string;
|
|
17
|
+
} | {
|
|
18
|
+
/** Optional header above the pills. */
|
|
19
|
+
title?: ReactNode;
|
|
20
|
+
/** Accessible name, required when there is no `title`. */
|
|
21
|
+
'aria-label': string;
|
|
22
|
+
};
|
|
23
|
+
type PillSelectorCommonProps = PillSelectorNameProps & {
|
|
24
|
+
/** Options, rendered in order and wrapped onto new lines when they do not fit. */
|
|
25
|
+
items: PillSelectorItem[];
|
|
26
|
+
/** Disable every pill. */
|
|
27
|
+
isDisabled?: boolean;
|
|
28
|
+
/** Show the selection without letting it change. */
|
|
29
|
+
isReadOnly?: boolean;
|
|
30
|
+
/** Class of the outer wrapper. */
|
|
31
|
+
className?: string;
|
|
32
|
+
};
|
|
33
|
+
/** Radio mode: exactly one pill is selected, a click on the selected one changes nothing. */
|
|
34
|
+
export type PillSelectorRadioProps = PillSelectorCommonProps & {
|
|
35
|
+
/** Selection mode (default `radio`). */
|
|
36
|
+
mode?: 'radio';
|
|
37
|
+
/** Selected value (controlled). */
|
|
38
|
+
value: string | null | undefined;
|
|
39
|
+
/** Fires with the newly selected value. */
|
|
40
|
+
onChange: (value: string) => void;
|
|
41
|
+
};
|
|
42
|
+
/** Checkbox mode: any number of pills is selected, a click toggles one. */
|
|
43
|
+
export type PillSelectorCheckboxProps = PillSelectorCommonProps & {
|
|
44
|
+
/** Selection mode. */
|
|
45
|
+
mode: 'checkbox';
|
|
46
|
+
/** Selected values (controlled). */
|
|
47
|
+
value: string[];
|
|
48
|
+
/** Fires with the next selection; a newly checked value is appended. */
|
|
49
|
+
onChange: (value: string[]) => void;
|
|
50
|
+
};
|
|
51
|
+
/** Props of {@link PillSelector}. */
|
|
52
|
+
export type PillSelectorProps = PillSelectorRadioProps | PillSelectorCheckboxProps;
|
|
53
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/index.d.ts
CHANGED
|
@@ -57,6 +57,7 @@ export { NumberPicker } from './NumberPicker';
|
|
|
57
57
|
export { CodeEditor, json as jsonLanguage, html as htmlLanguage } from './CodeEditor';
|
|
58
58
|
export { EnhancedInput, SearchInput } from './EnhancedInput';
|
|
59
59
|
export { Chip, type ChipProps, type ChipVariant } from './Chip';
|
|
60
|
+
export { PillSelector, Pill, type PillProps, type PillSelectorProps, type PillSelectorRadioProps, type PillSelectorCheckboxProps, type PillSelectorItem, } from './PillSelector';
|
|
60
61
|
export { RichmediaPreview, useRichmediaHtml } from './RichmediaPreview';
|
|
61
62
|
export { SandboxedHtmlFrame, type SandboxedHtmlFrameProps, type IsolatedSandboxedHtmlFrameProps, type InspectableSandboxedHtmlFrameProps, } from './SandboxedHtmlFrame';
|
|
62
63
|
export { NativeRichMediaPreview, parseNativeRichMediaConfig, parseLocalizationParams, NATIVE_RICH_MEDIA_DISPLAY_TYPES, EDITABLE_NATIVE_RICH_MEDIA_DISPLAY_TYPES, } from './NativeRichMediaPreview';
|
package/index.js
CHANGED
|
@@ -51,6 +51,7 @@ export { NumberPicker } from './NumberPicker';
|
|
|
51
51
|
export { CodeEditor, json as jsonLanguage, html as htmlLanguage } from './CodeEditor';
|
|
52
52
|
export { EnhancedInput, SearchInput } from './EnhancedInput';
|
|
53
53
|
export { Chip } from './Chip';
|
|
54
|
+
export { PillSelector, Pill } from './PillSelector';
|
|
54
55
|
export { RichmediaPreview, useRichmediaHtml } from './RichmediaPreview';
|
|
55
56
|
export { SandboxedHtmlFrame } from './SandboxedHtmlFrame';
|
|
56
57
|
export { NativeRichMediaPreview, parseNativeRichMediaConfig, parseLocalizationParams, NATIVE_RICH_MEDIA_DISPLAY_TYPES, EDITABLE_NATIVE_RICH_MEDIA_DISPLAY_TYPES } from './NativeRichMediaPreview';
|