@ndla/primitives 0.0.2 → 0.0.4

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 (89) hide show
  1. package/dist/panda.buildinfo.json +95 -25
  2. package/dist/styles.css +340 -50
  3. package/es/ArticleLists.js +4 -32
  4. package/es/Badge.js +4 -16
  5. package/es/BlockQuote.js +5 -17
  6. package/es/Button.js +40 -33
  7. package/es/Checkbox.js +160 -51
  8. package/es/Dialog.js +6 -7
  9. package/es/ExpandableBox.js +4 -17
  10. package/es/Field.js +17 -0
  11. package/es/FieldErrorMessage.js +12 -16
  12. package/es/FieldHelper.js +9 -12
  13. package/es/FramedContent.js +3 -15
  14. package/es/Icon.js +9 -6
  15. package/es/Input.js +25 -23
  16. package/es/Label.js +15 -39
  17. package/es/Menu.js +32 -6
  18. package/es/MessageBox.js +3 -15
  19. package/es/NdlaLogo.js +6 -5
  20. package/es/RadioGroup.js +7 -11
  21. package/es/Skeleton.js +2 -8
  22. package/es/Spinner.js +3 -15
  23. package/es/Switch.js +8 -15
  24. package/es/Table.js +2 -5
  25. package/es/Tabs.js +231 -0
  26. package/es/Text.js +33 -32
  27. package/es/Toast.js +14 -6
  28. package/es/createStyleContext.js +15 -7
  29. package/lib/Accordion.d.ts +14 -7
  30. package/lib/ArticleLists.d.ts +9 -11
  31. package/lib/ArticleLists.js +5 -36
  32. package/lib/Badge.d.ts +9 -4
  33. package/lib/Badge.js +4 -17
  34. package/lib/BlockQuote.d.ts +9 -3
  35. package/lib/BlockQuote.js +5 -18
  36. package/lib/Button.d.ts +24 -11
  37. package/lib/Button.js +39 -32
  38. package/lib/Checkbox.d.ts +172 -5
  39. package/lib/Checkbox.js +165 -56
  40. package/lib/Dialog.d.ts +18 -10
  41. package/lib/Dialog.js +6 -7
  42. package/lib/ExpandableBox.d.ts +8 -5
  43. package/lib/ExpandableBox.js +5 -20
  44. package/lib/Field.d.ts +9 -0
  45. package/lib/Field.js +23 -0
  46. package/lib/FieldErrorMessage.d.ts +3 -3
  47. package/lib/FieldErrorMessage.js +11 -15
  48. package/lib/FieldHelper.d.ts +3 -3
  49. package/lib/FieldHelper.js +8 -11
  50. package/lib/FramedContent.d.ts +8 -4
  51. package/lib/FramedContent.js +3 -16
  52. package/lib/Icon.d.ts +3 -2
  53. package/lib/Icon.js +8 -5
  54. package/lib/Input.d.ts +12 -11
  55. package/lib/Input.js +25 -23
  56. package/lib/Label.d.ts +13 -7
  57. package/lib/Label.js +15 -39
  58. package/lib/Menu.d.ts +66 -13
  59. package/lib/Menu.js +41 -15
  60. package/lib/MessageBox.d.ts +8 -4
  61. package/lib/MessageBox.js +3 -16
  62. package/lib/NdlaLogo.d.ts +5 -4
  63. package/lib/NdlaLogo.js +5 -4
  64. package/lib/Pagination.d.ts +17 -6
  65. package/lib/Popover.d.ts +34 -13
  66. package/lib/RadioGroup.d.ts +18 -7
  67. package/lib/RadioGroup.js +7 -11
  68. package/lib/Skeleton.d.ts +5 -2
  69. package/lib/Skeleton.js +3 -10
  70. package/lib/Slider.d.ts +18 -7
  71. package/lib/Spinner.d.ts +8 -3
  72. package/lib/Spinner.js +3 -16
  73. package/lib/Switch.d.ts +12 -7
  74. package/lib/Switch.js +13 -20
  75. package/lib/Table.d.ts +6 -3
  76. package/lib/Table.js +4 -8
  77. package/lib/Tabs.d.ts +145 -0
  78. package/lib/Tabs.js +239 -0
  79. package/lib/Text.d.ts +6 -9
  80. package/lib/Text.js +32 -33
  81. package/lib/Toast.d.ts +10 -7
  82. package/lib/Toast.js +15 -7
  83. package/lib/Tooltip.d.ts +19 -7
  84. package/lib/createStyleContext.d.ts +5 -3
  85. package/lib/createStyleContext.js +14 -6
  86. package/package.json +5 -5
  87. package/es/FormControl.js +0 -163
  88. package/lib/FormControl.d.ts +0 -65
  89. package/lib/FormControl.js +0 -173
