@ndla/primitives 0.0.3 → 0.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/panda.buildinfo.json +51 -29
- package/dist/styles.css +176 -90
- package/es/BlockQuote.js +2 -2
- package/es/Button.js +2 -1
- package/es/Checkbox.js +160 -51
- package/es/Field.js +17 -0
- package/es/FieldErrorMessage.js +6 -12
- package/es/FieldHelper.js +6 -11
- package/es/Input.js +13 -14
- package/es/Label.js +6 -31
- package/es/RadioGroup.js +1 -9
- package/es/Switch.js +1 -12
- package/es/Tabs.js +2 -3
- package/lib/BlockQuote.d.ts +1 -0
- package/lib/BlockQuote.js +2 -2
- package/lib/Button.js +2 -1
- package/lib/Checkbox.d.ts +162 -2
- package/lib/Checkbox.js +165 -56
- package/lib/Field.d.ts +9 -0
- package/lib/Field.js +23 -0
- package/lib/FieldErrorMessage.js +5 -11
- package/lib/FieldHelper.js +5 -10
- package/lib/Input.d.ts +2 -2
- package/lib/Input.js +13 -14
- package/lib/Label.d.ts +1 -4
- package/lib/Label.js +6 -31
- package/lib/RadioGroup.js +1 -9
- package/lib/Switch.js +6 -17
- package/lib/Tabs.js +2 -3
- package/package.json +3 -3
- package/es/FormControl.js +0 -164
- package/lib/FormControl.d.ts +0 -67
- package/lib/FormControl.js +0 -174
package/lib/Label.js
CHANGED
|
@@ -3,12 +3,11 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports.Legend = exports.Label = exports.
|
|
6
|
+
exports.Legend = exports.Label = exports.Fieldset = exports.FieldLabel = void 0;
|
|
7
7
|
var _react = require("react");
|
|
8
8
|
var _react2 = require("@ark-ui/react");
|
|
9
9
|
var _css = require("@ndla/styled-system/css");
|
|
10
10
|
var _jsx2 = require("@ndla/styled-system/jsx");
|
|
11
|
-
var _FormControl = require("./FormControl");
|
|
12
11
|
var _jsxRuntime = require("react/jsx-runtime");
|
|
13
12
|
/**
|
|
14
13
|
* Copyright (c) 2024-present, NDLA.
|
|
@@ -45,24 +44,6 @@ const Legend = exports.Legend = /*#__PURE__*/(0, _react.forwardRef)((_ref, ref)
|
|
|
45
44
|
ref: ref
|
|
46
45
|
});
|
|
47
46
|
});
|
|
48
|
-
const FormLegend = exports.FormLegend = /*#__PURE__*/(0, _react.forwardRef)((props, ref) => {
|
|
49
|
-
var _control$getLabelProp;
|
|
50
|
-
const control = (0, _FormControl.useFormControlContext)();
|
|
51
|
-
// Legend does not use htmlFor (for), so we remove it.
|
|
52
|
-
const {
|
|
53
|
-
id: _,
|
|
54
|
-
htmlFor: __,
|
|
55
|
-
...fieldProps
|
|
56
|
-
} = (_control$getLabelProp = control === null || control === void 0 ? void 0 : control.getLabelProps(props, ref)) !== null && _control$getLabelProp !== void 0 ? _control$getLabelProp : {
|
|
57
|
-
ref,
|
|
58
|
-
...props
|
|
59
|
-
};
|
|
60
|
-
return /*#__PURE__*/(0, _jsxRuntime.jsx)(Legend, {
|
|
61
|
-
...props,
|
|
62
|
-
...fieldProps,
|
|
63
|
-
ref: ref
|
|
64
|
-
});
|
|
65
|
-
});
|
|
66
47
|
const StyledLabel = (0, _jsx2.styled)(_react2.ark.label, {
|
|
67
48
|
base: {
|
|
68
49
|
display: "inline-block",
|
|
@@ -89,19 +70,13 @@ const Label = exports.Label = /*#__PURE__*/(0, _react.forwardRef)((_ref2, ref) =
|
|
|
89
70
|
ref: ref
|
|
90
71
|
});
|
|
91
72
|
});
|
|
92
|
-
const
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
const fieldProps = (_control$getLabelProp2 = control === null || control === void 0 ? void 0 : control.getLabelProps(props, ref)) !== null && _control$getLabelProp2 !== void 0 ? _control$getLabelProp2 : {
|
|
96
|
-
ref,
|
|
97
|
-
...props
|
|
98
|
-
};
|
|
99
|
-
return /*#__PURE__*/(0, _jsxRuntime.jsx)(Label, {
|
|
73
|
+
const FieldLabel = exports.FieldLabel = /*#__PURE__*/(0, _react.forwardRef)((props, ref) => /*#__PURE__*/(0, _jsxRuntime.jsx)(_react2.Field.Label, {
|
|
74
|
+
asChild: true,
|
|
75
|
+
children: /*#__PURE__*/(0, _jsxRuntime.jsx)(Label, {
|
|
100
76
|
...props,
|
|
101
|
-
...fieldProps,
|
|
102
77
|
ref: ref
|
|
103
|
-
})
|
|
104
|
-
});
|
|
78
|
+
})
|
|
79
|
+
}));
|
|
105
80
|
const Fieldset = exports.Fieldset = (0, _jsx2.styled)("fieldset", {
|
|
106
81
|
base: {
|
|
107
82
|
border: "none",
|
package/lib/RadioGroup.js
CHANGED
|
@@ -9,7 +9,6 @@ var _anatomy = require("@ark-ui/anatomy");
|
|
|
9
9
|
var _react2 = require("@ark-ui/react");
|
|
10
10
|
var _css = require("@ndla/styled-system/css");
|
|
11
11
|
var _createStyleContext = require("./createStyleContext");
|
|
12
|
-
var _FormControl = require("./FormControl");
|
|
13
12
|
var _Label = require("./Label");
|
|
14
13
|
var _Text = require("./Text");
|
|
15
14
|
var _jsxRuntime = require("react/jsx-runtime");
|
|
@@ -98,14 +97,7 @@ const {
|
|
|
98
97
|
withProvider,
|
|
99
98
|
withContext
|
|
100
99
|
} = (0, _createStyleContext.createStyleContext)(radioGroupRecipe);
|
|
101
|
-
const
|
|
102
|
-
const RadioGroupRoot = exports.RadioGroupRoot = /*#__PURE__*/(0, _react.forwardRef)((props, ref) => {
|
|
103
|
-
const field = (0, _FormControl.useFormControl)(props);
|
|
104
|
-
return /*#__PURE__*/(0, _jsxRuntime.jsx)(InternalRadioGroupRoot, {
|
|
105
|
-
...field,
|
|
106
|
-
ref: ref
|
|
107
|
-
});
|
|
108
|
-
});
|
|
100
|
+
const RadioGroupRoot = exports.RadioGroupRoot = withProvider(_react2.RadioGroup.Root, "root");
|
|
109
101
|
const RadioGroupIndicator = exports.RadioGroupIndicator = withContext(_react2.RadioGroup.Indicator, "indicator");
|
|
110
102
|
const RadioGroupItemControl = exports.RadioGroupItemControl = withContext(_react2.RadioGroup.ItemControl, "itemControl");
|
|
111
103
|
const RadioGroupItem = exports.RadioGroupItem = withContext(_react2.RadioGroup.Item, "item");
|
package/lib/Switch.js
CHANGED
|
@@ -4,12 +4,10 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.SwitchThumb = exports.SwitchRoot = exports.SwitchLabel = exports.SwitchHiddenInput = exports.SwitchControl = void 0;
|
|
7
|
-
var _react = require("react");
|
|
8
7
|
var _anatomy = require("@ark-ui/anatomy");
|
|
9
|
-
var
|
|
8
|
+
var _react = require("@ark-ui/react");
|
|
10
9
|
var _css = require("@ndla/styled-system/css");
|
|
11
10
|
var _createStyleContext = require("./createStyleContext");
|
|
12
|
-
var _FormControl = require("./FormControl");
|
|
13
11
|
var _Text = require("./Text");
|
|
14
12
|
var _jsxRuntime = require("react/jsx-runtime");
|
|
15
13
|
/**
|
|
@@ -106,19 +104,10 @@ const {
|
|
|
106
104
|
withProvider,
|
|
107
105
|
withContext
|
|
108
106
|
} = (0, _createStyleContext.createStyleContext)(switchRecipe);
|
|
109
|
-
const
|
|
110
|
-
const
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
return /*#__PURE__*/(0, _jsxRuntime.jsx)(InternalSwitchRoot, {
|
|
114
|
-
invalid: (_field$invalid = field.invalid) !== null && _field$invalid !== void 0 ? _field$invalid : !!field["aria-invalid"],
|
|
115
|
-
...field,
|
|
116
|
-
ref: ref
|
|
117
|
-
});
|
|
118
|
-
});
|
|
119
|
-
const SwitchControl = exports.SwitchControl = withContext(_react2.Switch.Control, "control");
|
|
120
|
-
const SwitchThumb = exports.SwitchThumb = withContext(_react2.Switch.Thumb, "thumb");
|
|
121
|
-
const InternalSwitchLabel = withContext(_react2.Switch.Label, "label");
|
|
107
|
+
const SwitchRoot = exports.SwitchRoot = withProvider(_react.Switch.Root, "root");
|
|
108
|
+
const SwitchControl = exports.SwitchControl = withContext(_react.Switch.Control, "control");
|
|
109
|
+
const SwitchThumb = exports.SwitchThumb = withContext(_react.Switch.Thumb, "thumb");
|
|
110
|
+
const InternalSwitchLabel = withContext(_react.Switch.Label, "label");
|
|
122
111
|
const SwitchLabel = _ref => {
|
|
123
112
|
let {
|
|
124
113
|
textStyle = "label.medium",
|
|
@@ -138,4 +127,4 @@ const SwitchLabel = _ref => {
|
|
|
138
127
|
});
|
|
139
128
|
};
|
|
140
129
|
exports.SwitchLabel = SwitchLabel;
|
|
141
|
-
const SwitchHiddenInput = exports.SwitchHiddenInput =
|
|
130
|
+
const SwitchHiddenInput = exports.SwitchHiddenInput = _react.Switch.HiddenInput;
|
package/lib/Tabs.js
CHANGED
|
@@ -4,6 +4,7 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.TabsTrigger = exports.TabsRoot = exports.TabsList = exports.TabsIndicator = exports.TabsContent = void 0;
|
|
7
|
+
var _anatomy = require("@ark-ui/anatomy");
|
|
7
8
|
var _react = require("@ark-ui/react");
|
|
8
9
|
var _css = require("@ndla/styled-system/css");
|
|
9
10
|
var _createStyleContext = require("./createStyleContext");
|
|
@@ -17,9 +18,7 @@ var _jsxRuntime = require("react/jsx-runtime");
|
|
|
17
18
|
*/
|
|
18
19
|
|
|
19
20
|
const tabsRecipe = (0, _css.sva)({
|
|
20
|
-
|
|
21
|
-
// Come back to this once ark releases a new version.
|
|
22
|
-
slots: ["root", "list", "trigger", "content", "indicator"],
|
|
21
|
+
slots: _anatomy.tabsAnatomy.keys(),
|
|
23
22
|
base: {
|
|
24
23
|
root: {
|
|
25
24
|
position: "relative",
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ndla/primitives",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.5",
|
|
4
4
|
"description": "Primitive components for NDLA",
|
|
5
5
|
"license": "GPL-3.0",
|
|
6
6
|
"main": "lib/index.js",
|
|
@@ -34,7 +34,7 @@
|
|
|
34
34
|
"@ndla/util": "^4.0.4"
|
|
35
35
|
},
|
|
36
36
|
"devDependencies": {
|
|
37
|
-
"@ndla/preset-panda": "^0.0.
|
|
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": "
|
|
47
|
+
"gitHead": "ca017a610c49c3f08691b7963b7a82284c3baa2b"
|
|
48
48
|
}
|
package/es/FormControl.js
DELETED
|
@@ -1,164 +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 { ark } from "@ark-ui/react";
|
|
11
|
-
import { styled } from "@ndla/styled-system/jsx";
|
|
12
|
-
import { composeRefs } from "@ndla/util";
|
|
13
|
-
import { jsx as _jsx } from "react/jsx-runtime";
|
|
14
|
-
const StyledFormControl = styled(ark.div, {
|
|
15
|
-
base: {
|
|
16
|
-
display: "flex",
|
|
17
|
-
flexDirection: "column",
|
|
18
|
-
gap: "3xsmall"
|
|
19
|
-
}
|
|
20
|
-
});
|
|
21
|
-
const useFormControlProvider = _ref => {
|
|
22
|
-
let {
|
|
23
|
-
id: idProp,
|
|
24
|
-
isRequired,
|
|
25
|
-
isDisabled,
|
|
26
|
-
isInvalid
|
|
27
|
-
} = _ref;
|
|
28
|
-
const id = "field-".concat(idProp);
|
|
29
|
-
const labelId = "".concat(id, "-label");
|
|
30
|
-
const errorTextId = "".concat(id, "-error-message");
|
|
31
|
-
const helpTextId = "".concat(id, "-helper");
|
|
32
|
-
const [hasErrorText, setHasErrorText] = useState(false);
|
|
33
|
-
const [hasHelpText, setHasHelpText] = useState(false);
|
|
34
|
-
const getHelpTextProps = useCallback(function () {
|
|
35
|
-
let props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
36
|
-
let forwardedRef = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
|
|
37
|
-
return {
|
|
38
|
-
id: helpTextId,
|
|
39
|
-
...props,
|
|
40
|
-
ref: composeRefs(forwardedRef, node => {
|
|
41
|
-
if (!node) return;
|
|
42
|
-
setHasHelpText(true);
|
|
43
|
-
})
|
|
44
|
-
};
|
|
45
|
-
}, [helpTextId]);
|
|
46
|
-
const getLabelProps = useCallback(function () {
|
|
47
|
-
let props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
48
|
-
let forwardedRef = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
|
|
49
|
-
return {
|
|
50
|
-
...props,
|
|
51
|
-
ref: forwardedRef,
|
|
52
|
-
"data-disabled": props !== null && props !== void 0 && props["data-disabled"] ? props["data-disabled"] : isDisabled,
|
|
53
|
-
"data-invalid": props !== null && props !== void 0 && props["data-invalid"] ? props["data-invalid"] : isInvalid,
|
|
54
|
-
id: props.id !== undefined ? props.id : labelId,
|
|
55
|
-
htmlFor: props.htmlFor !== undefined ? props.htmlFor : id
|
|
56
|
-
};
|
|
57
|
-
}, [id, isDisabled, isInvalid, labelId]);
|
|
58
|
-
const getErrorMessageProps = useCallback(function () {
|
|
59
|
-
let props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
60
|
-
let forwardedRef = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
|
|
61
|
-
return {
|
|
62
|
-
id: errorTextId,
|
|
63
|
-
...props,
|
|
64
|
-
ref: composeRefs(forwardedRef, node => {
|
|
65
|
-
if (!node) return;
|
|
66
|
-
setHasErrorText(true);
|
|
67
|
-
}),
|
|
68
|
-
"aria-live": "polite"
|
|
69
|
-
};
|
|
70
|
-
}, [errorTextId]);
|
|
71
|
-
return {
|
|
72
|
-
isRequired: !!isRequired,
|
|
73
|
-
isDisabled: !!isDisabled,
|
|
74
|
-
isInvalid: !!isInvalid,
|
|
75
|
-
hasErrorText,
|
|
76
|
-
setHasErrorText,
|
|
77
|
-
hasHelpText,
|
|
78
|
-
setHasHelpText,
|
|
79
|
-
id,
|
|
80
|
-
labelId,
|
|
81
|
-
errorTextId,
|
|
82
|
-
helpTextId,
|
|
83
|
-
getHelpTextProps,
|
|
84
|
-
getErrorMessageProps,
|
|
85
|
-
getLabelProps
|
|
86
|
-
};
|
|
87
|
-
};
|
|
88
|
-
const FormControlContext = /*#__PURE__*/createContext(undefined);
|
|
89
|
-
|
|
90
|
-
/**
|
|
91
|
-
* Form control component inspired by chakra-ui. Allows for easy composition of form fields.
|
|
92
|
-
*/
|
|
93
|
-
export const FormControl = _ref2 => {
|
|
94
|
-
let {
|
|
95
|
-
children,
|
|
96
|
-
id,
|
|
97
|
-
isDisabled,
|
|
98
|
-
isInvalid,
|
|
99
|
-
isRequired,
|
|
100
|
-
...rest
|
|
101
|
-
} = _ref2;
|
|
102
|
-
const context = useFormControlProvider({
|
|
103
|
-
id,
|
|
104
|
-
isDisabled,
|
|
105
|
-
isInvalid,
|
|
106
|
-
isRequired
|
|
107
|
-
});
|
|
108
|
-
return /*#__PURE__*/_jsx(FormControlContext.Provider, {
|
|
109
|
-
value: context,
|
|
110
|
-
children: /*#__PURE__*/_jsx(StyledFormControl, {
|
|
111
|
-
...rest,
|
|
112
|
-
children: children
|
|
113
|
-
})
|
|
114
|
-
});
|
|
115
|
-
};
|
|
116
|
-
export const useFormControlContext = () => {
|
|
117
|
-
const context = useContext(FormControlContext);
|
|
118
|
-
return context;
|
|
119
|
-
};
|
|
120
|
-
export const useFormControlProps = _ref3 => {
|
|
121
|
-
let {
|
|
122
|
-
id,
|
|
123
|
-
disabled,
|
|
124
|
-
required,
|
|
125
|
-
isDisabled,
|
|
126
|
-
isInvalid,
|
|
127
|
-
isRequired,
|
|
128
|
-
...rest
|
|
129
|
-
} = _ref3;
|
|
130
|
-
const field = useFormControlContext();
|
|
131
|
-
const labelIds = [];
|
|
132
|
-
if (field !== null && field !== void 0 && field.hasErrorText && field !== null && field !== void 0 && field.isInvalid) {
|
|
133
|
-
labelIds.push(field.errorTextId);
|
|
134
|
-
}
|
|
135
|
-
if (rest["aria-describedby"]) {
|
|
136
|
-
labelIds.push(rest["aria-describedby"]);
|
|
137
|
-
}
|
|
138
|
-
if (field !== null && field !== void 0 && field.hasHelpText) {
|
|
139
|
-
labelIds.push(field.helpTextId);
|
|
140
|
-
}
|
|
141
|
-
return {
|
|
142
|
-
...rest,
|
|
143
|
-
"aria-describedby": labelIds.join(" ") || undefined,
|
|
144
|
-
id: id !== null && id !== void 0 ? id : field === null || field === void 0 ? void 0 : field.id,
|
|
145
|
-
isDisabled: disabled !== null && disabled !== void 0 ? disabled : field === null || field === void 0 ? void 0 : field.isDisabled,
|
|
146
|
-
isRequired: required !== null && required !== void 0 ? required : field === null || field === void 0 ? void 0 : field.isRequired,
|
|
147
|
-
isInvalid: isInvalid !== null && isInvalid !== void 0 ? isInvalid : field === null || field === void 0 ? void 0 : field.isInvalid
|
|
148
|
-
};
|
|
149
|
-
};
|
|
150
|
-
export const useFormControl = props => {
|
|
151
|
-
const {
|
|
152
|
-
isDisabled,
|
|
153
|
-
isInvalid,
|
|
154
|
-
isRequired,
|
|
155
|
-
...rest
|
|
156
|
-
} = useFormControlProps(props);
|
|
157
|
-
return {
|
|
158
|
-
...rest,
|
|
159
|
-
disabled: isDisabled,
|
|
160
|
-
required: isRequired,
|
|
161
|
-
"aria-invalid": isInvalid ? true : undefined,
|
|
162
|
-
"aria-required": isRequired ? true : undefined
|
|
163
|
-
};
|
|
164
|
-
};
|
package/lib/FormControl.d.ts
DELETED
|
@@ -1,67 +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, Ref, RefObject } from "react";
|
|
9
|
-
import { HTMLArkProps } from "@ark-ui/react";
|
|
10
|
-
import { JsxStyleProps } from "@ndla/styled-system/types";
|
|
11
|
-
type Merge<T, P> = P & Omit<T, keyof P>;
|
|
12
|
-
type PropGetter<T extends ElementType = ElementType, P = {}> = (props?: Merge<ComponentPropsWithRef<T>, P>, ref?: Ref<any> | RefObject<any>) => Merge<ComponentPropsWithRef<T>, P>;
|
|
13
|
-
export interface FormControlOptions {
|
|
14
|
-
isDisabled?: boolean;
|
|
15
|
-
isInvalid?: boolean;
|
|
16
|
-
isRequired?: boolean;
|
|
17
|
-
}
|
|
18
|
-
export interface FormControlProps extends FormControlOptions {
|
|
19
|
-
id: string;
|
|
20
|
-
}
|
|
21
|
-
/**
|
|
22
|
-
* Form control component inspired by chakra-ui. Allows for easy composition of form fields.
|
|
23
|
-
*/
|
|
24
|
-
export declare const FormControl: ({ children, id, isDisabled, isInvalid, isRequired, ...rest }: HTMLArkProps<"div"> & JsxStyleProps & FormControlProps) => import("react/jsx-runtime").JSX.Element;
|
|
25
|
-
export declare const useFormControlContext: () => {
|
|
26
|
-
isRequired: boolean;
|
|
27
|
-
isDisabled: boolean;
|
|
28
|
-
isInvalid: boolean;
|
|
29
|
-
hasErrorText: boolean;
|
|
30
|
-
setHasErrorText: import("react").Dispatch<import("react").SetStateAction<boolean>>;
|
|
31
|
-
hasHelpText: boolean;
|
|
32
|
-
setHasHelpText: import("react").Dispatch<import("react").SetStateAction<boolean>>;
|
|
33
|
-
id: string;
|
|
34
|
-
labelId: string;
|
|
35
|
-
errorTextId: string;
|
|
36
|
-
helpTextId: string;
|
|
37
|
-
getHelpTextProps: PropGetter<ElementType, {}>;
|
|
38
|
-
getErrorMessageProps: PropGetter<ElementType, {}>;
|
|
39
|
-
getLabelProps: PropGetter<ElementType, {}>;
|
|
40
|
-
} | undefined;
|
|
41
|
-
export interface UseFormControlProps extends FormControlOptions {
|
|
42
|
-
id?: string;
|
|
43
|
-
disabled?: boolean;
|
|
44
|
-
readOnly?: boolean;
|
|
45
|
-
required?: boolean;
|
|
46
|
-
"aria-describedby"?: string;
|
|
47
|
-
}
|
|
48
|
-
export declare const useFormControlProps: <T extends UseFormControlProps>({ id, disabled, required, isDisabled, isInvalid, isRequired, ...rest }: T) => Omit<T, "id" | "disabled" | "required" | "isRequired" | "isDisabled" | "isInvalid"> & {
|
|
49
|
-
"aria-describedby": string | undefined;
|
|
50
|
-
id: string | undefined;
|
|
51
|
-
isDisabled: boolean | undefined;
|
|
52
|
-
isRequired: boolean | undefined;
|
|
53
|
-
isInvalid: boolean | undefined;
|
|
54
|
-
};
|
|
55
|
-
export declare const useFormControl: <T extends UseFormControlProps>(props: T) => Omit<Omit<T, "id" | "disabled" | "required" | "isRequired" | "isDisabled" | "isInvalid"> & {
|
|
56
|
-
"aria-describedby": string | undefined;
|
|
57
|
-
id: string | undefined;
|
|
58
|
-
isDisabled: boolean | undefined;
|
|
59
|
-
isRequired: boolean | undefined;
|
|
60
|
-
isInvalid: boolean | undefined;
|
|
61
|
-
}, "isRequired" | "isDisabled" | "isInvalid"> & {
|
|
62
|
-
disabled: boolean | undefined;
|
|
63
|
-
required: boolean | undefined;
|
|
64
|
-
"aria-invalid": boolean | undefined;
|
|
65
|
-
"aria-required": boolean | undefined;
|
|
66
|
-
};
|
|
67
|
-
export {};
|
package/lib/FormControl.js
DELETED
|
@@ -1,174 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.useFormControlProps = exports.useFormControlContext = exports.useFormControl = exports.FormControl = void 0;
|
|
7
|
-
var _react = require("react");
|
|
8
|
-
var _react2 = require("@ark-ui/react");
|
|
9
|
-
var _jsx2 = require("@ndla/styled-system/jsx");
|
|
10
|
-
var _util = require("@ndla/util");
|
|
11
|
-
var _jsxRuntime = require("react/jsx-runtime");
|
|
12
|
-
/**
|
|
13
|
-
* Copyright (c) 2024-present, NDLA.
|
|
14
|
-
*
|
|
15
|
-
* This source code is licensed under the GPLv3 license found in the
|
|
16
|
-
* LICENSE file in the root directory of this source tree.
|
|
17
|
-
*
|
|
18
|
-
*/
|
|
19
|
-
|
|
20
|
-
const StyledFormControl = (0, _jsx2.styled)(_react2.ark.div, {
|
|
21
|
-
base: {
|
|
22
|
-
display: "flex",
|
|
23
|
-
flexDirection: "column",
|
|
24
|
-
gap: "3xsmall"
|
|
25
|
-
}
|
|
26
|
-
});
|
|
27
|
-
const useFormControlProvider = _ref => {
|
|
28
|
-
let {
|
|
29
|
-
id: idProp,
|
|
30
|
-
isRequired,
|
|
31
|
-
isDisabled,
|
|
32
|
-
isInvalid
|
|
33
|
-
} = _ref;
|
|
34
|
-
const id = "field-".concat(idProp);
|
|
35
|
-
const labelId = "".concat(id, "-label");
|
|
36
|
-
const errorTextId = "".concat(id, "-error-message");
|
|
37
|
-
const helpTextId = "".concat(id, "-helper");
|
|
38
|
-
const [hasErrorText, setHasErrorText] = (0, _react.useState)(false);
|
|
39
|
-
const [hasHelpText, setHasHelpText] = (0, _react.useState)(false);
|
|
40
|
-
const getHelpTextProps = (0, _react.useCallback)(function () {
|
|
41
|
-
let props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
42
|
-
let forwardedRef = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
|
|
43
|
-
return {
|
|
44
|
-
id: helpTextId,
|
|
45
|
-
...props,
|
|
46
|
-
ref: (0, _util.composeRefs)(forwardedRef, node => {
|
|
47
|
-
if (!node) return;
|
|
48
|
-
setHasHelpText(true);
|
|
49
|
-
})
|
|
50
|
-
};
|
|
51
|
-
}, [helpTextId]);
|
|
52
|
-
const getLabelProps = (0, _react.useCallback)(function () {
|
|
53
|
-
let props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
54
|
-
let forwardedRef = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
|
|
55
|
-
return {
|
|
56
|
-
...props,
|
|
57
|
-
ref: forwardedRef,
|
|
58
|
-
"data-disabled": props !== null && props !== void 0 && props["data-disabled"] ? props["data-disabled"] : isDisabled,
|
|
59
|
-
"data-invalid": props !== null && props !== void 0 && props["data-invalid"] ? props["data-invalid"] : isInvalid,
|
|
60
|
-
id: props.id !== undefined ? props.id : labelId,
|
|
61
|
-
htmlFor: props.htmlFor !== undefined ? props.htmlFor : id
|
|
62
|
-
};
|
|
63
|
-
}, [id, isDisabled, isInvalid, labelId]);
|
|
64
|
-
const getErrorMessageProps = (0, _react.useCallback)(function () {
|
|
65
|
-
let props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
66
|
-
let forwardedRef = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
|
|
67
|
-
return {
|
|
68
|
-
id: errorTextId,
|
|
69
|
-
...props,
|
|
70
|
-
ref: (0, _util.composeRefs)(forwardedRef, node => {
|
|
71
|
-
if (!node) return;
|
|
72
|
-
setHasErrorText(true);
|
|
73
|
-
}),
|
|
74
|
-
"aria-live": "polite"
|
|
75
|
-
};
|
|
76
|
-
}, [errorTextId]);
|
|
77
|
-
return {
|
|
78
|
-
isRequired: !!isRequired,
|
|
79
|
-
isDisabled: !!isDisabled,
|
|
80
|
-
isInvalid: !!isInvalid,
|
|
81
|
-
hasErrorText,
|
|
82
|
-
setHasErrorText,
|
|
83
|
-
hasHelpText,
|
|
84
|
-
setHasHelpText,
|
|
85
|
-
id,
|
|
86
|
-
labelId,
|
|
87
|
-
errorTextId,
|
|
88
|
-
helpTextId,
|
|
89
|
-
getHelpTextProps,
|
|
90
|
-
getErrorMessageProps,
|
|
91
|
-
getLabelProps
|
|
92
|
-
};
|
|
93
|
-
};
|
|
94
|
-
const FormControlContext = /*#__PURE__*/(0, _react.createContext)(undefined);
|
|
95
|
-
|
|
96
|
-
/**
|
|
97
|
-
* Form control component inspired by chakra-ui. Allows for easy composition of form fields.
|
|
98
|
-
*/
|
|
99
|
-
const FormControl = _ref2 => {
|
|
100
|
-
let {
|
|
101
|
-
children,
|
|
102
|
-
id,
|
|
103
|
-
isDisabled,
|
|
104
|
-
isInvalid,
|
|
105
|
-
isRequired,
|
|
106
|
-
...rest
|
|
107
|
-
} = _ref2;
|
|
108
|
-
const context = useFormControlProvider({
|
|
109
|
-
id,
|
|
110
|
-
isDisabled,
|
|
111
|
-
isInvalid,
|
|
112
|
-
isRequired
|
|
113
|
-
});
|
|
114
|
-
return /*#__PURE__*/(0, _jsxRuntime.jsx)(FormControlContext.Provider, {
|
|
115
|
-
value: context,
|
|
116
|
-
children: /*#__PURE__*/(0, _jsxRuntime.jsx)(StyledFormControl, {
|
|
117
|
-
...rest,
|
|
118
|
-
children: children
|
|
119
|
-
})
|
|
120
|
-
});
|
|
121
|
-
};
|
|
122
|
-
exports.FormControl = FormControl;
|
|
123
|
-
const useFormControlContext = () => {
|
|
124
|
-
const context = (0, _react.useContext)(FormControlContext);
|
|
125
|
-
return context;
|
|
126
|
-
};
|
|
127
|
-
exports.useFormControlContext = useFormControlContext;
|
|
128
|
-
const useFormControlProps = _ref3 => {
|
|
129
|
-
let {
|
|
130
|
-
id,
|
|
131
|
-
disabled,
|
|
132
|
-
required,
|
|
133
|
-
isDisabled,
|
|
134
|
-
isInvalid,
|
|
135
|
-
isRequired,
|
|
136
|
-
...rest
|
|
137
|
-
} = _ref3;
|
|
138
|
-
const field = useFormControlContext();
|
|
139
|
-
const labelIds = [];
|
|
140
|
-
if (field !== null && field !== void 0 && field.hasErrorText && field !== null && field !== void 0 && field.isInvalid) {
|
|
141
|
-
labelIds.push(field.errorTextId);
|
|
142
|
-
}
|
|
143
|
-
if (rest["aria-describedby"]) {
|
|
144
|
-
labelIds.push(rest["aria-describedby"]);
|
|
145
|
-
}
|
|
146
|
-
if (field !== null && field !== void 0 && field.hasHelpText) {
|
|
147
|
-
labelIds.push(field.helpTextId);
|
|
148
|
-
}
|
|
149
|
-
return {
|
|
150
|
-
...rest,
|
|
151
|
-
"aria-describedby": labelIds.join(" ") || undefined,
|
|
152
|
-
id: id !== null && id !== void 0 ? id : field === null || field === void 0 ? void 0 : field.id,
|
|
153
|
-
isDisabled: disabled !== null && disabled !== void 0 ? disabled : field === null || field === void 0 ? void 0 : field.isDisabled,
|
|
154
|
-
isRequired: required !== null && required !== void 0 ? required : field === null || field === void 0 ? void 0 : field.isRequired,
|
|
155
|
-
isInvalid: isInvalid !== null && isInvalid !== void 0 ? isInvalid : field === null || field === void 0 ? void 0 : field.isInvalid
|
|
156
|
-
};
|
|
157
|
-
};
|
|
158
|
-
exports.useFormControlProps = useFormControlProps;
|
|
159
|
-
const useFormControl = props => {
|
|
160
|
-
const {
|
|
161
|
-
isDisabled,
|
|
162
|
-
isInvalid,
|
|
163
|
-
isRequired,
|
|
164
|
-
...rest
|
|
165
|
-
} = useFormControlProps(props);
|
|
166
|
-
return {
|
|
167
|
-
...rest,
|
|
168
|
-
disabled: isDisabled,
|
|
169
|
-
required: isRequired,
|
|
170
|
-
"aria-invalid": isInvalid ? true : undefined,
|
|
171
|
-
"aria-required": isRequired ? true : undefined
|
|
172
|
-
};
|
|
173
|
-
};
|
|
174
|
-
exports.useFormControl = useFormControl;
|