UNPKG

@atlaskit/form

Version:

A form allows people to input information.

187 lines (178 loc) 6.22 kB
/* messages.tsx generated by @compiled/babel-plugin v3.0.2 */ import "./messages.compiled.css"; import * as React from 'react'; import { ax, ix } from "@compiled/react/runtime"; import { createContext, useContext, useEffect, useRef, useState } from 'react'; import ErrorIcon from '@atlaskit/icon/core/status-error'; import SuccessIcon from '@atlaskit/icon/core/status-success'; import { FieldId } from './field-id-context'; /** * API for the internal `<Message />` component. This is not public API. */ /** * Public API of the various message components. */ 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 messageIcons = { error: /*#__PURE__*/React.createElement(ErrorIcon, { color: "currentColor", label: "error", size: "small" }), valid: /*#__PURE__*/React.createElement(SuccessIcon, { color: "currentColor", label: "success", size: "small" }) }; const Message = ({ children, appearance = 'default', fieldId, testId }) => { const icon = messageIcons[appearance]; const messageRef = useRef(null); const [hasMessageWrapper, setHasMessageWrapper] = useState(false); const { isWrapper } = useContext(MessageWrapperContext); useEffect(() => { 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. */ const 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: 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' * */ export const HelperMessage = ({ children, testId }) => /*#__PURE__*/React.createElement(FieldId.Consumer, null, fieldId => /*#__PURE__*/React.createElement(Message, { fieldId: fieldId ? `${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'. * */ // eslint-disable-next-line @atlaskit/volt-strict-mode/no-multiple-exports // TODO: Fill in the component {description} and ensure links point to the correct {packageName} location. // Remove links that the component does not have (such as usage). If there are no links remove them all. /** * __Error message__ * * An error message {description}. * * - [Examples](https://atlassian.design/components/{packageName}/examples) * - [Code](https://atlassian.design/components/{packageName}/code) * - [Usage](https://atlassian.design/components/{packageName}/usage) */ // eslint-disable-next-line @atlaskit/volt-strict-mode/no-multiple-exports, @repo/internal/react/require-jsdoc export const ErrorMessage = ({ children, testId }) => /*#__PURE__*/React.createElement(FieldId.Consumer, null, fieldId => /*#__PURE__*/React.createElement(Message, { appearance: "error", fieldId: fieldId ? `${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'. * */ // eslint-disable-next-line @atlaskit/volt-strict-mode/no-multiple-exports // TODO: Fill in the component {description} and ensure links point to the correct {packageName} location. // Remove links that the component does not have (such as usage). If there are no links remove them all. /** * __Valid message__ * * A valid message {description}. * * - [Examples](https://atlassian.design/components/{packageName}/examples) * - [Code](https://atlassian.design/components/{packageName}/code) * - [Usage](https://atlassian.design/components/{packageName}/usage) */ // eslint-disable-next-line @atlaskit/volt-strict-mode/no-multiple-exports, @repo/internal/react/require-jsdoc export const ValidMessage = ({ children, testId }) => /*#__PURE__*/React.createElement(FieldId.Consumer, null, fieldId => /*#__PURE__*/React.createElement(Message, { appearance: "valid", fieldId: fieldId ? `${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. */ const MessageWrapperContext = /*#__PURE__*/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. * */ // eslint-disable-next-line @atlaskit/volt-strict-mode/no-multiple-exports, @repo/internal/react/require-jsdoc export const MessageWrapper = ({ children }) => { const contextValue = { isWrapper: true }; return /*#__PURE__*/React.createElement(MessageWrapperContext.Provider, { value: contextValue }, /*#__PURE__*/React.createElement("div", { "aria-live": "polite", "data-testid": "message-wrapper" }, children)); };