package/lib/Text.js CHANGED
@@ -4,7 +4,10 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.Text = exports.Heading = void 0;
7
+ var _react = require("react");
8
+ var _react2 = require("@ark-ui/react");
7
9
  var _css = require("@ndla/styled-system/css");
10
+ var _jsx2 = require("@ndla/styled-system/jsx");
8
11
  var _jsxRuntime = require("react/jsx-runtime");
9
12
  /**
10
13
  * Copyright (c) 2024-present, NDLA.
@@ -14,49 +17,45 @@ var _jsxRuntime = require("react/jsx-runtime");
14
17
  *
15
18
  */
16
19
 
17
- const BaseText = props => {
18
- const {
19
- as: As = "p",
20
- className,
20
+ const StyledP = (0, _jsx2.styled)(_react2.ark.p);
21
+ const Text = exports.Text = /*#__PURE__*/(0, _react.forwardRef)((_ref, ref) => {
22
+ let {
23
+ textStyle = "body.medium",
21
24
  fontWeight,
22
25
  color,
23
- textStyle = "body.medium",
24
26
  srOnly,
27
+ css: cssProp,
25
28
  ...rest
26
- } = props;
27
- return /*#__PURE__*/(0, _jsxRuntime.jsx)(As, {
28
- className: (0, _css.cx)((0, _css.css)({
29
+ } = _ref;
30
+ return /*#__PURE__*/(0, _jsxRuntime.jsx)(StyledP, {
31
+ css: _css.css.raw({
29
32
  textStyle,
30
33
  fontWeight,
31
34
  color,
32
- srOnly: srOnly
33
- }), className),
35
+ srOnly
36
+ }, cssProp),
37
+ ref: ref,
34
38
  ...rest
35
39
  });
36
- };
37
- const Heading = props => {
38
- const {
39
- as = "h1",
40
+ });
41
+ const StyledH1 = (0, _jsx2.styled)(_react2.ark.h1);
42
+ const Heading = exports.Heading = /*#__PURE__*/(0, _react.forwardRef)((_ref2, ref) => {
43
+ let {
40
44
  textStyle = "heading.medium",
45
+ fontWeight,
46
+ color,
47
+ srOnly,
48
+ css: cssProp,
41
49
  ...rest
42
- } = props;
43
- return /*#__PURE__*/(0, _jsxRuntime.jsx)(BaseText, {
44
- as: as,
45
- textStyle: textStyle,
46
- ...rest
47
- });
48
- };
49
- exports.Heading = Heading;
50
- const Text = props => {
51
- const {
52
- as = "p",
53
- textStyle = "body.medium",
54
- ...rest
55
- } = props;
56
- return /*#__PURE__*/(0, _jsxRuntime.jsx)(BaseText, {
57
- as: as,
58
- textStyle: textStyle,
50
+ } = _ref2;
51
+ return /*#__PURE__*/(0, _jsxRuntime.jsx)(StyledH1, {
52
+ css: _css.css.raw({
53
+ textStyle,
54
+ fontWeight,
55
+ color,
56
+ srOnly
57
+ }, cssProp),
58
+ ref: ref,
59
59
  ...rest
60
60
  });
61
- };
62
- exports.Text = Text;
61
+ });
package/lib/Toast.d.ts CHANGED
@@ -6,13 +6,16 @@
6
6
  *
7
7
  */
8
8
  import { Toast } from "@ark-ui/react";
9
+ import { JsxStyleProps } from "@ndla/styled-system/types";
9
10
  import { TextProps } from "./Text";
10
- export interface RootProps extends Toast.RootProps {
11
+ export interface RootProps extends Toast.RootProps, JsxStyleProps {
11
12
  }
12
13
  export declare const ToastRoot: import("react").ForwardRefExoticComponent<RootProps & import("react").RefAttributes<HTMLDivElement>>;
13
- export declare const ToastActionTrigger: import("react").ForwardRefExoticComponent<Toast.ActionTriggerProps & import("react").RefAttributes<HTMLButtonElement>>;
14
- export declare const ToastCloseTrigger: import("react").ForwardRefExoticComponent<Toast.CloseTriggerProps & import("react").RefAttributes<HTMLDivElement>>;
15
- export declare const InternalToastDescription: import("react").ForwardRefExoticComponent<Toast.DescriptionProps & import("react").RefAttributes<HTMLDivElement>>;
16
- export declare const ToastDescription: ({ textStyle, ...props }: Toast.DescriptionProps & TextProps) => import("@emotion/react/jsx-runtime").JSX.Element;
17
- export declare const InternalToastTitle: import("react").ForwardRefExoticComponent<Toast.TitleProps & import("react").RefAttributes<HTMLDivElement>>;
18
- export declare const ToastTitle: ({ textStyle, fontWeight, ...props }: Toast.TitleProps & TextProps) => import("@emotion/react/jsx-runtime").JSX.Element;
14
+ export declare const ToastActionTrigger: import("react").ForwardRefExoticComponent<{
15
+ forwardCssProp?: boolean | undefined;
16
+ } & import("@ndla/styled-system/types").WithCss & Toast.ActionTriggerProps & import("react").RefAttributes<HTMLButtonElement>>;
17
+ export declare const ToastCloseTrigger: import("react").ForwardRefExoticComponent<{
18
+ forwardCssProp?: boolean | undefined;
19
+ } & import("@ndla/styled-system/types").WithCss & Toast.CloseTriggerProps & import("react").RefAttributes<HTMLDivElement>>;
20
+ export declare const ToastDescription: ({ textStyle, children, ...props }: Toast.DescriptionProps & TextProps & JsxStyleProps) => import("react/jsx-runtime").JSX.Element;
21
+ export declare const ToastTitle: ({ textStyle, fontWeight, children, ...props }: JsxStyleProps & Toast.TitleProps & TextProps) => import("react/jsx-runtime").JSX.Element;
package/lib/Toast.js CHANGED
@@ -3,7 +3,7 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.ToastTitle = exports.ToastRoot = exports.ToastDescription = exports.ToastCloseTrigger = exports.ToastActionTrigger = exports.InternalToastTitle = exports.InternalToastDescription = void 0;
6
+ exports.ToastTitle = exports.ToastRoot = exports.ToastDescription = exports.ToastCloseTrigger = exports.ToastActionTrigger = void 0;
7
7
  var _anatomy = require("@ark-ui/anatomy");
8
8
  var _react = require("@ark-ui/react");
9
9
  var _css = require("@ndla/styled-system/css");
@@ -54,36 +54,44 @@ const {
54
54
  const ToastRoot = exports.ToastRoot = withProvider(_react.Toast.Root, "root");
55
55
  const ToastActionTrigger = exports.ToastActionTrigger = withContext(_react.Toast.ActionTrigger, "actionTrigger");
56
56
  const ToastCloseTrigger = exports.ToastCloseTrigger = withContext(_react.Toast.CloseTrigger, "closeTrigger");
57
- const InternalToastDescription = exports.InternalToastDescription = withContext(_react.Toast.Description, "description");
57
+ const InternalToastDescription = withContext(_react.Toast.Description, "description");
58
58
  const ToastDescription = _ref => {
59
59
  let {
60
60
  textStyle = "label.medium",
61
+ children,
61
62
  ...props
62
63
  } = _ref;
63
64
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(InternalToastDescription, {
64
65
  asChild: true,
65
66
  children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_Text.Text, {
66
- as: "div",
67
+ asChild: true,
67
68
  textStyle: textStyle,
68
- ...props
69
+ ...props,
70
+ children: /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
71
+ children: children
72
+ })
69
73
  })
70
74
  });
71
75
  };
72
76
  exports.ToastDescription = ToastDescription;
73
- const InternalToastTitle = exports.InternalToastTitle = withContext(_react.Toast.Title, "title");
77
+ const InternalToastTitle = withContext(_react.Toast.Title, "title");
74
78
  const ToastTitle = _ref2 => {
75
79
  let {
76
80
  textStyle = "label.medium",
77
81
  fontWeight = "semibold",
82
+ children,
78
83
  ...props
79
84
  } = _ref2;
80
85
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(InternalToastTitle, {
81
86
  asChild: true,
82
87
  children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_Text.Text, {
83
- as: "div",
88
+ asChild: true,
84
89
  fontWeight: fontWeight,
85
90
  textStyle: textStyle,
86
- ...props
91
+ ...props,
92
+ children: /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
93
+ children: children
94
+ })
87
95
  })
88
96
  });
89
97
  };
