UNPKG

@atlaskit/form

Version:

A form allows people to input information.

153 lines (147 loc) • 4.73 kB
/* message.tsx generated by @compiled/babel-plugin v3.0.2 */ import "./message.compiled.css"; import * as React from 'react'; import { ax, ix } from "@compiled/react/runtime"; import { useContext } from 'react'; import ErrorIcon from '@atlaskit/icon/core/status-error'; import SuccessIcon from '@atlaskit/icon/core/status-success'; import { useMotion } from '@atlaskit/motion/entering/use-motion'; import { fg } from '@atlaskit/platform-feature-flags/fg'; import { FormContext } from './form-context'; import { MessageWrapperContext } from './message-context'; import MessageTrack from './message-track'; /** * 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. */ const messageStyles = null; const messageAppearanceStyles = { default: "_syaz1rpy", error: "_syaz1tmw", valid: "_syazgsak" }; const iconWrapperStyles = { root: "_1e0c1txw _4t3i7vkz _4cvr1h6o" }; const IconWrapper = ({ children }) => /*#__PURE__*/React.createElement("span", { className: ax([iconWrapperStyles.root]) }, children); const getMessageIcon = (appearance, iconLabel) => { if (appearance === 'error') { return /*#__PURE__*/React.createElement(ErrorIcon, { color: "currentColor", label: iconLabel !== null && iconLabel !== void 0 ? iconLabel : 'error', size: "small" }); } if (appearance === 'valid') { return /*#__PURE__*/React.createElement(SuccessIcon, { color: "currentColor", label: iconLabel !== null && iconLabel !== void 0 ? iconLabel : 'success', size: "small" }); } }; const getContextIconLabel = (appearance, errorMessageLabel, validMessageLabel) => { if (appearance === 'error') { return errorMessageLabel; } if (appearance === 'valid') { return validMessageLabel; } }; const MessageRow = ({ children, appearance = 'default', fieldId, iconLabel, isExiting = false, testId }) => { const { errorMessageLabel, validMessageLabel } = useContext(FormContext); const contextIconLabel = getContextIconLabel(appearance, errorMessageLabel, validMessageLabel); const icon = getMessageIcon(appearance, iconLabel !== null && iconLabel !== void 0 ? iconLabel : contextIconLabel); const { isWrapper } = useContext(MessageWrapperContext); /** * 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. */ const 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: ax(["_zulp12x7 _11c8wadc _1e0c1txw _1bah1q9y _1pfh1b66", messageAppearanceStyles[appearance]]) }, icon && /*#__PURE__*/React.createElement(IconWrapper, null, icon), content); }; const MotionMessage = props => { const isErrorMessage = props.appearance === 'error'; const { hasMotionBoundary, transition } = useContext(MessageWrapperContext); const shouldAnimate = isErrorMessage || hasMotionBoundary; const { ref, state } = useMotion({ initialState: shouldAnimate && transition !== 'instant' ? undefined : 'visible' }); const 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, { state: state, transition: transition, motionRef: ref, testId: props.testId && `${props.testId}-motion` }, row); }; /** * __Message__ * * A message component for displaying messages in a form. */ const Message = props => { if (!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); }; export default Message;