@coopdigital/react 0.18.0 → 0.18.1

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.
Files changed (52) hide show
  1. package/dist/components/Expandable/Expandable.d.ts +1 -1
  2. package/dist/components/Expandable/Expandable.js +1 -1
  3. package/dist/components/Icon/SearchIcon.js +12 -0
  4. package/dist/components/SearchBox/SearchBox.d.ts +48 -0
  5. package/dist/components/SearchBox/SearchBox.js +67 -0
  6. package/dist/components/SearchBox/index.d.ts +4 -0
  7. package/dist/index.d.ts +1 -0
  8. package/dist/index.js +1 -0
  9. package/package.json +9 -9
  10. package/src/components/Expandable/Expandable.tsx +13 -2
  11. package/src/components/Icon/AddIcon.tsx +1 -0
  12. package/src/components/Icon/ArrowDownIcon.tsx +1 -0
  13. package/src/components/Icon/ArrowLeftIcon.tsx +1 -0
  14. package/src/components/Icon/ArrowRightIcon.tsx +1 -0
  15. package/src/components/Icon/ArrowUpIcon.tsx +1 -0
  16. package/src/components/Icon/AvatarAltIcon.tsx +1 -0
  17. package/src/components/Icon/AvatarIcon.tsx +1 -0
  18. package/src/components/Icon/BasketIcon.tsx +1 -0
  19. package/src/components/Icon/CalendarIcon.tsx +1 -0
  20. package/src/components/Icon/ChevronDownIcon.tsx +1 -0
  21. package/src/components/Icon/ChevronLeftIcon.tsx +1 -0
  22. package/src/components/Icon/ChevronRightIcon.tsx +1 -0
  23. package/src/components/Icon/ChevronUpIcon.tsx +1 -0
  24. package/src/components/Icon/ClockIcon.tsx +1 -0
  25. package/src/components/Icon/CloseAltIcon.tsx +1 -0
  26. package/src/components/Icon/CloseIcon.tsx +1 -0
  27. package/src/components/Icon/CoopCardIcon.tsx +1 -0
  28. package/src/components/Icon/CoopIcon.tsx +1 -0
  29. package/src/components/Icon/CoopLocationIcon.tsx +1 -0
  30. package/src/components/Icon/DownloadIcon.tsx +1 -0
  31. package/src/components/Icon/HomeIcon.tsx +1 -0
  32. package/src/components/Icon/InformationIcon.tsx +1 -0
  33. package/src/components/Icon/LocationIcon.tsx +1 -0
  34. package/src/components/Icon/MailIcon.tsx +1 -0
  35. package/src/components/Icon/MenuIcon.tsx +1 -0
  36. package/src/components/Icon/MessageIcon.tsx +1 -0
  37. package/src/components/Icon/MinusIcon.tsx +1 -0
  38. package/src/components/Icon/OpenNewIcon.tsx +1 -0
  39. package/src/components/Icon/PencilIcon.tsx +1 -0
  40. package/src/components/Icon/PhoneIcon.tsx +1 -0
  41. package/src/components/Icon/QuestionIcon.tsx +1 -0
  42. package/src/components/Icon/ScooterIcon.tsx +1 -0
  43. package/src/components/Icon/SearchIcon.tsx +1 -0
  44. package/src/components/Icon/SettingsIcon.tsx +1 -0
  45. package/src/components/Icon/TickAltIcon.tsx +1 -0
  46. package/src/components/Icon/TickIcon.tsx +1 -0
  47. package/src/components/Icon/VanIcon.tsx +1 -0
  48. package/src/components/Icon/WarningIcon.tsx +1 -0
  49. package/src/components/Icon/WriteIcon.tsx +1 -0
  50. package/src/components/SearchBox/SearchBox.tsx +131 -0
  51. package/src/components/SearchBox/index.ts +5 -0
  52. package/src/index.ts +1 -0
@@ -1,7 +1,7 @@
1
1
  import type { DetailsHTMLAttributes, JSX } from "react";