package/lib/Tooltip.d.ts CHANGED
@@ -7,10 +7,22 @@
7
7
  */
8
8
  import { Tooltip } from "@ark-ui/react";
9
9
  export type TooltipRootProps = Tooltip.RootProps;
10
- export declare const TooltipRoot: (props: Tooltip.RootProps) => import("@emotion/react/jsx-runtime").JSX.Element;
11
- export declare const TooltipArrow: import("react").ForwardRefExoticComponent<Tooltip.ArrowProps & import("react").RefAttributes<HTMLDivElement>>;
12
- export declare const TooltipArrowTip: import("react").ForwardRefExoticComponent<Tooltip.ArrowTipProps & import("react").RefAttributes<HTMLDivElement>>;
13
- export declare const TooltipContentStandalone: import("react").ForwardRefExoticComponent<Tooltip.ContentProps & import("react").RefAttributes<HTMLDivElement>>;
14
- export declare const TooltipContent: import("react").ForwardRefExoticComponent<Tooltip.ContentProps & import("react").RefAttributes<HTMLDivElement>>;
15
- export declare const TooltipPositioner: import("react").ForwardRefExoticComponent<Tooltip.PositionerProps & import("react").RefAttributes<HTMLDivElement>>;
16
- export declare const TooltipTrigger: import("react").ForwardRefExoticComponent<Tooltip.TriggerProps & import("react").RefAttributes<HTMLButtonElement>>;
10
+ export declare const TooltipRoot: (props: Tooltip.RootProps) => import("react/jsx-runtime").JSX.Element;
11
+ export declare const TooltipArrow: import("react").ForwardRefExoticComponent<{
12
+ forwardCssProp?: boolean | undefined;
13
+ } & import("@ndla/styled-system/types").WithCss & Tooltip.ArrowProps & import("react").RefAttributes<HTMLDivElement>>;
14
+ export declare const TooltipArrowTip: import("react").ForwardRefExoticComponent<{
15
+ forwardCssProp?: boolean | undefined;
16
+ } & import("@ndla/styled-system/types").WithCss & Tooltip.ArrowTipProps & import("react").RefAttributes<HTMLDivElement>>;
17
+ export declare const TooltipContentStandalone: import("react").ForwardRefExoticComponent<{
18
+ forwardCssProp?: boolean | undefined;
19
+ } & import("@ndla/styled-system/types").WithCss & Tooltip.ContentProps & import("react").RefAttributes<HTMLDivElement>>;
20
+ export declare const TooltipContent: import("react").ForwardRefExoticComponent<{
21
+ forwardCssProp?: boolean | undefined;
22
+ } & import("@ndla/styled-system/types").WithCss & Tooltip.ContentProps & import("react").RefAttributes<HTMLDivElement>>;
23
+ export declare const TooltipPositioner: import("react").ForwardRefExoticComponent<{
24
+ forwardCssProp?: boolean | undefined;
25
+ } & import("@ndla/styled-system/types").WithCss & Tooltip.PositionerProps & import("react").RefAttributes<HTMLDivElement>>;
26
+ export declare const TooltipTrigger: import("react").ForwardRefExoticComponent<{
27
+ forwardCssProp?: boolean | undefined;
28
+ } & import("@ndla/styled-system/types").WithCss & Tooltip.TriggerProps & import("react").RefAttributes<HTMLButtonElement>>;
@@ -6,9 +6,11 @@
6
6
  *
