UNPKG

2.02 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
5exports.__esModule = true;
6exports["default"] = exports.useFormControlContext = exports.useFormControl = void 0;
7
8var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
9
10var _objectWithoutPropertiesLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutPropertiesLoose"));
11
12var _core = require("@emotion/core");
13
14var _react = require("react");
15
16var _Box = _interopRequireDefault(require("../Box"));
17
18/** @jsx jsx */
19var useFormControl = function useFormControl(props) {
20 var context = useFormControlContext();
21
22 if (!context) {
23 return props;
24 }
25
26 var keys = Object.keys(context);
27 return keys.reduce(function (acc, prop) {
28 /** Giving precedence to `props` over `context` */
29 acc[prop] = props[prop];
30
31 if (context) {
32 if (props[prop] == null) {
33 acc[prop] = context[prop];
34 }
35 }
36
37 return acc;
38 }, {});
39};
40
41exports.useFormControl = useFormControl;
42var FormControlContext = (0, _react.createContext)();
43
44var useFormControlContext = function useFormControlContext() {
45 return (0, _react.useContext)(FormControlContext);
46};
47
48exports.useFormControlContext = useFormControlContext;
49var FormControl = (0, _react.forwardRef)(function (_ref, ref) {
50 var isInvalid = _ref.isInvalid,
51 isRequired = _ref.isRequired,
52 isDisabled = _ref.isDisabled,
53 isReadOnly = _ref.isReadOnly,
54 rest = (0, _objectWithoutPropertiesLoose2["default"])(_ref, ["isInvalid", "isRequired", "isDisabled", "isReadOnly"]);
55 var context = {
56 isRequired: isRequired,
57 isDisabled: isDisabled,
58 isInvalid: isInvalid,
59 isReadOnly: isReadOnly
60 };
61 return (0, _core.jsx)(FormControlContext.Provider, {
62 value: context
63 }, (0, _core.jsx)(_Box["default"], (0, _extends2["default"])({
64 role: "group",
65 ref: ref
66 }, rest)));
67});
68FormControl.displayName = "FormControl";
69var _default = FormControl;
70exports["default"] = _default;
\No newline at end of file