shelving 1.299.0 → 1.301.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/package.json +1 -1
- package/ui/input/ArrayCheckboxInputs.d.ts +26 -0
- package/ui/input/ArrayCheckboxInputs.js +23 -0
- package/ui/input/ArrayCheckboxInputs.tsx +59 -0
- package/ui/input/ImageInput.d.ts +27 -0
- package/ui/input/ImageInput.js +49 -0
- package/ui/input/ImageInput.md +48 -0
- package/ui/input/ImageInput.module.css +79 -0
- package/ui/input/ImageInput.tsx +100 -0
- package/ui/input/Input.module.css +10 -1
- package/ui/input/Input.test.tsx +33 -1
- package/ui/input/QueryCheckboxesInput.d.ts +24 -0
- package/ui/input/QueryCheckboxesInput.js +20 -0
- package/ui/input/QueryCheckboxesInput.md +38 -0
- package/ui/input/QueryCheckboxesInput.test.tsx +42 -0
- package/ui/input/QueryCheckboxesInput.tsx +50 -0
- package/ui/input/QueryInputBase.d.ts +48 -0
- package/ui/input/QueryInputBase.js +48 -0
- package/ui/input/QueryInputBase.tsx +126 -0
- package/ui/input/QueryRadiosInput.d.ts +22 -0
- package/ui/input/QueryRadiosInput.js +23 -0
- package/ui/input/QueryRadiosInput.md +42 -0
- package/ui/input/{QueryInput.test.tsx → QueryRadiosInput.test.tsx} +12 -4
- package/ui/input/QueryRadiosInput.tsx +53 -0
- package/ui/input/TextInput.js +1 -1
- package/ui/input/TextInput.tsx +2 -1
- package/ui/input/index.d.ts +5 -1
- package/ui/input/index.js +5 -1
- package/ui/input/index.ts +5 -1
- package/ui/notice/Empty.d.ts +24 -0
- package/ui/notice/Empty.js +20 -0
- package/ui/notice/Empty.md +35 -0
- package/ui/notice/Empty.test.tsx +18 -0
- package/ui/notice/Empty.tsx +34 -0
- package/ui/notice/index.d.ts +1 -0
- package/ui/notice/index.js +1 -0
- package/ui/notice/index.ts +1 -0
- package/ui/util/props.test.tsx +1 -1
- package/ui/input/QueryInput.d.ts +0 -44
- package/ui/input/QueryInput.js +0 -53
- package/ui/input/QueryInput.md +0 -38
- package/ui/input/QueryInput.tsx +0 -126
package/package.json
CHANGED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { ReactElement, ReactNode } from "react";
|
|
2
|
+
import type { ImmutableArray } from "../../util/array.js";
|
|
3
|
+
import type { ValueInputProps } from "./Input.js";
|
|
4
|
+
/**
|
|
5
|
+
* Props for `ArrayCheckboxInputs`, which renders a checkbox for each item in an array of values.
|
|
6
|
+
*
|
|
7
|
+
* @see https://shelving.cc/ui/ArrayCheckboxInputsProps
|
|
8
|
+
*/
|
|
9
|
+
export interface ArrayCheckboxInputsProps<T> extends ValueInputProps<ImmutableArray<T>> {
|
|
10
|
+
/** Array of values to show in the list of checkboxes. */
|
|
11
|
+
items: ImmutableArray<T>;
|
|
12
|
+
/** Function that formats an obscure item into a `ReactNode` for display. */
|
|
13
|
+
formatter?: ((value: T) => ReactNode) | undefined;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Output a list of `<CheckboxInput>` elements for each item in an array, to pick any number of them.
|
|
17
|
+
* - The value is the array of checked items, in the order they were checked. Items are compared with `===`.
|
|
18
|
+
* - Checked items that are not in `items` stay in the value.
|
|
19
|
+
* - The items can be any type, and can be formatted for output through an optional `formatter()` function.
|
|
20
|
+
*
|
|
21
|
+
* @kind component
|
|
22
|
+
* @returns Element rendering one checkbox per item.
|
|
23
|
+
* @example <ArrayCheckboxInputs name="sizes" items={["s", "m", "l"]} value={sizes} onValue={setSizes} />
|
|
24
|
+
* @see https://shelving.cc/ui/ArrayCheckboxInputs
|
|
25
|
+
*/
|
|
26
|
+
export declare function ArrayCheckboxInputs<T>({ value, onValue, required, items, formatter, className, ...props }: ArrayCheckboxInputsProps<T>): ReactElement;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { formatValue } from "../../util/format.js";
|
|
3
|
+
import { Column } from "../block/Column.js";
|
|
4
|
+
import { CheckboxInput } from "./CheckboxInput.js";
|
|
5
|
+
/**
|
|
6
|
+
* Output a list of `<CheckboxInput>` elements for each item in an array, to pick any number of them.
|
|
7
|
+
* - The value is the array of checked items, in the order they were checked. Items are compared with `===`.
|
|
8
|
+
* - Checked items that are not in `items` stay in the value.
|
|
9
|
+
* - The items can be any type, and can be formatted for output through an optional `formatter()` function.
|
|
10
|
+
*
|
|
11
|
+
* @kind component
|
|
12
|
+
* @returns Element rendering one checkbox per item.
|
|
13
|
+
* @example <ArrayCheckboxInputs name="sizes" items={["s", "m", "l"]} value={sizes} onValue={setSizes} />
|
|
14
|
+
* @see https://shelving.cc/ui/ArrayCheckboxInputs
|
|
15
|
+
*/
|
|
16
|
+
export function ArrayCheckboxInputs({ value = [], onValue, required = false, items, formatter = formatValue, className, ...props }) {
|
|
17
|
+
return (_jsx(Column, { className: className, children: items.map((v, i) => {
|
|
18
|
+
const checked = value.includes(v);
|
|
19
|
+
return (_jsx(CheckboxInput //
|
|
20
|
+
// The checkbox is uncontrolled, so the key includes `checked` to remount it when the value changes.
|
|
21
|
+
, { value: checked, onValue: on => onValue(on ? [...value, v] : value.filter(x => x !== v)), required: required && !value.length, ...props, children: formatter(v) }, `${i.toString()}:${checked ? "on" : "off"}`));
|
|
22
|
+
}) }));
|
|
23
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import type { ReactElement, ReactNode } from "react";
|
|
2
|
+
import type { ImmutableArray } from "../../util/array.js";
|
|
3
|
+
import { formatValue } from "../../util/format.js";
|
|
4
|
+
import { Column } from "../block/Column.js";
|
|
5
|
+
import { CheckboxInput } from "./CheckboxInput.js";
|
|
6
|
+
import type { ValueInputProps } from "./Input.js";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Props for `ArrayCheckboxInputs`, which renders a checkbox for each item in an array of values.
|
|
10
|
+
*
|
|
11
|
+
* @see https://shelving.cc/ui/ArrayCheckboxInputsProps
|
|
12
|
+
*/
|
|
13
|
+
export interface ArrayCheckboxInputsProps<T> extends ValueInputProps<ImmutableArray<T>> {
|
|
14
|
+
/** Array of values to show in the list of checkboxes. */
|
|
15
|
+
items: ImmutableArray<T>;
|
|
16
|
+
/** Function that formats an obscure item into a `ReactNode` for display. */
|
|
17
|
+
formatter?: ((value: T) => ReactNode) | undefined;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Output a list of `<CheckboxInput>` elements for each item in an array, to pick any number of them.
|
|
22
|
+
* - The value is the array of checked items, in the order they were checked. Items are compared with `===`.
|
|
23
|
+
* - Checked items that are not in `items` stay in the value.
|
|
24
|
+
* - The items can be any type, and can be formatted for output through an optional `formatter()` function.
|
|
25
|
+
*
|
|
26
|
+
* @kind component
|
|
27
|
+
* @returns Element rendering one checkbox per item.
|
|
28
|
+
* @example <ArrayCheckboxInputs name="sizes" items={["s", "m", "l"]} value={sizes} onValue={setSizes} />
|
|
29
|
+
* @see https://shelving.cc/ui/ArrayCheckboxInputs
|
|
30
|
+
*/
|
|
31
|
+
export function ArrayCheckboxInputs<T>({
|
|
32
|
+
value = [],
|
|
33
|
+
onValue,
|
|
34
|
+
required = false,
|
|
35
|
+
items,
|
|
36
|
+
formatter = formatValue,
|
|
37
|
+
className,
|
|
38
|
+
...props
|
|
39
|
+
}: ArrayCheckboxInputsProps<T>): ReactElement {
|
|
40
|
+
return (
|
|
41
|
+
<Column className={className}>
|
|
42
|
+
{items.map((v, i) => {
|
|
43
|
+
const checked = value.includes(v);
|
|
44
|
+
return (
|
|
45
|
+
<CheckboxInput //
|
|
46
|
+
// The checkbox is uncontrolled, so the key includes `checked` to remount it when the value changes.
|
|
47
|
+
key={`${i.toString()}:${checked ? "on" : "off"}`}
|
|
48
|
+
value={checked}
|
|
49
|
+
onValue={on => onValue(on ? [...value, v] : value.filter(x => x !== v))}
|
|
50
|
+
required={required && !value.length}
|
|
51
|
+
{...props}
|
|
52
|
+
>
|
|
53
|
+
{formatter(v)}
|
|
54
|
+
</CheckboxInput>
|
|
55
|
+
);
|
|
56
|
+
})}
|
|
57
|
+
</Column>
|
|
58
|
+
);
|
|
59
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { type ReactElement } from "react";
|
|
2
|
+
import type { FileTypes } from "../../util/file.js";
|
|
3
|
+
import { type BlockVariants } from "../style/Block.js";
|
|
4
|
+
import type { ValueInputProps } from "./Input.js";
|
|
5
|
+
/**
|
|
6
|
+
* Props for `<ImageInput>`, a square image picker that emits the picked `File`.
|
|
7
|
+
*
|
|
8
|
+
* @see https://shelving.cc/ui/ImageInputProps
|
|
9
|
+
*/
|
|
10
|
+
export interface ImageInputProps extends ValueInputProps<File, string>, BlockVariants {
|
|
11
|
+
/**
|
|
12
|
+
* The image types the picker accepts.
|
|
13
|
+
* @default { jpg: "image/jpeg", png: "image/png", webp: "image/webp", gif: "image/gif" }
|
|
14
|
+
*/
|
|
15
|
+
types?: FileTypes | undefined;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Square image picker that shows the picked image in the square.
|
|
19
|
+
* - With no image, the square shows a "Choose…" button in the middle (change it with `placeholder`).
|
|
20
|
+
* - With an image, the image fills the square and an X button in the corner clears it (`onValue(undefined)`).
|
|
21
|
+
* - A tap anywhere on the square opens the file picker.
|
|
22
|
+
* - `value` is a picked `File`, or the URL of the current image.
|
|
23
|
+
*
|
|
24
|
+
* @kind component
|
|
25
|
+
* @see https://shelving.cc/ui/ImageInput
|
|
26
|
+
*/
|
|
27
|
+
export declare function ImageInput({ name, title, placeholder, required, disabled, message, value, onValue, types, className, ...variants }: ImageInputProps): ReactElement;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { XMarkIcon } from "@heroicons/react/24/solid";
|
|
3
|
+
import { useEffect, useMemo } from "react";
|
|
4
|
+
import { getButtonClass } from "../button/Button.js";
|
|
5
|
+
import { getBlockClass } from "../style/Block.js";
|
|
6
|
+
import { getClass, getModuleClass } from "../util/css.js";
|
|
7
|
+
import IMAGE_INPUT_CSS from "./ImageInput.module.css";
|
|
8
|
+
const IMAGE_INPUT_CLASS = getModuleClass(IMAGE_INPUT_CSS, "image-input");
|
|
9
|
+
const IMAGE_INPUT_PICKER_CLASS = getModuleClass(IMAGE_INPUT_CSS, "picker");
|
|
10
|
+
const IMAGE_INPUT_PREVIEW_CLASS = getModuleClass(IMAGE_INPUT_CSS, "preview");
|
|
11
|
+
const IMAGE_INPUT_FILE_CLASS = getModuleClass(IMAGE_INPUT_CSS, "file");
|
|
12
|
+
const IMAGE_INPUT_CHOOSE_CLASS = getClass(getButtonClass({ solid: true }), getModuleClass(IMAGE_INPUT_CSS, "choose"));
|
|
13
|
+
const IMAGE_INPUT_CLEAR_CLASS = getClass(getButtonClass({ solid: true, small: true }), getModuleClass(IMAGE_INPUT_CSS, "clear"));
|
|
14
|
+
/** The image types the picker accepts by default. */
|
|
15
|
+
const IMAGE_TYPES = { jpg: "image/jpeg", png: "image/png", webp: "image/webp", gif: "image/gif" };
|
|
16
|
+
/**
|
|
17
|
+
* Square image picker that shows the picked image in the square.
|
|
18
|
+
* - With no image, the square shows a "Choose…" button in the middle (change it with `placeholder`).
|
|
19
|
+
* - With an image, the image fills the square and an X button in the corner clears it (`onValue(undefined)`).
|
|
20
|
+
* - A tap anywhere on the square opens the file picker.
|
|
21
|
+
* - `value` is a picked `File`, or the URL of the current image.
|
|
22
|
+
*
|
|
23
|
+
* @kind component
|
|
24
|
+
* @see https://shelving.cc/ui/ImageInput
|
|
25
|
+
*/
|
|
26
|
+
export function ImageInput({ name, title = "Image", placeholder = "Choose…", required = false, disabled = false, message, value, onValue, types = IMAGE_TYPES, className, ...variants }) {
|
|
27
|
+
const onChange = (e) => {
|
|
28
|
+
const file = e.currentTarget.files?.[0];
|
|
29
|
+
if (file)
|
|
30
|
+
onValue(file);
|
|
31
|
+
};
|
|
32
|
+
const onClear = ({ currentTarget }) => {
|
|
33
|
+
// Empty the native input too, so picking the same file again still fires `change`.
|
|
34
|
+
const input = currentTarget.parentElement?.querySelector("input");
|
|
35
|
+
if (input)
|
|
36
|
+
input.value = "";
|
|
37
|
+
onValue(undefined);
|
|
38
|
+
};
|
|
39
|
+
return (_jsxs("div", { className: getClass(IMAGE_INPUT_CLASS, getBlockClass(variants), className), children: [_jsxs("label", { className: IMAGE_INPUT_PICKER_CLASS, title: message, "aria-invalid": !!message, children: [value ? _jsx(ImageInputPreview, { value: value }) : _jsx("span", { className: IMAGE_INPUT_CHOOSE_CLASS, children: placeholder }), _jsx("input", { name: name, type: "file", accept: Object.values(types).join(","), required: required && !value, disabled: disabled, "aria-label": title, className: IMAGE_INPUT_FILE_CLASS, onChange: onChange })] }), value && !disabled && (_jsx("button", { type: "button", title: "Clear", className: IMAGE_INPUT_CLEAR_CLASS, onClick: onClear, children: _jsx(XMarkIcon, {}) }))] }));
|
|
40
|
+
}
|
|
41
|
+
/** The image in the square: a URL as is, or a picked `File` through an object URL that is freed when it changes. */
|
|
42
|
+
function ImageInputPreview({ value }) {
|
|
43
|
+
const src = useMemo(() => (typeof value === "string" ? value : URL.createObjectURL(value)), [value]);
|
|
44
|
+
useEffect(() => () => {
|
|
45
|
+
if (typeof value !== "string")
|
|
46
|
+
URL.revokeObjectURL(src);
|
|
47
|
+
}, [value, src]);
|
|
48
|
+
return _jsx("img", { src: src, alt: "", className: IMAGE_INPUT_PREVIEW_CLASS });
|
|
49
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
# ImageInput
|
|
2
|
+
|
|
3
|
+
A large square image picker. With no image, it shows a "Choose…" button in the middle. With an image, the image fills the square and an X button in the corner clears it.
|
|
4
|
+
|
|
5
|
+
## Things to know
|
|
6
|
+
|
|
7
|
+
- A tap anywhere on the square opens the file picker. On a phone this is the built-in photo picker, which can also take a photo.
|
|
8
|
+
- `onValue` gets the picked `File`, or `undefined` when the X button clears the image.
|
|
9
|
+
- `value` is the image to show: a picked `File`, or the URL of the current image (for example, a saved profile photo).
|
|
10
|
+
- The square is as wide as its container. Put it in a narrow container to make it smaller.
|
|
11
|
+
- `placeholder` sets the button text (default "Choose…"). `title` labels the input for screen readers.
|
|
12
|
+
- `types` sets the image types the picker accepts (default JPEG, PNG, WebP, and GIF).
|
|
13
|
+
|
|
14
|
+
## Usage
|
|
15
|
+
|
|
16
|
+
```tsx
|
|
17
|
+
import { ImageInput } from "shelving/ui";
|
|
18
|
+
|
|
19
|
+
function PhotoPicker() {
|
|
20
|
+
const [photo, setPhoto] = useState<File>();
|
|
21
|
+
return <ImageInput name="photo" title="Photo" value={photo} onValue={setPhoto} />;
|
|
22
|
+
}
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
### Show the current image
|
|
26
|
+
|
|
27
|
+
```tsx
|
|
28
|
+
import { ImageInput } from "shelving/ui";
|
|
29
|
+
|
|
30
|
+
// Shows the saved photo. A new pick calls `onValue` with the new `File`.
|
|
31
|
+
<ImageInput name="photo" value={user.photoURL} onValue={upload} />
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Styling
|
|
35
|
+
|
|
36
|
+
`ImageInput` paints from the [tint ladder](/ui/TINT_CLASS). The "Choose…" and X buttons are `<Button>` styles, so `--button-*` hooks also move them.
|
|
37
|
+
|
|
38
|
+
| Variable | Styles | Default |
|
|
39
|
+
|---|---|---|
|
|
40
|
+
| `--image-input-space` | Space above and below the square | `var(--space-small)` (12px) |
|
|
41
|
+
| `--image-input-radius` | Corner radius of the square | `var(--radius-small)` (12px) |
|
|
42
|
+
| `--image-input-background` | Square background when empty | `var(--tint-90)` |
|
|
43
|
+
| `--image-input-preview-background` | Background behind the picked image, seen through a transparent image | `#000` |
|
|
44
|
+
| `--image-input-focus-border` | Outline when the input has focus | `var(--stroke-focus) solid var(--color-focus)` |
|
|
45
|
+
| `--image-input-invalid-border` | Outline when `message` is set | `var(--stroke-focus) solid var(--color-failure)` |
|
|
46
|
+
| `--image-input-clear-inset` | Distance of the X button from the top and end edges | `var(--space-small)` (12px) |
|
|
47
|
+
|
|
48
|
+
Global tokens it reads: `--tint-90`, `--space-small`, `--radius-small`, `--stroke-focus`, `--color-focus`, `--color-failure`.
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
@import url("../style/layers.css");
|
|
2
|
+
@import url("../style/Color.module.css");
|
|
3
|
+
@import url("../style/Radius.module.css");
|
|
4
|
+
@import url("../style/Space.module.css");
|
|
5
|
+
@import url("../style/Stroke.module.css");
|
|
6
|
+
@import url("../style/Tint.module.css");
|
|
7
|
+
|
|
8
|
+
@layer components {
|
|
9
|
+
.image-input {
|
|
10
|
+
/* Box: a square as wide as its container. */
|
|
11
|
+
position: relative;
|
|
12
|
+
box-sizing: border-box;
|
|
13
|
+
inline-size: 100%;
|
|
14
|
+
aspect-ratio: 1;
|
|
15
|
+
margin-block: var(--image-input-space, var(--space-small));
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/* The square itself: a tap anywhere opens the file picker. */
|
|
19
|
+
.picker {
|
|
20
|
+
position: absolute;
|
|
21
|
+
inset: 0;
|
|
22
|
+
display: flex;
|
|
23
|
+
align-items: center;
|
|
24
|
+
justify-content: center;
|
|
25
|
+
overflow: hidden;
|
|
26
|
+
border-radius: var(--image-input-radius, var(--radius-small));
|
|
27
|
+
background: var(--image-input-background, var(--tint-90));
|
|
28
|
+
cursor: pointer;
|
|
29
|
+
|
|
30
|
+
/* The native file input is not seen, so show its focus ring on the square. */
|
|
31
|
+
&:has(:focus-visible) {
|
|
32
|
+
outline: var(--image-input-focus-border, var(--stroke-focus) solid var(--color-focus));
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
&[aria-invalid="true"] {
|
|
36
|
+
outline: var(--image-input-invalid-border, var(--stroke-focus) solid var(--color-failure));
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
&:has(:disabled) {
|
|
40
|
+
cursor: default;
|
|
41
|
+
opacity: 0.5;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/* The picked image, covering the whole square. */
|
|
46
|
+
.preview {
|
|
47
|
+
position: absolute;
|
|
48
|
+
inset: 0;
|
|
49
|
+
inline-size: 100%;
|
|
50
|
+
block-size: 100%;
|
|
51
|
+
object-fit: cover;
|
|
52
|
+
|
|
53
|
+
/* A transparent image shows on black, not on the empty square's tint. */
|
|
54
|
+
background: var(--image-input-preview-background, #000);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/* The native file input: still focusable and opened by its label, but not seen. */
|
|
58
|
+
.file {
|
|
59
|
+
position: absolute;
|
|
60
|
+
inline-size: 1px;
|
|
61
|
+
block-size: 1px;
|
|
62
|
+
overflow: hidden;
|
|
63
|
+
clip-path: inset(50%);
|
|
64
|
+
white-space: nowrap;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/* The "Choose…" button only shows the label's action, so taps go through to the label. */
|
|
68
|
+
.choose {
|
|
69
|
+
pointer-events: none;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/* The X button that clears the image, in the top corner. */
|
|
73
|
+
.clear {
|
|
74
|
+
position: absolute;
|
|
75
|
+
inset-block-start: var(--image-input-clear-inset, var(--space-small));
|
|
76
|
+
inset-inline-end: var(--image-input-clear-inset, var(--space-small));
|
|
77
|
+
margin: 0;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { XMarkIcon } from "@heroicons/react/24/solid";
|
|
2
|
+
import { type MouseEvent, type ReactElement, type SyntheticEvent, useEffect, useMemo } from "react";
|
|
3
|
+
import type { FileTypes } from "../../util/file.js";
|
|
4
|
+
import { getButtonClass } from "../button/Button.js";
|
|
5
|
+
import { type BlockVariants, getBlockClass } from "../style/Block.js";
|
|
6
|
+
import { getClass, getModuleClass } from "../util/css.js";
|
|
7
|
+
import IMAGE_INPUT_CSS from "./ImageInput.module.css";
|
|
8
|
+
import type { ValueInputProps } from "./Input.js";
|
|
9
|
+
|
|
10
|
+
const IMAGE_INPUT_CLASS = getModuleClass(IMAGE_INPUT_CSS, "image-input");
|
|
11
|
+
const IMAGE_INPUT_PICKER_CLASS = getModuleClass(IMAGE_INPUT_CSS, "picker");
|
|
12
|
+
const IMAGE_INPUT_PREVIEW_CLASS = getModuleClass(IMAGE_INPUT_CSS, "preview");
|
|
13
|
+
const IMAGE_INPUT_FILE_CLASS = getModuleClass(IMAGE_INPUT_CSS, "file");
|
|
14
|
+
const IMAGE_INPUT_CHOOSE_CLASS = getClass(getButtonClass({ solid: true }), getModuleClass(IMAGE_INPUT_CSS, "choose"));
|
|
15
|
+
const IMAGE_INPUT_CLEAR_CLASS = getClass(getButtonClass({ solid: true, small: true }), getModuleClass(IMAGE_INPUT_CSS, "clear"));
|
|
16
|
+
|
|
17
|
+
/** The image types the picker accepts by default. */
|
|
18
|
+
const IMAGE_TYPES: FileTypes = { jpg: "image/jpeg", png: "image/png", webp: "image/webp", gif: "image/gif" };
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Props for `<ImageInput>`, a square image picker that emits the picked `File`.
|
|
22
|
+
*
|
|
23
|
+
* @see https://shelving.cc/ui/ImageInputProps
|
|
24
|
+
*/
|
|
25
|
+
export interface ImageInputProps extends ValueInputProps<File, string>, BlockVariants {
|
|
26
|
+
/**
|
|
27
|
+
* The image types the picker accepts.
|
|
28
|
+
* @default { jpg: "image/jpeg", png: "image/png", webp: "image/webp", gif: "image/gif" }
|
|
29
|
+
*/
|
|
30
|
+
types?: FileTypes | undefined;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Square image picker that shows the picked image in the square.
|
|
35
|
+
* - With no image, the square shows a "Choose…" button in the middle (change it with `placeholder`).
|
|
36
|
+
* - With an image, the image fills the square and an X button in the corner clears it (`onValue(undefined)`).
|
|
37
|
+
* - A tap anywhere on the square opens the file picker.
|
|
38
|
+
* - `value` is a picked `File`, or the URL of the current image.
|
|
39
|
+
*
|
|
40
|
+
* @kind component
|
|
41
|
+
* @see https://shelving.cc/ui/ImageInput
|
|
42
|
+
*/
|
|
43
|
+
export function ImageInput({
|
|
44
|
+
name,
|
|
45
|
+
title = "Image",
|
|
46
|
+
placeholder = "Choose…",
|
|
47
|
+
required = false,
|
|
48
|
+
disabled = false,
|
|
49
|
+
message,
|
|
50
|
+
value,
|
|
51
|
+
onValue,
|
|
52
|
+
types = IMAGE_TYPES,
|
|
53
|
+
className,
|
|
54
|
+
...variants
|
|
55
|
+
}: ImageInputProps): ReactElement {
|
|
56
|
+
const onChange = (e: SyntheticEvent<HTMLInputElement>) => {
|
|
57
|
+
const file = e.currentTarget.files?.[0];
|
|
58
|
+
if (file) onValue(file);
|
|
59
|
+
};
|
|
60
|
+
const onClear = ({ currentTarget }: MouseEvent<HTMLButtonElement>) => {
|
|
61
|
+
// Empty the native input too, so picking the same file again still fires `change`.
|
|
62
|
+
const input = currentTarget.parentElement?.querySelector("input");
|
|
63
|
+
if (input) input.value = "";
|
|
64
|
+
onValue(undefined);
|
|
65
|
+
};
|
|
66
|
+
return (
|
|
67
|
+
<div className={getClass(IMAGE_INPUT_CLASS, getBlockClass(variants), className)}>
|
|
68
|
+
<label className={IMAGE_INPUT_PICKER_CLASS} title={message} aria-invalid={!!message}>
|
|
69
|
+
{value ? <ImageInputPreview value={value} /> : <span className={IMAGE_INPUT_CHOOSE_CLASS}>{placeholder}</span>}
|
|
70
|
+
<input
|
|
71
|
+
name={name}
|
|
72
|
+
type="file"
|
|
73
|
+
accept={Object.values(types).join(",")}
|
|
74
|
+
required={required && !value}
|
|
75
|
+
disabled={disabled}
|
|
76
|
+
aria-label={title}
|
|
77
|
+
className={IMAGE_INPUT_FILE_CLASS}
|
|
78
|
+
onChange={onChange}
|
|
79
|
+
/>
|
|
80
|
+
</label>
|
|
81
|
+
{value && !disabled && (
|
|
82
|
+
<button type="button" title="Clear" className={IMAGE_INPUT_CLEAR_CLASS} onClick={onClear}>
|
|
83
|
+
<XMarkIcon />
|
|
84
|
+
</button>
|
|
85
|
+
)}
|
|
86
|
+
</div>
|
|
87
|
+
);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** The image in the square: a URL as is, or a picked `File` through an object URL that is freed when it changes. */
|
|
91
|
+
function ImageInputPreview({ value }: { readonly value: File | string }): ReactElement {
|
|
92
|
+
const src = useMemo(() => (typeof value === "string" ? value : URL.createObjectURL(value)), [value]);
|
|
93
|
+
useEffect(
|
|
94
|
+
() => () => {
|
|
95
|
+
if (typeof value !== "string") URL.revokeObjectURL(src);
|
|
96
|
+
},
|
|
97
|
+
[value, src],
|
|
98
|
+
);
|
|
99
|
+
return <img src={src} alt="" className={IMAGE_INPUT_PREVIEW_CLASS} />;
|
|
100
|
+
}
|
|
@@ -101,7 +101,16 @@
|
|
|
101
101
|
white-space: pre-wrap;
|
|
102
102
|
overflow-wrap: break-word;
|
|
103
103
|
field-sizing: content;
|
|
104
|
-
|
|
104
|
+
|
|
105
|
+
/* `field-sizing` ignores `rows`, so set the minimum height from `--input-rows` (set inline from the `rows` prop). */
|
|
106
|
+
min-block-size: calc(
|
|
107
|
+
var(--input-rows, 2) *
|
|
108
|
+
1lh +
|
|
109
|
+
2 *
|
|
110
|
+
var(--input-padding, var(--space-small)) +
|
|
111
|
+
2 *
|
|
112
|
+
var(--input-stroke, var(--stroke-normal))
|
|
113
|
+
);
|
|
105
114
|
}
|
|
106
115
|
}
|
|
107
116
|
|
package/ui/input/Input.test.tsx
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { describe, expect, test } from "bun:test";
|
|
2
2
|
import type { ReactElement } from "react";
|
|
3
|
-
import { DateInput, FileInput, NumberInput, TextInput } from "shelving/ui";
|
|
3
|
+
import { DateInput, FileInput, ImageInput, NumberInput, TextInput } from "shelving/ui";
|
|
4
4
|
import { PASSTHROUGH } from "shelving/util/function";
|
|
5
5
|
|
|
6
6
|
/** The props of the rendered element that the tests read. */
|
|
@@ -36,3 +36,35 @@ describe("FileInput", () => {
|
|
|
36
36
|
expect(values).toEqual([file]);
|
|
37
37
|
});
|
|
38
38
|
});
|
|
39
|
+
|
|
40
|
+
describe("ImageInput", () => {
|
|
41
|
+
type _Element = ReactElement<{ children: ReactElement<{ children: ReactElement[] }>[] }>;
|
|
42
|
+
function _parts(element: ReactElement) {
|
|
43
|
+
const [label, clear] = (element as _Element).props.children;
|
|
44
|
+
const [shown, input] = (label as ReactElement<{ children: ReactElement[] }>).props.children;
|
|
45
|
+
return {
|
|
46
|
+
shown: shown as ReactElement<Record<string, unknown>>,
|
|
47
|
+
input: input as ReactElement<Record<string, unknown>>,
|
|
48
|
+
clear: clear as unknown as ReactElement<Record<string, unknown>> | false | undefined,
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
test("shows the Choose… button and no clear button when empty", () => {
|
|
52
|
+
const { shown, clear } = _parts(ImageInput({ name: "x", onValue: PASSTHROUGH }));
|
|
53
|
+
expect(shown.type).toBe("span");
|
|
54
|
+
expect(shown.props.children).toBe("Choose…");
|
|
55
|
+
expect(clear).toBeFalsy();
|
|
56
|
+
});
|
|
57
|
+
test("shows the image and a clear button for a URL value", () => {
|
|
58
|
+
const { shown, clear } = _parts(ImageInput({ name: "x", value: "https://x.com/a.jpg", onValue: PASSTHROUGH }));
|
|
59
|
+
expect(shown.props.value).toBe("https://x.com/a.jpg");
|
|
60
|
+
expect(clear).toBeTruthy();
|
|
61
|
+
});
|
|
62
|
+
test("calls onValue with the picked file, and with undefined on clear", () => {
|
|
63
|
+
const values: (File | undefined)[] = [];
|
|
64
|
+
const file = new File(["abc"], "a.jpg");
|
|
65
|
+
const { input, clear } = _parts(ImageInput({ name: "x", value: "https://x.com/a.jpg", onValue: v => void values.push(v) }));
|
|
66
|
+
(input.props.onChange as (e: { currentTarget: { files: File[] } }) => void)({ currentTarget: { files: [file] } });
|
|
67
|
+
((clear as ReactElement<Record<string, unknown>>).props.onClick as (e: unknown) => void)({ currentTarget: { parentElement: null } });
|
|
68
|
+
expect(values).toEqual([file, undefined]);
|
|
69
|
+
});
|
|
70
|
+
});
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { ReactElement } from "react";
|
|
2
|
+
import type { ImmutableArray } from "../../util/array.js";
|
|
3
|
+
import type { ValueInputProps } from "./Input.js";
|
|
4
|
+
import { type QueryInputBaseProps } from "./QueryInputBase.js";
|
|
5
|
+
/**
|
|
6
|
+
* Props for `QueryCheckboxesInput`, a combo box that queries items of type `O` from an input of type `I`, and picks any number of them.
|
|
7
|
+
*
|
|
8
|
+
* @see https://shelving.cc/ui/QueryCheckboxesInputProps
|
|
9
|
+
*/
|
|
10
|
+
export interface QueryCheckboxesInputProps<I, O> extends QueryInputBaseProps<I, O>, ValueInputProps<ImmutableArray<O>> {
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Combo box that queries a list of items and lets the user pick any number of them, with checkboxes.
|
|
14
|
+
* - Shows an input based on a `schema` of type `I`; its values are piped to the `onQuery()` callback.
|
|
15
|
+
* - Shows a `<Popover>` with a checkbox list of returned items. The value is the array of checked items. The popover stays open while the user checks items.
|
|
16
|
+
* - With `popover={false}`, shows the checkbox list inline below the input instead.
|
|
17
|
+
* - Checked items stay checked when a new query does not return them. Items are compared with `===`, so `onQuery()` must return the same objects each time (or primitives).
|
|
18
|
+
* - Errors, loading, and empty states are handled automatically.
|
|
19
|
+
*
|
|
20
|
+
* @kind component
|
|
21
|
+
* @returns A combo box element wrapping the input and results popover (or the inline list).
|
|
22
|
+
* @see https://shelving.cc/ui/QueryCheckboxesInput
|
|
23
|
+
*/
|
|
24
|
+
export declare function QueryCheckboxesInput<I, O>({ value, onValue, formatter, ...props }: QueryCheckboxesInputProps<I, O>): ReactElement;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { formatValue } from "../../util/format.js";
|
|
3
|
+
import { ArrayCheckboxInputs } from "./ArrayCheckboxInputs.js";
|
|
4
|
+
import { QueryInputBase } from "./QueryInputBase.js";
|
|
5
|
+
/**
|
|
6
|
+
* Combo box that queries a list of items and lets the user pick any number of them, with checkboxes.
|
|
7
|
+
* - Shows an input based on a `schema` of type `I`; its values are piped to the `onQuery()` callback.
|
|
8
|
+
* - Shows a `<Popover>` with a checkbox list of returned items. The value is the array of checked items. The popover stays open while the user checks items.
|
|
9
|
+
* - With `popover={false}`, shows the checkbox list inline below the input instead.
|
|
10
|
+
* - Checked items stay checked when a new query does not return them. Items are compared with `===`, so `onQuery()` must return the same objects each time (or primitives).
|
|
11
|
+
* - Errors, loading, and empty states are handled automatically.
|
|
12
|
+
*
|
|
13
|
+
* @kind component
|
|
14
|
+
* @returns A combo box element wrapping the input and results popover (or the inline list).
|
|
15
|
+
* @see https://shelving.cc/ui/QueryCheckboxesInput
|
|
16
|
+
*/
|
|
17
|
+
export function QueryCheckboxesInput({ value, onValue, formatter = formatValue, ...props }) {
|
|
18
|
+
const { name, required, disabled } = props;
|
|
19
|
+
return (_jsx(QueryInputBase, { ...props, list: items => (_jsx(ArrayCheckboxInputs, { name: name, required: required, disabled: disabled, items: items, value: value, formatter: formatter, onValue: onValue })) }));
|
|
20
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# QueryCheckboxesInput
|
|
2
|
+
|
|
3
|
+
Combo box that queries a list of items and lets the user pick any number of them. The text in the input goes to `onQuery()`, and the items it returns show as a checkbox list. The value is the array of checked items.
|
|
4
|
+
|
|
5
|
+
**Things to know:**
|
|
6
|
+
|
|
7
|
+
- It works like `<QueryRadiosInput>`: the same `schema`, `onQuery()`, `formatter`, `popover` and `empty` props, and the same loading, empty and error states.
|
|
8
|
+
- The popover stays open while the user checks items.
|
|
9
|
+
- Checked items stay in the value when a new search does not return them. Items are compared with `===`, so `onQuery()` must return the same objects each time (or primitives).
|
|
10
|
+
- Show your own button to act on the checked items, for example "Invite".
|
|
11
|
+
|
|
12
|
+
## Usage
|
|
13
|
+
|
|
14
|
+
### Inline list with a button
|
|
15
|
+
|
|
16
|
+
```tsx
|
|
17
|
+
import { Button, QueryCheckboxesInput } from "shelving/ui";
|
|
18
|
+
import { StringSchema } from "shelving/schema";
|
|
19
|
+
|
|
20
|
+
const SEARCH = new StringSchema({ title: "Search", one: "user" });
|
|
21
|
+
|
|
22
|
+
const [users, setUsers] = useState<readonly User[]>([]);
|
|
23
|
+
|
|
24
|
+
<QueryCheckboxesInput
|
|
25
|
+
popover={false}
|
|
26
|
+
name="users"
|
|
27
|
+
schema={SEARCH}
|
|
28
|
+
onQuery={search}
|
|
29
|
+
formatter={user => <Avatar user={user} />}
|
|
30
|
+
value={users}
|
|
31
|
+
onValue={v => setUsers(v ?? [])}
|
|
32
|
+
/>
|
|
33
|
+
<Button onClick={() => invite(users)}>Invite</Button>
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## Styling
|
|
37
|
+
|
|
38
|
+
`<QueryCheckboxesInput>` has no own hooks. The popover version uses the `<Popover>` styles. The inline version is a `<Column>` with the input and the `<ArrayCheckboxInputs>` list, so it uses their styles.
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
import { renderToStaticMarkup } from "react-dom/server";
|
|
3
|
+
import { STRING } from "shelving/schema";
|
|
4
|
+
import { ArrayCheckboxInputs, QueryCheckboxesInput } from "shelving/ui";
|
|
5
|
+
import { PASSTHROUGH } from "shelving/util/function";
|
|
6
|
+
|
|
7
|
+
describe("QueryCheckboxesInput", () => {
|
|
8
|
+
test("shows the input with no popover when `popover={false}`", () => {
|
|
9
|
+
const html = renderToStaticMarkup(
|
|
10
|
+
<QueryCheckboxesInput popover={false} name="q" schema={STRING} onQuery={() => []} onValue={PASSTHROUGH} />,
|
|
11
|
+
);
|
|
12
|
+
expect(html).toContain('name="q"');
|
|
13
|
+
});
|
|
14
|
+
test("value is an array of items", () => {
|
|
15
|
+
const element = (
|
|
16
|
+
<QueryCheckboxesInput<string, string>
|
|
17
|
+
name="q"
|
|
18
|
+
schema={STRING}
|
|
19
|
+
onQuery={() => []}
|
|
20
|
+
value={["a"]}
|
|
21
|
+
onValue={(v: readonly string[] | undefined) => v}
|
|
22
|
+
/>
|
|
23
|
+
);
|
|
24
|
+
expect(element).toBeTruthy();
|
|
25
|
+
});
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
describe("ArrayCheckboxInputs", () => {
|
|
29
|
+
test("renders a checked checkbox for each item in the value", () => {
|
|
30
|
+
const html = renderToStaticMarkup(<ArrayCheckboxInputs name="x" items={["a", "b", "c"]} value={["b"]} onValue={PASSTHROUGH} />);
|
|
31
|
+
expect(html.match(/type="checkbox"/g)?.length).toBe(3);
|
|
32
|
+
expect(html.match(/checked=""/g)?.length).toBe(1);
|
|
33
|
+
});
|
|
34
|
+
test("adds and removes items", () => {
|
|
35
|
+
const values: (readonly string[] | undefined)[] = [];
|
|
36
|
+
const element = ArrayCheckboxInputs({ name: "x", items: ["a", "b"], value: ["a"], onValue: v => values.push(v) });
|
|
37
|
+
const [a, b] = (element.props as { children: { props: { onValue(on: boolean): void } }[] }).children;
|
|
38
|
+
b?.props.onValue(true);
|
|
39
|
+
a?.props.onValue(false);
|
|
40
|
+
expect(values).toEqual([["a", "b"], []]);
|
|
41
|
+
});
|
|
42
|
+
});
|