7
7
  */
8
8
  import { type ElementType, type ForwardRefExoticComponent, type PropsWithoutRef, type RefAttributes } from "react";
9
+ import { WithCss } from "@ndla/styled-system/types";
9
10
  type Props = Record<string, unknown>;
10
11
  type Recipe = {
11
12
  (props?: Props): Props;
13
+ raw: (props?: Props) => Props;
12
14
  splitVariantProps: (props: Props) => [Props, Props];
13
15
  };
14
16
  type Slot<R extends Recipe> = keyof ReturnType<R>;
@@ -16,12 +18,12 @@ type Slot<R extends Recipe> = keyof ReturnType<R>;
16
18
  * A utility for creating a style context for a recipe, allowing one to change the styles of all parts of a component from the root component. Credit: https://github.com/cschroeter/park-ui/blob/main/website/src/lib/create-style-context.tsx.
17
19
  */
18
20
  export declare const createStyleContext: <R extends Recipe>(recipe: R) => {
19
- withRootProvider: <P extends {}>(Component: ElementType) => (props: P) => import("@emotion/react/jsx-runtime").JSX.Element;
21
+ withRootProvider: <P extends {}>(Component: ElementType) => (props: P) => import("react/jsx-runtime").JSX.Element;
20
22
  withProvider: <T, P_1 extends {
21
23
  className?: string | undefined;
22
- }>(Component: ElementType, slot: Slot<R>) => ForwardRefExoticComponent<PropsWithoutRef<P_1> & RefAttributes<T>>;
24
+ } & WithCss>(Component: ElementType, slot: Slot<R>) => ForwardRefExoticComponent<PropsWithoutRef<P_1> & RefAttributes<T>>;
23
25
  withContext: <T_1, P_2 extends {
24
26
  className?: string | undefined;
25
- }>(Component: ElementType, slot: Slot<R>) => ForwardRefExoticComponent<PropsWithoutRef<P_2> & RefAttributes<T_1>>;
27
+ } & WithCss>(Component: ElementType, slot: Slot<R>) => ForwardRefExoticComponent<PropsWithoutRef<P_2> & RefAttributes<T_1>>;
26
28
  };
