UNPKG

@atlaskit/form

Version:

A form allows people to input information.

180 lines (171 loc) 6.82 kB
/* messages.tsx generated by @compiled/babel-plugin v0.39.1 */ "use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); var _typeof = require("@babel/runtime/helpers/typeof"); Object.defineProperty(exports, "__esModule", { value: true }); exports.ValidMessage = exports.MessageWrapper = exports.HelperMessage = exports.ErrorMessage = void 0; require("./messages.compiled.css"); var _react = _interopRequireWildcard(require("react")); var React = _react; var _runtime = require("@compiled/react/runtime"); var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray")); var _statusError = _interopRequireDefault(require("@atlaskit/icon/core/status-error")); var _statusSuccess = _interopRequireDefault(require("@atlaskit/icon/core/status-success")); var _fieldIdContext = require("./field-id-context"); function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); } /** * API for the internal `<Message />` component. This is not public API. */ /** * Public API of the various message components. */ var messageStyles = null; var messageAppearanceStyles = { default: "_syaz1wmz", error: "_syaze6sf", valid: "_syazy73q" }; var iconWrapperStyles = { root: "_1e0c1txw _4t3i7vkz _4cvr1h6o" }; var IconWrapper = function IconWrapper(_ref) { var children = _ref.children; return /*#__PURE__*/React.createElement("span", { className: (0, _runtime.ax)([iconWrapperStyles.root]) }, children); }; var messageIcons = { error: /*#__PURE__*/React.createElement(_statusError.default, { color: "currentColor", label: "error", size: "small" }), valid: /*#__PURE__*/React.createElement(_statusSuccess.default, { color: "currentColor", label: "success", size: "small" }) }; var Message = function Message(_ref2) { var children = _ref2.children, _ref2$appearance = _ref2.appearance, appearance = _ref2$appearance === void 0 ? 'default' : _ref2$appearance, fieldId = _ref2.fieldId, testId = _ref2.testId; var icon = messageIcons[appearance]; var messageRef = (0, _react.useRef)(null); var _useState = (0, _react.useState)(false), _useState2 = (0, _slicedToArray2.default)(_useState, 2), hasMessageWrapper = _useState2[0], setHasMessageWrapper = _useState2[1]; var _useContext = (0, _react.useContext)(MessageWrapperContext), isWrapper = _useContext.isWrapper; (0, _react.useEffect)(function () { if (messageRef.current) { setHasMessageWrapper(isWrapper); } }, [isWrapper]); /** * The wrapping span is necessary to preserve spaces between children. * Otherwise the flex layout of the message will remove any whitespace * between children. * * If the child is just a string, this is not required and we can use one * less DOM element. */ var content = typeof children === 'string' ? children : /*#__PURE__*/React.createElement("span", null, children); return /*#__PURE__*/React.createElement("div", { "data-testid": testId, id: fieldId, ref: messageRef // For backwards compatability, if there is a wrapper, aria-live is not needed , "aria-live": !hasMessageWrapper ? 'polite' : undefined, className: (0, _runtime.ax)(["_zulp12x7 _11c8wadc _1e0c1txw _1bah1q9y _1pfh1b66", messageAppearanceStyles[appearance]]) }, icon && /*#__PURE__*/React.createElement(IconWrapper, null, icon), content); }; /** * __Helper message__ * * A helper message tells the user what kind of input the field takes. For example, a helper message could be * 'Password should be more than 4 characters' * */ var HelperMessage = exports.HelperMessage = function HelperMessage(_ref3) { var children = _ref3.children, testId = _ref3.testId; return /*#__PURE__*/React.createElement(_fieldIdContext.FieldId.Consumer, null, function (fieldId) { return /*#__PURE__*/React.createElement(Message, { fieldId: fieldId ? "".concat(fieldId, "-helper") : undefined, testId: testId }, children); }); }; /** * __Error message__ * * An error message is used to tell a user that the field input is invalid. For example, an error message could be * 'Invalid username, needs to be more than 4 characters'. * */ var ErrorMessage = exports.ErrorMessage = function ErrorMessage(_ref4) { var children = _ref4.children, testId = _ref4.testId; return /*#__PURE__*/React.createElement(_fieldIdContext.FieldId.Consumer, null, function (fieldId) { return /*#__PURE__*/React.createElement(Message, { appearance: "error", fieldId: fieldId ? "".concat(fieldId, "-error") : undefined, testId: testId }, children); }); }; /** * __Valid message__ * * A valid message is used to tell a user that the field input is valid. For example, * a helper message could be 'Nice one, this username is available'. * */ var ValidMessage = exports.ValidMessage = function ValidMessage(_ref5) { var children = _ref5.children, testId = _ref5.testId; return /*#__PURE__*/React.createElement(_fieldIdContext.FieldId.Consumer, null, function (fieldId) { return /*#__PURE__*/React.createElement(Message, { appearance: "valid", fieldId: fieldId ? "".concat(fieldId, "-valid") : undefined, testId: testId }, children); }); }; /** * __Message wrapper context__ * * A message wrapper context allows the children to check * if it is contained within the MessageWrapper. */ var MessageWrapperContext = /*#__PURE__*/(0, _react.createContext)({ isWrapper: false }); /** * __Message wrapper __ * * A message wrapper is used to allow assistive technologies, like screen readers, to announce error or * valid messages. This must be loaded into the DOM before the * ErrorMessage, ValidMessage is loaded. Otherwise, assistive technologies * may not render the message. * */ var MessageWrapper = exports.MessageWrapper = function MessageWrapper(_ref6) { var children = _ref6.children; var contextValue = { isWrapper: true }; return /*#__PURE__*/React.createElement(MessageWrapperContext.Provider, { value: contextValue }, /*#__PURE__*/React.createElement("div", { "aria-live": "polite", "data-testid": "message-wrapper" }, children)); };