@mantine/core
Version:
React components library focused on usability, accessibility and developer experience
1 lines • 10.4 kB
Source Map (JSON)
{"version":3,"file":"InputWrapper.cjs","names":["createVarsResolver","getFontSize","rem","factory","useProps","useStyles","InputLabel","InputDescription","Fragment","InputError","InputWrapperContext","getInputOffsets","Box","classes"],"sources":["../../../../src/components/Input/InputWrapper/InputWrapper.tsx"],"sourcesContent":["import { Fragment } from 'react';\nimport { useId } from '@mantine/hooks';\nimport {\n Box,\n BoxProps,\n createVarsResolver,\n DataAttributes,\n ElementProps,\n factory,\n Factory,\n getFontSize,\n MantineFontSize,\n rem,\n StylesApiProps,\n useProps,\n useStyles,\n} from '../../../core';\nimport {\n InputDescription,\n InputDescriptionCssVariables,\n InputDescriptionProps,\n InputDescriptionStylesNames,\n} from '../InputDescription/InputDescription';\nimport {\n InputError,\n InputErrorCssVariables,\n InputErrorProps,\n InputErrorStylesNames,\n} from '../InputError/InputError';\nimport {\n InputLabel,\n InputLabelCssVariables,\n InputLabelProps,\n InputLabelStylesNames,\n} from '../InputLabel/InputLabel';\nimport { InputWrapperContext } from '../InputWrapper.context';\nimport { getInputOffsets } from './get-input-offsets/get-input-offsets';\nimport classes from '../Input.module.css';\n\nexport type InputWrapperCssVariables = InputLabelCssVariables &\n InputErrorCssVariables &\n InputDescriptionCssVariables;\n\nexport type InputWrapperStylesNames =\n | 'root'\n | InputLabelStylesNames\n | InputDescriptionStylesNames\n | InputErrorStylesNames;\n\nexport interface __InputWrapperProps {\n /** Contents of `Input.Label` component. If not set, label is not displayed. */\n label?: React.ReactNode;\n\n /** Contents of `Input.Description` component. If not set, description is not displayed. */\n description?: React.ReactNode;\n\n /** Contents of `Input.Error` component. If not set, error is not displayed. */\n error?: React.ReactNode;\n\n /** Adds required attribute to the input and a red asterisk on the right side of label @default false */\n required?: boolean;\n\n /** If set, the required asterisk is displayed next to the label. Overrides `required` prop. Does not add required attribute to the input. @default false */\n withAsterisk?: boolean;\n\n /** Props passed down to the `Input.Label` component */\n labelProps?: InputLabelProps & DataAttributes;\n\n /** Props passed down to the `Input.Description` component */\n descriptionProps?: InputDescriptionProps & DataAttributes;\n\n /** Props passed down to the `Input.Error` component */\n errorProps?: InputErrorProps & DataAttributes;\n\n /** Render function to wrap the input element. Useful for adding tooltips, popovers, or other wrappers around the input. @default React.Fragment */\n inputContainer?: (children: React.ReactNode) => React.ReactNode;\n\n /** Controls order and visibility of wrapper elements. Only elements included in this array will be rendered. @default ['label', 'description', 'input', 'error'] */\n inputWrapperOrder?: ('label' | 'input' | 'description' | 'error')[];\n}\n\nexport interface InputWrapperProps\n extends __InputWrapperProps, BoxProps, StylesApiProps<InputWrapperFactory>, ElementProps<'div'> {\n __staticSelector?: string;\n\n /** Props passed to Styles API context, replaces Input.Wrapper props */\n __stylesApiProps?: Record<string, any>;\n\n /** Static id used as base to generate `aria-` attributes, by default generates random id */\n id?: string;\n\n /** Controls size of `Input.Label`, `Input.Description` and `Input.Error` components */\n size?: MantineFontSize;\n\n /** Root element for the label. Use `'div'` when wrapper contains multiple input elements and you need to handle `htmlFor` manually. @default 'label' */\n labelElement?: 'label' | 'div';\n}\n\nexport type InputWrapperFactory = Factory<{\n props: InputWrapperProps;\n ref: HTMLDivElement;\n stylesNames: InputWrapperStylesNames;\n vars: InputWrapperCssVariables;\n}>;\n\nconst defaultProps = {\n labelElement: 'label',\n inputContainer: (children) => children,\n inputWrapperOrder: ['label', 'description', 'input', 'error'],\n} satisfies Partial<InputWrapperProps>;\n\nconst varsResolver = createVarsResolver<InputWrapperFactory>((_, { size }) => ({\n label: {\n '--input-label-size': getFontSize(size),\n '--input-asterisk-color': undefined,\n },\n\n error: {\n '--input-error-size': size === undefined ? undefined : `calc(${getFontSize(size)} - ${rem(2)})`,\n },\n\n description: {\n '--input-description-size':\n size === undefined ? undefined : `calc(${getFontSize(size)} - ${rem(2)})`,\n },\n}));\n\nexport const InputWrapper = factory<InputWrapperFactory>((_props) => {\n const props = useProps('InputWrapper', defaultProps, _props);\n const {\n classNames,\n className,\n style,\n styles,\n unstyled,\n vars,\n size,\n variant,\n __staticSelector,\n inputContainer,\n inputWrapperOrder,\n label,\n error,\n description,\n labelProps,\n descriptionProps,\n errorProps,\n labelElement,\n children,\n withAsterisk,\n id,\n required,\n __stylesApiProps,\n mod,\n attributes,\n ...others\n } = props;\n\n const getStyles = useStyles<InputWrapperFactory>({\n name: ['InputWrapper', __staticSelector],\n props: __stylesApiProps || props,\n classes,\n className,\n style,\n classNames,\n styles,\n unstyled,\n attributes,\n vars,\n varsResolver,\n });\n\n const sharedProps = {\n size,\n variant,\n __staticSelector,\n };\n\n const idBase = useId(id);\n const isRequired = typeof withAsterisk === 'boolean' ? withAsterisk : required;\n const errorId = errorProps?.id || `${idBase}-error`;\n const descriptionId = descriptionProps?.id || `${idBase}-description`;\n const inputId = idBase;\n const hasError = !!error && typeof error !== 'boolean';\n const hasDescription = !!description;\n const _describedBy = `${hasError ? errorId : ''} ${hasDescription ? descriptionId : ''}`;\n const describedBy = _describedBy.trim().length > 0 ? _describedBy.trim() : undefined;\n const labelId = labelProps?.id || `${idBase}-label`;\n\n const _label = label && (\n <InputLabel\n key=\"label\"\n labelElement={labelElement}\n id={labelId}\n htmlFor={inputId}\n required={isRequired}\n {...sharedProps}\n {...labelProps}\n >\n {label}\n </InputLabel>\n );\n\n const _description = hasDescription && (\n <InputDescription\n key=\"description\"\n {...descriptionProps}\n {...sharedProps}\n size={descriptionProps?.size || sharedProps.size}\n id={descriptionProps?.id || descriptionId}\n >\n {description}\n </InputDescription>\n );\n\n const _input = <Fragment key=\"input\">{inputContainer(children)}</Fragment>;\n\n const _error = hasError && (\n <InputError\n {...errorProps}\n {...sharedProps}\n size={errorProps?.size || sharedProps.size}\n key=\"error\"\n id={errorProps?.id || errorId}\n >\n {error}\n </InputError>\n );\n\n const content = inputWrapperOrder.map((part) => {\n switch (part) {\n case 'label':\n return _label;\n case 'input':\n return _input;\n case 'description':\n return _description;\n case 'error':\n return _error;\n default:\n return null;\n }\n });\n\n return (\n <InputWrapperContext\n value={{\n getStyles,\n describedBy,\n inputId,\n labelId,\n ...getInputOffsets(inputWrapperOrder, { hasDescription, hasError }),\n }}\n >\n <Box\n variant={variant}\n size={size}\n mod={[{ error: !!error }, mod]}\n id={labelElement === 'label' ? undefined : id}\n {...getStyles('root')}\n {...others}\n >\n {content}\n </Box>\n </InputWrapperContext>\n );\n});\n\nInputWrapper.classes = classes;\nInputWrapper.varsResolver = varsResolver;\nInputWrapper.displayName = '@mantine/core/InputWrapper';\n"],"mappings":";;;;;;;;;;;;;;;;;;;AAyGA,MAAM,eAAe;CACnB,cAAc;CACd,iBAAiB,aAAa;CAC9B,mBAAmB;EAAC;EAAS;EAAe;EAAS;EAAQ;CAC9D;AAED,MAAM,eAAeA,6BAAAA,oBAAyC,GAAG,EAAE,YAAY;CAC7E,OAAO;EACL,sBAAsBC,iBAAAA,YAAY,KAAK;EACvC,0BAA0B,KAAA;EAC3B;CAED,OAAO,EACL,sBAAsB,SAAS,KAAA,IAAY,KAAA,IAAY,QAAQA,iBAAAA,YAAY,KAAK,CAAC,KAAKC,YAAAA,IAAI,EAAE,CAAC,IAC9F;CAED,aAAa,EACX,4BACE,SAAS,KAAA,IAAY,KAAA,IAAY,QAAQD,iBAAAA,YAAY,KAAK,CAAC,KAAKC,YAAAA,IAAI,EAAE,CAAC,IAC1E;CACF,EAAE;AAEH,MAAa,eAAeC,gBAAAA,SAA8B,WAAW;CACnE,MAAM,QAAQC,kBAAAA,SAAS,gBAAgB,cAAc,OAAO;CAC5D,MAAM,EACJ,YACA,WACA,OACA,QACA,UACA,MACA,MACA,SACA,kBACA,gBACA,mBACA,OACA,OACA,aACA,YACA,kBACA,YACA,cACA,UACA,cACA,IACA,UACA,kBACA,KACA,YACA,GAAG,WACD;CAEJ,MAAM,YAAYC,mBAAAA,UAA+B;EAC/C,MAAM,CAAC,gBAAgB,iBAAiB;EACxC,OAAO,oBAAoB;EAC3B,SAAA,qBAAA;EACA;EACA;EACA;EACA;EACA;EACA;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