27
29
  export {};
@@ -24,7 +24,7 @@ const createStyleContext = recipe => {
24
24
  const withRootProvider = Component => {
25
25
  const StyledComponent = props => {
26
26
  const [variantProps, otherProps] = recipe.splitVariantProps(props);
27
- const slotStyles = recipe(variantProps);
27
+ const slotStyles = recipe.raw(variantProps);
28
28
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(StyleContext.Provider, {
29
29
  value: slotStyles,
30
30
  children: /*#__PURE__*/(0, _jsxRuntime.jsx)(Component, {
@@ -36,27 +36,35 @@ const createStyleContext = recipe => {
36
36
  };
37
37
  const withProvider = (Component, slot) => {
38
38
  const StyledComponent = (0, _jsx2.styled)(Component);
39
- return /*#__PURE__*/(0, _react.forwardRef)((props, ref) => {
39
+ return /*#__PURE__*/(0, _react.forwardRef)((_ref, ref) => {
40
+ let {
41
+ css: cssProp,
42
+ ...props
43
+ } = _ref;
40
44
  const [variantProps, otherProps] = recipe.splitVariantProps(props);
41
- const slotStyles = recipe(variantProps);
45
+ const slotStyles = recipe.raw(variantProps);
42
46
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(StyleContext.Provider, {
43
47
  value: slotStyles,
44
48
  children: /*#__PURE__*/(0, _jsxRuntime.jsx)(StyledComponent, {
45
49
  ...otherProps,
46
50
  ref: ref,
47
- className: (0, _css.cx)(slotStyles === null || slotStyles === void 0 ? void 0 : slotStyles[slot], props.className)
51
+ css: _css.css.raw(slotStyles === null || slotStyles === void 0 ? void 0 : slotStyles[slot], cssProp)
48
52
  })
49
53
  });
50
54
  });
51
55
  };
52
56
  const withContext = (Component, slot) => {
53
57
  const StyledComponent = (0, _jsx2.styled)(Component);
54
- return /*#__PURE__*/(0, _react.forwardRef)((props, ref) => {
58
+ return /*#__PURE__*/(0, _react.forwardRef)((_ref2, ref) => {
59
+ let {
60
+ css: cssProp,
61
+ ...props
62
+ } = _ref2;
55
63
  const slotStyles = (0, _react.useContext)(StyleContext);
56
64
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(StyledComponent, {
57
65
  ...props,
58
66
  ref: ref,
59
- className: (0, _css.cx)(slotStyles === null || slotStyles === void 0 ? void 0 : slotStyles[slot], props.className)
67
+ css: _css.css.raw(slotStyles === null || slotStyles === void 0 ? void 0 : slotStyles[slot], cssProp)
60
68
  });
61
69
  });
62
70
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ndla/primitives",
3
- "version": "0.0.2",
3
+ "version": "0.0.4",
4
4
  "description": "Primitive components for NDLA",
5
5
  "license": "GPL-3.0",
6
6
  "main": "lib/index.js",
@@ -28,13 +28,13 @@
28
28
  "lib"
29
29
  ],
30
30
  "dependencies": {
31
- "@ark-ui/anatomy": "^3.3.1",
32
- "@ark-ui/react": "^3.3.0",
31
+ "@ark-ui/anatomy": "^3.4.0",
32
+ "@ark-ui/react": "^3.4.0",
33
33
  "@ndla/styled-system": "workspace:^",
34
34
  "@ndla/util": "^4.0.4"
35
35
  },
36
36
  "devDependencies": {
37
- "@ndla/preset-panda": "^0.0.1",
37
+ "@ndla/preset-panda": "^0.0.3",
38
38
  "@pandacss/dev": "^0.40.1"
39
39
  },
40
40
  "peerDependencies": {
@@ -44,5 +44,5 @@
44
44
  "publishConfig": {
45
45
  "access": "public"
46
46
  },
47
- "gitHead": "882ce593e9f688151f370555f8203ca467ac7e2b"
47
+ "gitHead": "8f0469f768ed618830dea90b36b92b0d4bb20639"
48
48
  }
package/es/FormControl.js DELETED
@@ -1,163 +0,0 @@
1
- /**
2
- * Copyright (c) 2024-present, NDLA.
3
- *
4
- * This source code is licensed under the GPLv3 license found in the
5
- * LICENSE file in the root directory of this source tree.
6
- *
7
- */
8
-
9
- import { createContext, useCallback, useContext, useState } from "react";
10
- import { styled } from "@ndla/styled-system/jsx";
11
- import { composeRefs } from "@ndla/util";
12
- import { jsx as _jsx } from "react/jsx-runtime";
13
- const StyledFormControl = styled("div", {
14
- base: {
15
- display: "flex",
16
- flexDirection: "column",
17
- gap: "3xsmall"
18
- }
19
- });
20
- const useFormControlProvider = _ref => {
21
- let {
22
- id: idProp,
23
- isRequired,
24
- isDisabled,
25
- isInvalid
26
- } = _ref;
27
- const id = "field-".concat(idProp);
28
- const labelId = "".concat(id, "-label");
29
- const errorTextId = "".concat(id, "-error-message");
30
- const helpTextId = "".concat(id, "-helper");
31
- const [hasErrorText, setHasErrorText] = useState(false);
32
- const [hasHelpText, setHasHelpText] = useState(false);
33
- const getHelpTextProps = useCallback(function () {
34
- let props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
35
- let forwardedRef = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
36
- return {
37
- id: helpTextId,
38
- ...props,
39
- ref: composeRefs(forwardedRef, node => {
40
- if (!node) return;
41
- setHasHelpText(true);
42
- })
43
- };
44
- }, [helpTextId]);
45
- const getLabelProps = useCallback(function () {
46
- let props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
47
- let forwardedRef = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
48
- return {
49
- ...props,
50
- ref: forwardedRef,
51
- "data-disabled": props !== null && props !== void 0 && props["data-disabled"] ? props["data-disabled"] : isDisabled,
52
- "data-invalid": props !== null && props !== void 0 && props["data-invalid"] ? props["data-invalid"] : isInvalid,
53
- id: props.id !== undefined ? props.id : labelId,
54
- htmlFor: props.htmlFor !== undefined ? props.htmlFor : id
55
- };
56
- }, [id, isDisabled, isInvalid, labelId]);
57
- const getErrorMessageProps = useCallback(function () {
58
- let props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
59
- let forwardedRef = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
60
- return {
61
- id: errorTextId,
62
- ...props,
63
- ref: composeRefs(forwardedRef, node => {
64
- if (!node) return;
65
- setHasErrorText(true);
66
- }),
67
- "aria-live": "polite"
68
- };
69
- }, [errorTextId]);
70
- return {
71
- isRequired: !!isRequired,
72
- isDisabled: !!isDisabled,
73
- isInvalid: !!isInvalid,
74
- hasErrorText,
75
- setHasErrorText,
76
- hasHelpText,
77
- setHasHelpText,
78
- id,
79
- labelId,
80
- errorTextId,
81
- helpTextId,
82
- getHelpTextProps,
83
- getErrorMessageProps,
84
- getLabelProps
85
- };
86
- };
87
- const FormControlContext = /*#__PURE__*/createContext(undefined);
88
-
89
- /**
90
- * Form control component inspired by chakra-ui. Allows for easy composition of form fields.
91
- */
92
- export const FormControl = _ref2 => {
93
- let {
94
- children,
95
- id,
96
- isDisabled,
97
- isInvalid,
98
- isRequired,
99
- ...rest
100
- } = _ref2;
101
- const context = useFormControlProvider({
102
- id,
103
- isDisabled,
104
- isInvalid,
105
- isRequired
106
- });
107
- return /*#__PURE__*/_jsx(FormControlContext.Provider, {
108
- value: context,
109
- children: /*#__PURE__*/_jsx(StyledFormControl, {
110
- ...rest,
111
- children: children
112
- })
113
- });
114
- };
115
- export const useFormControlContext = () => {
116
- const context = useContext(FormControlContext);
117
- return context;
118
- };
119
- export const useFormControlProps = _ref3 => {
120
- let {
121
- id,
122
- disabled,
123
- required,
124
- isDisabled,
125
- isInvalid,
126
- isRequired,
127
- ...rest
128
- } = _ref3;
129
- const field = useFormControlContext();
130
- const labelIds = [];
131
- if (field !== null && field !== void 0 && field.hasErrorText && field !== null && field !== void 0 && field.isInvalid) {
132
- labelIds.push(field.errorTextId);
133
- }
134
- if (rest["aria-describedby"]) {
135
- labelIds.push(rest["aria-describedby"]);
136
- }
137
- if (field !== null && field !== void 0 && field.hasHelpText) {
138
- labelIds.push(field.helpTextId);
139
- }
140
- return {
141
- ...rest,
142
- "aria-describedby": labelIds.join(" ") || undefined,
143
- id: id !== null && id !== void 0 ? id : field === null || field === void 0 ? void 0 : field.id,
144
- isDisabled: disabled !== null && disabled !== void 0 ? disabled : field === null || field === void 0 ? void 0 : field.isDisabled,
145
- isRequired: required !== null && required !== void 0 ? required : field === null || field === void 0 ? void 0 : field.isRequired,
146
- isInvalid: isInvalid !== null && isInvalid !== void 0 ? isInvalid : field === null || field === void 0 ? void 0 : field.isInvalid
147
- };
148
- };
149
- export const useFormControl = props => {
150
- const {
151
- isDisabled,
152
- isInvalid,
153
- isRequired,
154
- ...rest
155
- } = useFormControlProps(props);
156
- return {
157
- ...rest,
158
- disabled: isDisabled,
159
- required: isRequired,
160
- "aria-invalid": isInvalid ? true : undefined,
161
- "aria-required": isRequired ? true : undefined
162
- };
163
- };
@@ -1,65 +0,0 @@
1
- /**
2
- * Copyright (c) 2024-present, NDLA.
3
- *
4
- * This source code is licensed under the GPLv3 license found in the
5
- * LICENSE file in the root directory of this source tree.
6
- *
7
- */
8
- import { ComponentPropsWithRef, ElementType, HTMLAttributes, Ref, RefObject } from "react";
9
- type Merge<T, P> = P & Omit<T, keyof P>;
10
- type PropGetter<T extends ElementType = ElementType, P = {}> = (props?: Merge<ComponentPropsWithRef<T>, P>, ref?: Ref<any> | RefObject<any>) => Merge<ComponentPropsWithRef<T>, P>;
11
- export interface FormControlOptions {
12
- isDisabled?: boolean;
13
- isInvalid?: boolean;
14
- isRequired?: boolean;
15
- }
16
- export interface FormControlProps extends FormControlOptions {
17
- id: string;
18
- }
19
- /**
20
- * Form control component inspired by chakra-ui. Allows for easy composition of form fields.
21
- */
22
- export declare const FormControl: ({ children, id, isDisabled, isInvalid, isRequired, ...rest }: HTMLAttributes<HTMLDivElement> & FormControlProps) => import("@emotion/react/jsx-runtime").JSX.Element;
23
- export declare const useFormControlContext: () => {
24
- isRequired: boolean;
25
- isDisabled: boolean;
26
- isInvalid: boolean;
27
- hasErrorText: boolean;
28
- setHasErrorText: import("react").Dispatch<import("react").SetStateAction<boolean>>;
29
- hasHelpText: boolean;
30
- setHasHelpText: import("react").Dispatch<import("react").SetStateAction<boolean>>;
31
- id: string;
32
- labelId: string;
33
- errorTextId: string;
34
- helpTextId: string;
35
- getHelpTextProps: PropGetter<ElementType, {}>;
36
- getErrorMessageProps: PropGetter<ElementType, {}>;
37
- getLabelProps: PropGetter<ElementType, {}>;
38
- } | undefined;
39
- export interface UseFormControlProps extends FormControlOptions {
40
- id?: string;
41
- disabled?: boolean;
42
- readOnly?: boolean;
43
- required?: boolean;
44
- "aria-describedby"?: string;
45
- }
46
- export declare const useFormControlProps: <T extends UseFormControlProps>({ id, disabled, required, isDisabled, isInvalid, isRequired, ...rest }: T) => Omit<T, "id" | "disabled" | "required" | "isRequired" | "isDisabled" | "isInvalid"> & {
47
- "aria-describedby": string | undefined;
48
- id: string | undefined;
49
- isDisabled: boolean | undefined;
50
- isRequired: boolean | undefined;
51
- isInvalid: boolean | undefined;
52
- };
53
- export declare const useFormControl: <T extends UseFormControlProps>(props: T) => Omit<Omit<T, "id" | "disabled" | "required" | "isRequired" | "isDisabled" | "isInvalid"> & {
54
- "aria-describedby": string | undefined;
55
- id: string | undefined;
56
- isDisabled: boolean | undefined;
57
- isRequired: boolean | undefined;
58
- isInvalid: boolean | undefined;
59
- }, "isRequired" | "isDisabled" | "isInvalid"> & {
60
- disabled: boolean | undefined;
61
- required: boolean | undefined;
62
- "aria-invalid": boolean | undefined;
63
- "aria-required": boolean | undefined;
64
- };
65
- export {};