2
2
  export interface ExpandableProps extends DetailsHTMLAttributes<HTMLDetailsElement> {
3
3
  /** **(Optional)** Specifies the Expandable background color from the available options. */
4
- background?: "grey" | "purple" | "pink" | "green" | "orange" | "red" | "yellow" | "lilac" | "blue";
4
+ background?: "grey" | "purple" | "pink" | "green" | "orange" | "red" | "yellow" | "lilac" | "blue" | "white" | "transparent";
5
5
  /** Represents the content inside the Expandable component, only visible when expanded. It can be any valid JSX or string. */
6
6
  children: React.ReactNode;
7
7
  /** **(Optional)** Receives any className to be applied to Expandable component. */
@@ -6,7 +6,7 @@ import { jsxs, jsx } from 'react/jsx-runtime';
6
6
  * It can be used to reveal more context for a specific issue or action.
7
7
  */
8
8
  const Expandable = (_a) => {
9
- var { background, children, className = "", summary } = _a, props = __rest(_a, ["background", "children", "className", "summary"]);
9
+ var { background = "grey", children, className = "", summary } = _a, props = __rest(_a, ["background", "children", "className", "summary"]);
10
10
  const componentProps = Object.assign({ className: `coop-expandable ${className}`, "data-bg": background }, props);
11
11
  return (jsxs("details", Object.assign({}, componentProps, { children: [jsxs("summary", { children: [summary, jsx("svg", { fill: "none", height: "24", width: "24", xmlns: "http://www.w3.org/2000/svg", children: jsx("path", { d: "m20.25 9.75-6.22 6.22a.75.75 0 0 1-1.06 0L6.75 9.75", stroke: "black", strokeLinecap: "round", strokeLinejoin: "round", strokeWidth: "1.5" }) })] }), jsx("div", { className: "coop-expandable--content", children: children })] })));
12
12
  };
@@ -0,0 +1,12 @@
1
+ import { __rest } from '../../node_modules/tslib/tslib.es6.js';
2
+ import { jsxs, jsx } from 'react/jsx-runtime';
3
+ import { useId } from 'react';
4
+
5
+ const SearchIcon = (_a) => {
6
+ var { alt, className } = _a, props = __rest(_a, ["alt", "className"]);
7
+ const id = useId();
8
+ const componentProps = Object.assign({ "aria-labelledby": alt ? id : undefined, className: `coop-icon ${className !== null && className !== void 0 ? className : ""}`.trim(), "data-icon": "search", fill: "none", role: alt ? "img" : undefined, viewBox: "0 0 32 32" }, props);
9
+ return (jsxs("svg", Object.assign({}, componentProps, { children: [alt ? jsx("title", { id: id, children: alt }) : null, jsx("path", { d: "M13.25 25.49a12.25 12.25 0 1 1 12.24-12.24 12.25 12.25 0 0 1-12.24 12.24m0-22.61a10.37 10.37 0 1 0 10.36 10.37A10.38 10.38 0 0 0 13.25 2.88", fill: "currentColor" }), jsx("path", { d: "M30.06 31a.94.94 0 0 1-.67-.28l-8.81-8.81a.94.94 0 1 1 1.33-1.33l8.81 8.81a.94.94 0 0 1 0 1.33.9.9 0 0 1-.66.28", fill: "currentColor" })] })));
10
+ };
11
+
12
+ export { SearchIcon };
@@ -0,0 +1,48 @@
1
+ import React, { HTMLAttributes } from "react";
2
+ import { type JSX } from "react";
3
+ import { type ButtonProps } from "../Button";
4
+ export interface SearchBoxProps extends HTMLAttributes<HTMLInputElement> {
5
+ /** **(Optional)** Server endpoint to submit the form. Will be ignored if onSubmit is also set. */
6
+ action?: string;
7
+ /** **(Optional)** Whether or not to enable auto complete on the input field. Default: `off`. */
8
+ autoComplete?: "off" | "on";
9
+ /** **(Optional)** Props to forward to the Button element. Use `label` to set Button text. */
10
+ button?: Pick<ButtonProps, "className" | "loadingText"> & {
11
+ label?: React.ReactNode;
12
+ };
13
+ /** **(Optional)** Receives any className to be applied to SearchBox component. */
14
+ className?: string;
15
+ /** Receives the label to be applied to SearchBox component. */
16
+ label: string;
17
+ /** **(Optional)** Receives whether label is visible to human or only screen readers. */
18
+ labelVisible?: boolean;
19
+ /** **(Optional)** Name of the input element, also used as the URL search parameter. Defaults to `query` */
20
+ name?: string;
21
+ /** **(Optional)** Callback to run when the form is submitted. If this is an async function, it will be awaited and the SearchBox will be in a pending state until the promise is resolved. */
22
+ onSubmit?: React.FormEventHandler<HTMLElement> | undefined;
23
+ /** **(Optional)** Receives any placeholder for SearchBox component. */
24
+ placeholder?: string;
25
+ /** **(Optional)** Receives any size to be applied to SearchBox component. */
26
+ size?: string;
27
+ /** **(Optional)** Receives the variant to be applied to SearchBox component. */
28
+ variant?: "primary" | "secondary" | "ghost";
29
+ }
30
+ /**
31
+ * SearchBox component allows a person to enter a word or a phrase to find relevant content.
32
+ *
33
+ * The `label` prop is mandatory but not visible by default (it is visible for screen readers). You can make it visible to humans by setting `labelVisible` to true.
34
+ *
35
+ * If you are using a single icon with no text for `button.label`, ensure you pass an `alt` property to the icon for accessibility purposes.
36
+ *
37
+ * You can provide an `action` to submit input like a normal HTML form, or an `onSubmit` callback to handle submissions client-side.
38
+ *
39
+ * **Good to know:** SearchBox requires both `SearchBox` and `Button` css/scss files.
40
+ *
41
+ * ```
42
+ * import "@coopdigital/styles/components/Searchbox.css"
43
+ * import "@coopdigital/styles/components/Button.css"
44
+ *
45
+ * ```
46
+ */
47
+ export declare const SearchBox: ({ action, "aria-placeholder": ariaPlaceholder, autoCapitalize, autoComplete, button, className, label, labelVisible, name, onSubmit, placeholder, size, variant, ...props }: SearchBoxProps) => JSX.Element;
48
+ export default SearchBox;
@@ -0,0 +1,67 @@
1
+ import { __rest, __awaiter } from '../../node_modules/tslib/tslib.es6.js';
2
+ import { jsxs, jsx } from 'react/jsx-runtime';
3
+ import React, { useState, useCallback, useId } from 'react';
4
+ import { Button } from '../Button/Button.js';
5
+ import { SearchIcon } from '../Icon/SearchIcon.js';
6
+
7
+ const defaultButtonProps = {
8
+ label: React.createElement(SearchIcon, { alt: "Search", stroke: "currentColor", strokeWidth: 2 }),
9
+ loadingText: "",
10
+ };
11
+ /**
12
+ * SearchBox component allows a person to enter a word or a phrase to find relevant content.
13
+ *
14
+ * The `label` prop is mandatory but not visible by default (it is visible for screen readers). You can make it visible to humans by setting `labelVisible` to true.
15
+ *
16
+ * If you are using a single icon with no text for `button.label`, ensure you pass an `alt` property to the icon for accessibility purposes.
17
+ *
18
+ * You can provide an `action` to submit input like a normal HTML form, or an `onSubmit` callback to handle submissions client-side.
19
+ *
20
+ * **Good to know:** SearchBox requires both `SearchBox` and `Button` css/scss files.
21
+ *
22
+ * ```
23
+ * import "@coopdigital/styles/components/Searchbox.css"
24
+ * import "@coopdigital/styles/components/Button.css"
25
+ *
26
+ * ```
27
+ */
28
+ const SearchBox = (_a) => {
29
+ var _b, _c;
30
+ var { action, "aria-placeholder": ariaPlaceholder, autoCapitalize = "off", autoComplete = "off", button = defaultButtonProps, className, label, labelVisible = false, name = "query", onSubmit, placeholder, size = "md", variant = "primary" } = _a, props = __rest(_a, ["action", "aria-placeholder", "autoCapitalize", "autoComplete", "button", "className", "label", "labelVisible", "name", "onSubmit", "placeholder", "size", "variant"]);
31
+ const [isPending, setIsPending] = useState(false);
32
+ const handleSubmit = useCallback((event) => __awaiter(void 0, void 0, void 0, function* () {
33
+ event.preventDefault();
34
+ if (isPending || !onSubmit)
35
+ return;
36
+ setIsPending(true);
37
+ try {
38
+ yield Promise.resolve(onSubmit(event));
39
+ }
40
+ finally {
41
+ setIsPending(false);
42
+ }
43
+ }), [onSubmit, isPending]);
44
+ const id = useId();
45
+ const formProps = {
46
+ action: action !== null && action !== void 0 ? action : undefined,
47
+ className: `coop-search-box ${className !== null && className !== void 0 ? className : ""}`.trim(),
48
+ "data-size": size.length && size !== "md" ? size : undefined,
49
+ "data-variant": variant.length && variant !== "primary" ? variant : undefined,
50
+ onSubmit: onSubmit ? handleSubmit : undefined,
51
+ };
52
+ const buttonProps = {
53
+ className: button === null || button === void 0 ? void 0 : button.className,
54
+ isLoading: isPending,
55
+ loadingText: (_b = button === null || button === void 0 ? void 0 : button.loadingText) !== null && _b !== void 0 ? _b : "",
56
+ size: size,
57
+ variant: variant,
58
+ };
59
+ const inputProps = Object.assign({ "aria-placeholder": (_c = placeholder !== null && placeholder !== void 0 ? placeholder : ariaPlaceholder) !== null && _c !== void 0 ? _c : undefined, autoCapitalize,
60
+ autoComplete,
61
+ id,
62
+ name,
63
+ placeholder, type: "search" }, props);
64
+ return (jsxs("form", Object.assign({}, formProps, { children: [jsx("label", { className: labelVisible ? "" : "sr-only", htmlFor: id, children: label }), jsxs("div", { className: "coop-search-box--inner", children: [jsx("input", Object.assign({}, inputProps)), jsx(Button, Object.assign({}, buttonProps, { children: button.label }))] })] })));
65
+ };
66
+
67
+ export { SearchBox, SearchBox as default };
@@ -0,0 +1,4 @@
1
+ import SearchBox from "./SearchBox";
2
+ export default SearchBox;
3
+ export { SearchBox };
4
+ export * from "./SearchBox";
package/dist/index.d.ts CHANGED
@@ -6,6 +6,7 @@ export * from "./components/Expandable";
6
6
  export * from "./components/Image";
7
7
  export * from "./components/Pill";
8
8
  export * from "./components/RootSVG";
9
+ export * from "./components/SearchBox";
9
10
  export * from "./components/Signpost";
10
11
  export * from "./components/SkipNav";
11
12
  export * from "./components/Tag";
package/dist/index.js CHANGED
@@ -6,6 +6,7 @@ export { Expandable } from './components/Expandable/Expandable.js';
6
6
  export { Image } from './components/Image/Image.js';
7
7
  export { Pill } from './components/Pill/Pill.js';
8
8
  export { RootSVG } from './components/RootSVG/RootSVG.js';
9
+ export { SearchBox } from './components/SearchBox/SearchBox.js';
9
10
  export { Signpost } from './components/Signpost/Signpost.js';
10
11
  export { SkipNav } from './components/SkipNav/SkipNav.js';
11
12
  export { Tag } from './components/Tag/Tag.js';
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@coopdigital/react",
3
3
  "type": "module",
4
- "version": "0.18.0",
4
+ "version": "0.18.1",
5
5
  "private": false,
6
6
  "publishConfig": {
7
7
  "access": "public"
@@ -45,8 +45,8 @@
45
45
  "description": "",
46
46
  "devDependencies": {
47
47
  "@axe-core/playwright": "^4.10.1",
48
- "@coopdigital/styles": "^0.15.2",
49
- "@playwright/test": "^1.51.1",
48
+ "@coopdigital/styles": "^0.15.3",
49
+ "@playwright/test": "^1.52.0",
50
50
  "@storybook/addon-a11y": "^8.6.12",
51
51
  "@storybook/addon-essentials": "^8.6.12",
52
52
  "@storybook/addon-interactions": "^8.6.12",
@@ -58,14 +58,14 @@
58
58
  "@storybook/test": "^8.6.12",
59
59
  "@storybook/theming": "^8.6.12",
60
60
  "@testing-library/jest-dom": "^6.6.3",
61
- "@testing-library/react": "^16.2.0",
62
- "@types/react": "^19.0.12",
63
- "@types/react-dom": "^19.0.4",
61
+ "@testing-library/react": "^16.3.0",
62
+ "@types/react": "^19.1.2",
63
+ "@types/react-dom": "^19.1.2",
64
64
  "storybook": "^8.6.12"
65
65
  },
66
66
  "peerDependencies": {
67
- "react": "^19.0.0",
68
- "react-dom": "^19.0.0"
67
+ "react": "^19.1.0",
68
+ "react-dom": "^19.1.0"
69
69
  },
70
- "gitHead": "0fa78c25e19ac1c1dc6af564c570e94646e71bc4"
70
+ "gitHead": "3bad30dd8fdb2c160a2f71e0c95c1da38d12b0ab"
71
71
  }
@@ -2,7 +2,18 @@ import type { DetailsHTMLAttributes, JSX } from "react"
2
2
 
3
3
  export interface ExpandableProps extends DetailsHTMLAttributes<HTMLDetailsElement> {
4
4
  /** **(Optional)** Specifies the Expandable background color from the available options. */
5
- background?: "grey" | "purple" | "pink" | "green" | "orange" | "red" | "yellow" | "lilac" | "blue"
5
+ background?:
6
+ | "grey"
7
+ | "purple"
8
+ | "pink"
9
+ | "green"
10
+ | "orange"
11
+ | "red"
12
+ | "yellow"
13
+ | "lilac"
14
+ | "blue"
15
+ | "white"
16
+ | "transparent"
6
17
  /** Represents the content inside the Expandable component, only visible when expanded. It can be any valid JSX or string. */
7
18
  children: React.ReactNode
8
19
  /** **(Optional)** Receives any className to be applied to Expandable component. */
@@ -17,7 +28,7 @@ export interface ExpandableProps extends DetailsHTMLAttributes<HTMLDetailsElemen
17
28
  */
18
29
 
19
30
  export const Expandable = ({
20
- background,
31
+ background = "grey",
21
32
  children,
22
33
  className = "",
23
34
  summary,
@@ -9,6 +9,7 @@ export const AddIcon = ({ alt, className, ...props }: IconProps) => {
9
9
  const componentProps = {
10
10
  "aria-labelledby": alt ? id : undefined,
11
11
  className: `coop-icon ${className ?? ""}`.trim(),
12
+ "data-icon": "add",
12
13
  fill: "none",
13
14
  role: alt ? "img" : undefined,
14
15
  viewBox: "0 0 32 32",
@@ -9,6 +9,7 @@ export const ArrowDownIcon = ({ alt, className, ...props }: IconProps) => {
9
9
  const componentProps = {
10
10
  "aria-labelledby": alt ? id : undefined,
11
11
  className: `coop-icon ${className ?? ""}`.trim(),
12
+ "data-icon": "arrow-down",
12
13
  fill: "none",
13
14
  role: alt ? "img" : undefined,
14
15
  viewBox: "0 0 32 32",
@@ -9,6 +9,7 @@ export const ArrowLeftIcon = ({ alt, className, ...props }: IconProps) => {
9
9
  const componentProps = {
10
10
  "aria-labelledby": alt ? id : undefined,
11
11
  className: `coop-icon ${className ?? ""}`.trim(),
12
+ "data-icon": "arrow-left",
12
13
  fill: "none",
13
14
  role: alt ? "img" : undefined,
14
15
  viewBox: "0 0 32 32",
@@ -9,6 +9,7 @@ export const ArrowRightIcon = ({ alt, className, ...props }: IconProps) => {
9
9
  const componentProps = {
10
10
  "aria-labelledby": alt ? id : undefined,
11
11
  className: `coop-icon ${className ?? ""}`.trim(),
12
+ "data-icon": "arrow-right",
12
13
  fill: "none",
13
14
  role: alt ? "img" : undefined,
14
15
  viewBox: "0 0 32 32",
@@ -9,6 +9,7 @@ export const ArrowUpIcon = ({ alt, className, ...props }: IconProps) => {
9
9
  const componentProps = {
10
10
  "aria-labelledby": alt ? id : undefined,
11
11
  className: `coop-icon ${className ?? ""}`.trim(),
12
+ "data-icon": "arrow-up",
12
13
  fill: "none",
13
14
  role: alt ? "img" : undefined,
14
15
  viewBox: "0 0 32 32",
@@ -9,6 +9,7 @@ export const AvatarAltIcon = ({ alt, className, ...props }: IconProps) => {
9
9
  const componentProps = {
10
10
  "aria-labelledby": alt ? id : undefined,
11
11
  className: `coop-icon ${className ?? ""}`.trim(),
12
+ "data-icon": "avatar-alt",
12
13
  fill: "none",
13
14
  role: alt ? "img" : undefined,
14
15
  viewBox: "0 0 32 32",
@@ -9,6 +9,7 @@ export const AvatarIcon = ({ alt, className, ...props }: IconProps) => {
9
9
  const componentProps = {
10
10
  "aria-labelledby": alt ? id : undefined,
11
11
  className: `coop-icon ${className ?? ""}`.trim(),
12
+ "data-icon": "avatar",
12
13
  fill: "none",
13
14
  role: alt ? "img" : undefined,
14
15
  viewBox: "0 0 32 32",
@@ -9,6 +9,7 @@ export const BasketIcon = ({ alt, className, ...props }: IconProps) => {
9
9
  const componentProps = {
10
10
  "aria-labelledby": alt ? id : undefined,
11
11
  className: `coop-icon ${className ?? ""}`.trim(),
12
+ "data-icon": "basket",
12
13
  fill: "none",
13
14
  role: alt ? "img" : undefined,
14
15
  viewBox: "0 0 32 32",
@@ -9,6 +9,7 @@ export const CalendarIcon = ({ alt, className, ...props }: IconProps) => {
9
9
  const componentProps = {
10
10
  "aria-labelledby": alt ? id : undefined,
11
11
  className: `coop-icon ${className ?? ""}`.trim(),
12
+ "data-icon": "calendar",
12
13
  fill: "none",
13
14
  role: alt ? "img" : undefined,
14
15
  viewBox: "0 0 32 32",
@@ -9,6 +9,7 @@ export const ChevronDownIcon = ({ alt, className, ...props }: IconProps) => {
9
9
  const componentProps = {
10
10
  "aria-labelledby": alt ? id : undefined,
11
11
  className: `coop-icon ${className ?? ""}`.trim(),
12
+ "data-icon": "chevron-down",
12
13
  fill: "none",
13
14
  role: alt ? "img" : undefined,
14
15
  viewBox: "0 0 32 32",
@@ -9,6 +9,7 @@ export const ChevronLeftIcon = ({ alt, className, ...props }: IconProps) => {
9
9
  const componentProps = {
10
10
  "aria-labelledby": alt ? id : undefined,
11
11
  className: `coop-icon ${className ?? ""}`.trim(),
12
+ "data-icon": "chevron-left",
12
13
  fill: "none",
13
14
  role: alt ? "img" : undefined,
14
15
  viewBox: "0 0 32 32",
@@ -9,6 +9,7 @@ export const ChevronRightIcon = ({ alt, className, ...props }: IconProps) => {
9
9
  const componentProps = {
10
10
  "aria-labelledby": alt ? id : undefined,
11
11
  className: `coop-icon ${className ?? ""}`.trim(),
12
+ "data-icon": "chevron-right",
12
13
  fill: "none",
13
14
  role: alt ? "img" : undefined,
14
15
  viewBox: "0 0 32 32",
@@ -9,6 +9,7 @@ export const ChevronUpIcon = ({ alt, className, ...props }: IconProps) => {
9
9
  const componentProps = {
10
10
  "aria-labelledby": alt ? id : undefined,
11
11
  className: `coop-icon ${className ?? ""}`.trim(),
12
+ "data-icon": "chevron-up",
12
13
  fill: "none",
13
14
  role: alt ? "img" : undefined,
14
15
  viewBox: "0 0 32 32",
@@ -9,6 +9,7 @@ export const ClockIcon = ({ alt, className, ...props }: IconProps) => {
9
9
  const componentProps = {
10
10
  "aria-labelledby": alt ? id : undefined,
11
11
  className: `coop-icon ${className ?? ""}`.trim(),
12
+ "data-icon": "clock",
12
13
  fill: "none",
13
14
  role: alt ? "img" : undefined,
14
15
  viewBox: "0 0 32 32",
@@ -9,6 +9,7 @@ export const CloseAltIcon = ({ alt, className, ...props }: IconProps) => {
9
9
  const componentProps = {
10
10
  "aria-labelledby": alt ? id : undefined,
11
11
  className: `coop-icon ${className ?? ""}`.trim(),
12
+ "data-icon": "close-alt",
12
13
  fill: "none",
13
14
  role: alt ? "img" : undefined,
14
15
  viewBox: "0 0 32 32",
@@ -9,6 +9,7 @@ export const CloseIcon = ({ alt, className, ...props }: IconProps) => {
9
9
  const componentProps = {
10
10
  "aria-labelledby": alt ? id : undefined,
11
11
  className: `coop-icon ${className ?? ""}`.trim(),
12
+ "data-icon": "close",
12
13
  fill: "none",
13
14
  role: alt ? "img" : undefined,
14
15
  viewBox: "0 0 32 32",
@@ -9,6 +9,7 @@ export const CoopCardIcon = ({ alt, className, ...props }: IconProps) => {
9
9
  const componentProps = {
10
10
  "aria-labelledby": alt ? id : undefined,
11
11
  className: `coop-icon ${className ?? ""}`.trim(),
12
+ "data-icon": "coop-card",
12
13
  fill: "none",
13
14
  role: alt ? "img" : undefined,
14
15
  viewBox: "0 0 32 32",
@@ -9,6 +9,7 @@ export const CoopIcon = ({ alt, className, ...props }: IconProps) => {
9
9
  const componentProps = {
10
10
  "aria-labelledby": alt ? id : undefined,
11
11
  className: `coop-icon ${className ?? ""}`.trim(),
12
+ "data-icon": "coop",
12
13
  fill: "none",
13
14
  role: alt ? "img" : undefined,
14
15
  viewBox: "0 0 32 32",
@@ -9,6 +9,7 @@ export const CoopLocationIcon = ({ alt, className, ...props }: IconProps) => {
9
9
  const componentProps = {
10
10
  "aria-labelledby": alt ? id : undefined,
11
11
  className: `coop-icon ${className ?? ""}`.trim(),
12
+ "data-icon": "coop-location",
12
13
  fill: "none",
13
14
  role: alt ? "img" : undefined,
14
15
  viewBox: "0 0 32 32",
@@ -9,6 +9,7 @@ export const DownloadIcon = ({ alt, className, ...props }: IconProps) => {
9
9
  const componentProps = {
10
10
  "aria-labelledby": alt ? id : undefined,
11
11
  className: `coop-icon ${className ?? ""}`.trim(),
12
+ "data-icon": "download",
12
13
  fill: "none",
13
14
  role: alt ? "img" : undefined,
14
15
  viewBox: "0 0 32 32",
@@ -9,6 +9,7 @@ export const HomeIcon = ({ alt, className, ...props }: IconProps) => {
9
9
  const componentProps = {
10
10
  "aria-labelledby": alt ? id : undefined,
11
11
  className: `coop-icon ${className ?? ""}`.trim(),
12
+ "data-icon": "home",
12
13
  fill: "none",
13
14
  role: alt ? "img" : undefined,
14
15
  viewBox: "0 0 32 32",
@@ -9,6 +9,7 @@ export const InformationIcon = ({ alt, className, ...props }: IconProps) => {
9
9
  const componentProps = {
10
10
  "aria-labelledby": alt ? id : undefined,
11
11
  className: `coop-icon ${className ?? ""}`.trim(),
12
+ "data-icon": "information",
12
13
  fill: "none",
13
14
  role: alt ? "img" : undefined,
14
15
  viewBox: "0 0 32 32",
@@ -9,6 +9,7 @@ export const LocationIcon = ({ alt, className, ...props }: IconProps) => {
9
9
  const componentProps = {
10
10
  "aria-labelledby": alt ? id : undefined,
11
11
  className: `coop-icon ${className ?? ""}`.trim(),
12
+ "data-icon": "location",
12
13
  fill: "none",
13
14
  role: alt ? "img" : undefined,
14
15
  viewBox: "0 0 32 32",
@@ -9,6 +9,7 @@ export const MailIcon = ({ alt, className, ...props }: IconProps) => {
9
9
  const componentProps = {
10
10
  "aria-labelledby": alt ? id : undefined,
11
11
  className: `coop-icon ${className ?? ""}`.trim(),
12
+ "data-icon": "mail",
12
13
  fill: "none",
13
14
  role: alt ? "img" : undefined,
14
15
  viewBox: "0 0 32 32",
@@ -9,6 +9,7 @@ export const MenuIcon = ({ alt, className, ...props }: IconProps) => {
9
9
  const componentProps = {
10
10
  "aria-labelledby": alt ? id : undefined,
11
11
  className: `coop-icon ${className ?? ""}`.trim(),
12
+ "data-icon": "menu",
12
13
  fill: "none",
13
14
  role: alt ? "img" : undefined,
14
15
  viewBox: "0 0 32 32",
@@ -9,6 +9,7 @@ export const MessageIcon = ({ alt, className, ...props }: IconProps) => {
9
9
  const componentProps = {
10
10
  "aria-labelledby": alt ? id : undefined,
11
11
  className: `coop-icon ${className ?? ""}`.trim(),
12
+ "data-icon": "message",
12
13
  fill: "none",
13
14
  role: alt ? "img" : undefined,
14
15
  viewBox: "0 0 32 32",
@@ -9,6 +9,7 @@ export const MinusIcon = ({ alt, className, ...props }: IconProps) => {
9
9
  const componentProps = {
10
10
  "aria-labelledby": alt ? id : undefined,
11
11
  className: `coop-icon ${className ?? ""}`.trim(),
12
+ "data-icon": "minus",
12
13
  fill: "none",
13
14
  role: alt ? "img" : undefined,
14
15
  viewBox: "0 0 32 32",
@@ -9,6 +9,7 @@ export const OpenNewIcon = ({ alt, className, ...props }: IconProps) => {
9
9
  const componentProps = {
10
10
  "aria-labelledby": alt ? id : undefined,
11
11
  className: `coop-icon ${className ?? ""}`.trim(),
12
+ "data-icon": "open-new",
12
13
  fill: "none",
13
14
  role: alt ? "img" : undefined,
14
15
  viewBox: "0 0 32 32",
@@ -9,6 +9,7 @@ export const PencilIcon = ({ alt, className, ...props }: IconProps) => {
9
9
  const componentProps = {
10
10
  "aria-labelledby": alt ? id : undefined,
11
11
  className: `coop-icon ${className ?? ""}`.trim(),
12
+ "data-icon": "pencil",
12
13
  fill: "none",
13
14
  role: alt ? "img" : undefined,
14
15
  viewBox: "0 0 32 32",
@@ -9,6 +9,7 @@ export const PhoneIcon = ({ alt, className, ...props }: IconProps) => {
9
9
  const componentProps = {
10
10
  "aria-labelledby": alt ? id : undefined,
11
11
  className: `coop-icon ${className ?? ""}`.trim(),
12
+ "data-icon": "phone",
12
13
  fill: "none",
13
14
  role: alt ? "img" : undefined,
14
15
  viewBox: "0 0 32 32",
@@ -9,6 +9,7 @@ export const QuestionIcon = ({ alt, className, ...props }: IconProps) => {
9
9
  const componentProps = {
10
10
  "aria-labelledby": alt ? id : undefined,
11
11
  className: `coop-icon ${className ?? ""}`.trim(),
12
+ "data-icon": "question",
12
13
  fill: "none",
13
14
  role: alt ? "img" : undefined,
14
15
  viewBox: "0 0 32 32",
@@ -9,6 +9,7 @@ export const ScooterIcon = ({ alt, className, ...props }: IconProps) => {
9
9
  const componentProps = {
10
10
  "aria-labelledby": alt ? id : undefined,
11
11
  className: `coop-icon ${className ?? ""}`.trim(),
12
+ "data-icon": "scooter",
12
13
  fill: "none",
13
14
  role: alt ? "img" : undefined,
14
15
  viewBox: "0 0 32 32",
@@ -9,6 +9,7 @@ export const SearchIcon = ({ alt, className, ...props }: IconProps) => {
9
9
  const componentProps = {
10
10
  "aria-labelledby": alt ? id : undefined,
11
11
  className: `coop-icon ${className ?? ""}`.trim(),
12
+ "data-icon": "search",
12
13
  fill: "none",
13
14
  role: alt ? "img" : undefined,
14
15
  viewBox: "0 0 32 32",
@@ -9,6 +9,7 @@ export const SettingsIcon = ({ alt, className, ...props }: IconProps) => {
9
9
  const componentProps = {
10
10
  "aria-labelledby": alt ? id : undefined,
11
11
  className: `coop-icon ${className ?? ""}`.trim(),
12
+ "data-icon": "settings",
12
13
  fill: "none",
13
14
  role: alt ? "img" : undefined,
14
15
  viewBox: "0 0 32 32",
@@ -9,6 +9,7 @@ export const TickAltIcon = ({ alt, className, ...props }: IconProps) => {
9
9
  const componentProps = {
10
10
  "aria-labelledby": alt ? id : undefined,
11
11
  className: `coop-icon ${className ?? ""}`.trim(),
12
+ "data-icon": "tick-alt",
12
13
  fill: "none",
13
14
  role: alt ? "img" : undefined,
14
15
  viewBox: "0 0 32 32",
@@ -9,6 +9,7 @@ export const TickIcon = ({ alt, className, ...props }: IconProps) => {
9
9
  const componentProps = {
10
10
  "aria-labelledby": alt ? id : undefined,
11
11
  className: `coop-icon ${className ?? ""}`.trim(),
12
+ "data-icon": "tick",
12
13
  fill: "none",
13
14
  role: alt ? "img" : undefined,
14
15
  viewBox: "0 0 32 32",
@@ -9,6 +9,7 @@ export const VanIcon = ({ alt, className, ...props }: IconProps) => {
9
9
  const componentProps = {
10
10
  "aria-labelledby": alt ? id : undefined,
11
11
  className: `coop-icon ${className ?? ""}`.trim(),
12
+ "data-icon": "van",
12
13
  fill: "none",
13
14
  role: alt ? "img" : undefined,
14
15
  viewBox: "0 0 32 32",
@@ -9,6 +9,7 @@ export const WarningIcon = ({ alt, className, ...props }: IconProps) => {
9
9
  const componentProps = {
10
10
  "aria-labelledby": alt ? id : undefined,
11
11
  className: `coop-icon ${className ?? ""}`.trim(),
12
+ "data-icon": "warning",
12
13
  fill: "none",
13
14
  role: alt ? "img" : undefined,
14
15
  viewBox: "0 0 32 32",
@@ -9,6 +9,7 @@ export const WriteIcon = ({ alt, className, ...props }: IconProps) => {
9
9
  const componentProps = {
10
10
  "aria-labelledby": alt ? id : undefined,
11
11
  className: `coop-icon ${className ?? ""}`.trim(),
12
+ "data-icon": "write",
12
13
  fill: "none",
13
14
  role: alt ? "img" : undefined,
14
15
  viewBox: "0 0 32 32",
@@ -0,0 +1,131 @@
1
+ import React, { HTMLAttributes, useCallback, useId, useState } from "react"
2
+ import { type JSX } from "react"
3
+
4
+ import { Button, type ButtonProps } from "../Button"
5
+ import { SearchIcon } from "../Icon"
6
+
7
+ export interface SearchBoxProps extends HTMLAttributes<HTMLInputElement> {
8
+ /** **(Optional)** Server endpoint to submit the form. Will be ignored if onSubmit is also set. */
9
+ action?: string
10
+ /** **(Optional)** Whether or not to enable auto complete on the input field. Default: `off`. */
11
+ autoComplete?: "off" | "on"
12
+ /** **(Optional)** Props to forward to the Button element. Use `label` to set Button text. */
13
+ button?: Pick<ButtonProps, "className" | "loadingText"> & { label?: React.ReactNode }
14
+ /** **(Optional)** Receives any className to be applied to SearchBox component. */
15
+ className?: string
16
+ /** Receives the label to be applied to SearchBox component. */
17
+ label: string
18
+ /** **(Optional)** Receives whether label is visible to human or only screen readers. */
19
+ labelVisible?: boolean
20
+ /** **(Optional)** Name of the input element, also used as the URL search parameter. Defaults to `query` */
21
+ name?: string
22
+ /** **(Optional)** Callback to run when the form is submitted. If this is an async function, it will be awaited and the SearchBox will be in a pending state until the promise is resolved. */
23
+ onSubmit?: React.FormEventHandler<HTMLElement> | undefined
24
+ /** **(Optional)** Receives any placeholder for SearchBox component. */
25
+ placeholder?: string
26
+ /** **(Optional)** Receives any size to be applied to SearchBox component. */
27
+ size?: string
28
+ /** **(Optional)** Receives the variant to be applied to SearchBox component. */
29
+ variant?: "primary" | "secondary" | "ghost"
30
+ }
31
+
32
+ const defaultButtonProps: SearchBoxProps["button"] = {
33
+ label: React.createElement(SearchIcon, { alt: "Search", stroke: "currentColor", strokeWidth: 2 }),
34
+ loadingText: "",
35
+ }
36
+
37
+ /**
38
+ * SearchBox component allows a person to enter a word or a phrase to find relevant content.
39
+ *
40
+ * The `label` prop is mandatory but not visible by default (it is visible for screen readers). You can make it visible to humans by setting `labelVisible` to true.
41
+ *
42
+ * If you are using a single icon with no text for `button.label`, ensure you pass an `alt` property to the icon for accessibility purposes.
43
+ *
44
+ * You can provide an `action` to submit input like a normal HTML form, or an `onSubmit` callback to handle submissions client-side.
45
+ *
46
+ * **Good to know:** SearchBox requires both `SearchBox` and `Button` css/scss files.
47
+ *
48
+ * ```
49
+ * import "@coopdigital/styles/components/Searchbox.css"
50
+ * import "@coopdigital/styles/components/Button.css"
51
+ *
52
+ * ```
53
+ */
54
+ export const SearchBox = ({
55
+ action,
56
+ "aria-placeholder": ariaPlaceholder,
57
+ autoCapitalize = "off",
58
+ autoComplete = "off",
59
+ button = defaultButtonProps,
60
+ className,
61
+ label,
62
+ labelVisible = false,
63
+ name = "query",
64
+ onSubmit,
65
+ placeholder,
66
+ size = "md",
67
+ variant = "primary",
68
+ ...props
69
+ }: SearchBoxProps): JSX.Element => {
70
+ const [isPending, setIsPending] = useState(false)
71
+
72
+ const handleSubmit = useCallback(
73
+ async (event: React.FormEvent<HTMLFormElement>) => {
74
+ event.preventDefault()
75
+
76
+ if (isPending || !onSubmit) return
77
+
78
+ setIsPending(true)
79
+
80
+ try {
81
+ await Promise.resolve(onSubmit(event))
82
+ } finally {
83
+ setIsPending(false)
84
+ }
85
+ },
86
+ [onSubmit, isPending]
87
+ )
88
+
89
+ const id = useId()
90
+
91
+ const formProps = {
92
+ action: action ?? undefined,
93
+ className: `coop-search-box ${className ?? ""}`.trim(),
94
+ "data-size": size.length && size !== "md" ? size : undefined,
95
+ "data-variant": variant.length && variant !== "primary" ? variant : undefined,
96
+ onSubmit: onSubmit ? handleSubmit : undefined,
97
+ }
98
+
99
+ const buttonProps = {
100
+ className: button?.className,
101
+ isLoading: isPending,
102
+ loadingText: button?.loadingText ?? "",
103
+ size: size as keyof ButtonProps["size"],
104
+ variant: variant as keyof ButtonProps["variant"],
105
+ }
106
+
107
+ const inputProps = {
108
+ "aria-placeholder": placeholder ?? ariaPlaceholder ?? undefined,
109
+ autoCapitalize,
110
+ autoComplete,
111
+ id,
112
+ name,
113
+ placeholder,
114
+ type: "search",
115
+ ...props,
116
+ }
117
+
118
+ return (
119
+ <form {...formProps}>
120
+ <label className={labelVisible ? "" : "sr-only"} htmlFor={id}>
121
+ {label}
122
+ </label>
123
+ <div className="coop-search-box--inner">
124
+ <input {...inputProps}></input>
125
+ <Button {...buttonProps}>{button.label}</Button>
126
+ </div>
127
+ </form>
128
+ )
129
+ }
130
+
131
+ export default SearchBox
@@ -0,0 +1,5 @@
1
+ import SearchBox from "./SearchBox"
2
+
3
+ export default SearchBox
4
+ export { SearchBox }
5
+ export * from "./SearchBox"
package/src/index.ts CHANGED
@@ -6,6 +6,7 @@ export * from "./components/Expandable"
6
6
  export * from "./components/Image"
7
7
  export * from "./components/Pill"
8
8
  export * from "./components/RootSVG"
9
+ export * from "./components/SearchBox"
9
10
  export * from "./components/Signpost"
10
11
  export * from "./components/SkipNav"
11
12
  export * from "./components/Tag"