UNPKG

@atlaskit/form

Version:

A form allows people to input information.

159 lines (153 loc) • 6.5 kB
/* message.tsx generated by @compiled/babel-plugin v3.0.2 */ "use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); var _typeof = require("@babel/runtime/helpers/typeof"); Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; require("./message.compiled.css"); var _react = _interopRequireWildcard(require("react")); var React = _react; var _runtime = require("@compiled/react/runtime"); var _statusError = _interopRequireDefault(require("@atlaskit/icon/core/status-error")); var _statusSuccess = _interopRequireDefault(require("@atlaskit/icon/core/status-success")); var _useMotion2 = require("@atlaskit/motion/entering/use-motion"); var _fg = require("@atlaskit/platform-feature-flags/fg"); var _formContext = require("./form-context"); var _messageContext = require("./message-context"); var _messageTrack = _interopRequireDefault(require("./message-track")); 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. */ /** * Public API of message components that render a status icon. */ var messageStyles = null; var messageAppearanceStyles = { default: "_syaz1rpy", error: "_syaz1tmw", valid: "_syazgsak" }; 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 getMessageIcon = function getMessageIcon(appearance, iconLabel) { if (appearance === 'error') { return /*#__PURE__*/React.createElement(_statusError.default, { color: "currentColor", label: iconLabel !== null && iconLabel !== void 0 ? iconLabel : 'error', size: "small" }); } if (appearance === 'valid') { return /*#__PURE__*/React.createElement(_statusSuccess.default, { color: "currentColor", label: iconLabel !== null && iconLabel !== void 0 ? iconLabel : 'success', size: "small" }); } }; var getContextIconLabel = function getContextIconLabel(appearance, errorMessageLabel, validMessageLabel) { if (appearance === 'error') { return errorMessageLabel; } if (appearance === 'valid') { return validMessageLabel; } }; var MessageRow = function MessageRow(_ref2) { var children = _ref2.children, _ref2$appearance = _ref2.appearance, appearance = _ref2$appearance === void 0 ? 'default' : _ref2$appearance, fieldId = _ref2.fieldId, iconLabel = _ref2.iconLabel, _ref2$isExiting = _ref2.isExiting, isExiting = _ref2$isExiting === void 0 ? false : _ref2$isExiting, testId = _ref2.testId; var _useContext = (0, _react.useContext)(_formContext.FormContext), errorMessageLabel = _useContext.errorMessageLabel, validMessageLabel = _useContext.validMessageLabel; var contextIconLabel = getContextIconLabel(appearance, errorMessageLabel, validMessageLabel); var icon = getMessageIcon(appearance, iconLabel !== null && iconLabel !== void 0 ? iconLabel : contextIconLabel); var _useContext2 = (0, _react.useContext)(_messageContext.MessageWrapperContext), isWrapper = _useContext2.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: isExiting ? undefined : fieldId // Let the wrapper own announcements from the first render when present. , "aria-live": isWrapper ? undefined : 'polite', "aria-hidden": isExiting ? true : undefined, className: (0, _runtime.ax)(["_zulp12x7 _11c8wadc _1e0c1txw _1bah1q9y _1pfh1b66", messageAppearanceStyles[appearance]]) }, icon && /*#__PURE__*/React.createElement(IconWrapper, null, icon), content); }; var MotionMessage = function MotionMessage(props) { var isErrorMessage = props.appearance === 'error'; var _useContext3 = (0, _react.useContext)(_messageContext.MessageWrapperContext), hasMotionBoundary = _useContext3.hasMotionBoundary, transition = _useContext3.transition; var shouldAnimate = isErrorMessage || hasMotionBoundary; var _useMotion = (0, _useMotion2.useMotion)({ initialState: shouldAnimate && transition !== 'instant' ? undefined : 'visible' }), ref = _useMotion.ref, state = _useMotion.state; var row = /*#__PURE__*/React.createElement(MessageRow, { appearance: props.appearance, fieldId: props.fieldId, iconLabel: props.iconLabel, isExiting: state === 'exiting', testId: props.testId }, props.children); if (!shouldAnimate) { return row; } return /*#__PURE__*/React.createElement(_messageTrack.default, { state: state, transition: transition, motionRef: ref, testId: props.testId && "".concat(props.testId, "-motion") }, row); }; /** * __Message__ * * A message component for displaying messages in a form. */ var Message = function Message(props) { if (!(0, _fg.fg)('platform-dst-motion-uplift-input')) { return /*#__PURE__*/React.createElement(MessageRow, { appearance: props.appearance, fieldId: props.fieldId, iconLabel: props.iconLabel, testId: props.testId }, props.children); } return /*#__PURE__*/React.createElement(MotionMessage, { appearance: props.appearance, fieldId: props.fieldId, iconLabel: props.iconLabel, testId: props.testId }, props.children); }; var _default = exports.default = Message;