@mantine/core
Version:
React components library focused on usability, accessibility and developer experience
1 lines • 14.8 kB
Source Map (JSON)
{"version":3,"file":"Input.cjs","names":["createVarsResolver","getSize","getFontSize","getRadius","rem","polymorphicFactory","useProps","extractStyleProps","InputWrapperContext","useStyles","Loader","InputClearSection","InputContext","Box","classes","InputWrapper","InputLabel","InputError","InputDescription","InputPlaceholder","InputClearButton"],"sources":["../../../src/components/Input/Input.tsx"],"sourcesContent":["import { use } from 'react';\nimport {\n Box,\n BoxProps,\n createVarsResolver,\n DataAttributes,\n extractStyleProps,\n getFontSize,\n getRadius,\n getSize,\n MantineRadius,\n MantineSize,\n polymorphicFactory,\n PolymorphicFactory,\n rem,\n StylesApiProps,\n useProps,\n useStyles,\n} from '../../core';\nimport { Loader } from '../Loader/Loader';\nimport { InputContext } from './Input.context';\nimport { InputClearButton } from './InputClearButton/InputClearButton';\nimport { ClearSectionMode, InputClearSection } from './InputClearSection/InputClearSection';\nimport { InputDescription } from './InputDescription/InputDescription';\nimport { InputError } from './InputError/InputError';\nimport { InputLabel } from './InputLabel/InputLabel';\nimport { InputPlaceholder } from './InputPlaceholder/InputPlaceholder';\nimport { InputWrapperContext } from './InputWrapper.context';\nimport {\n __InputWrapperProps,\n InputWrapper,\n InputWrapperStylesNames,\n} from './InputWrapper/InputWrapper';\nimport classes from './Input.module.css';\n\n// Required to be a separate type for docgen script\ntype WrapperProps = React.ComponentProps<'div'> & DataAttributes;\n\nexport interface __BaseInputProps extends __InputWrapperProps, __InputProps {\n /** Props passed down to the root element */\n wrapperProps?: WrapperProps;\n}\n\nexport type __InputStylesNames = InputStylesNames | InputWrapperStylesNames;\n\nexport type InputStylesNames = 'input' | 'wrapper' | 'section';\nexport type InputVariant = 'default' | 'filled' | 'unstyled';\nexport type InputCssVariables = {\n wrapper:\n | '--input-height'\n | '--input-fz'\n | '--input-radius'\n | '--input-left-section-width'\n | '--input-right-section-width'\n | '--input-left-section-pointer-events'\n | '--input-right-section-pointer-events'\n | '--input-padding-y'\n | '--input-margin-top'\n | '--input-margin-bottom';\n};\n\nexport interface InputStylesCtx {\n offsetTop: boolean | undefined;\n offsetBottom: boolean | undefined;\n}\n\nexport interface __InputProps {\n /** Content section displayed on the left side of the input */\n leftSection?: React.ReactNode;\n\n /** Left section width, used to set `width` of the section and input `padding-left`, by default equals to the input height */\n leftSectionWidth?: React.CSSProperties['width'];\n\n /** Props passed down to the `leftSection` element */\n leftSectionProps?: React.ComponentProps<'div'>;\n\n /** Sets `pointer-events` styles on the `leftSection` element. Use `'all'` when section contains interactive elements (buttons, links). @default 'none' */\n leftSectionPointerEvents?: React.CSSProperties['pointerEvents'];\n\n /** Content section displayed on the right side of the input */\n rightSection?: React.ReactNode;\n\n /** Right section width, used to set `width` of the section and input `padding-right`, by default equals to the input height */\n rightSectionWidth?: React.CSSProperties['width'];\n\n /** Props passed down to the `rightSection` element */\n rightSectionProps?: React.ComponentProps<'div'>;\n\n /** Sets `pointer-events` styles on the `rightSection` element. Use `'all'` when section contains interactive elements (buttons, links). @default 'none' */\n rightSectionPointerEvents?: React.CSSProperties['pointerEvents'];\n\n /** Sets `required` attribute on the `input` element */\n required?: boolean;\n\n /** Key of `theme.radius` or any valid CSS value to set `border-radius`, numbers are converted to rem @default theme.defaultRadius */\n radius?: MantineRadius;\n\n /** Sets `disabled` attribute on the `input` element */\n disabled?: boolean;\n\n /** Controls input `height`, horizontal `padding`, and `font-size` @default 'sm' */\n size?: MantineSize | (string & {});\n\n /** Determines whether the input should have `cursor: pointer` style. Use when input acts as a button-like trigger (e.g., `component=\"button\"` for Select/DatePicker). @default false */\n pointer?: boolean;\n\n /** Determines whether the input should have red border and red text color when the `error` prop is set @default true */\n withErrorStyles?: boolean;\n\n /** HTML `size` attribute for the input element (number of visible characters) */\n inputSize?: string;\n\n /** Section to be displayed when the input is `__clearable` and `rightSection` is not defined */\n __clearSection?: React.ReactNode;\n\n /** Determines whether the `__clearSection` should be displayed if it is passed to the component, has no effect if `rightSection` is defined */\n __clearable?: boolean;\n\n /** Determines how the clear button and rightSection are rendered @default 'both' */\n __clearSectionMode?: ClearSectionMode;\n\n /** Right section displayed when both `__clearSection` and `rightSection` are not defined */\n __defaultRightSection?: React.ReactNode;\n\n /** Displays loading indicator in the left or right section @default false */\n loading?: boolean;\n\n /** Position of the loading indicator @default 'right' */\n loadingPosition?: 'left' | 'right';\n}\n\nexport interface InputProps extends BoxProps, __InputProps, StylesApiProps<InputFactory> {\n __staticSelector?: string;\n\n /** Props passed to Styles API context, replaces `Input.Wrapper` props */\n __stylesApiProps?: Record<string, any>;\n\n /** Determines whether the input should have error styles and `aria-invalid` attribute */\n error?: React.ReactNode;\n\n /** Adjusts padding and sizing calculations for multiline inputs (use with `component=\"textarea\"`). Does not make the input multiline by itself. @default false */\n multiline?: boolean;\n\n /** Input element id */\n id?: string;\n\n /** Determines whether `aria-` and other accessibility attributes should be added to the input. Only disable when implementing custom accessibility handling. @default true */\n withAria?: boolean;\n\n /** Props passed down to the root element of the `Input` component */\n wrapperProps?: WrapperProps;\n}\n\nexport type InputFactory = PolymorphicFactory<{\n props: InputProps;\n defaultRef: HTMLInputElement;\n defaultComponent: 'input';\n stylesNames: InputStylesNames;\n variant: InputVariant;\n vars: InputCssVariables;\n ctx: InputStylesCtx;\n staticComponents: {\n Label: typeof InputLabel;\n Error: typeof InputError;\n Description: typeof InputDescription;\n Placeholder: typeof InputPlaceholder;\n Wrapper: typeof InputWrapper;\n ClearButton: typeof InputClearButton;\n };\n}>;\n\nconst defaultProps = {\n variant: 'default',\n leftSectionPointerEvents: 'none',\n rightSectionPointerEvents: 'none',\n withAria: true,\n withErrorStyles: true,\n size: 'sm',\n loading: false,\n loadingPosition: 'right',\n} satisfies Partial<InputProps>;\n\nconst varsResolver = createVarsResolver<InputFactory>((_, props, ctx) => ({\n wrapper: {\n '--input-margin-top': ctx.offsetTop ? 'calc(var(--mantine-spacing-xs) / 2)' : undefined,\n '--input-margin-bottom': ctx.offsetBottom ? 'calc(var(--mantine-spacing-xs) / 2)' : undefined,\n '--input-height': getSize(props.size, 'input-height'),\n '--input-fz': getFontSize(props.size),\n '--input-radius': props.radius === undefined ? undefined : getRadius(props.radius),\n '--input-left-section-width':\n props.leftSectionWidth !== undefined ? rem(props.leftSectionWidth) : undefined,\n '--input-right-section-width':\n props.rightSectionWidth !== undefined ? rem(props.rightSectionWidth) : undefined,\n '--input-padding-y': props.multiline ? getSize(props.size, 'input-padding-y') : undefined,\n '--input-left-section-pointer-events': props.leftSectionPointerEvents,\n '--input-right-section-pointer-events': props.rightSectionPointerEvents,\n },\n}));\n\nexport const Input = polymorphicFactory<InputFactory>((_props) => {\n const props = useProps('Input', defaultProps, _props);\n const {\n classNames,\n className,\n style,\n styles,\n unstyled,\n required,\n __staticSelector,\n __stylesApiProps,\n size,\n wrapperProps,\n error,\n disabled,\n leftSection,\n leftSectionProps,\n leftSectionWidth,\n rightSection,\n rightSectionProps,\n rightSectionWidth,\n rightSectionPointerEvents,\n leftSectionPointerEvents,\n variant,\n vars,\n pointer,\n multiline,\n radius,\n id,\n withAria,\n withErrorStyles,\n mod,\n inputSize,\n attributes,\n __clearSection,\n __clearable,\n __clearSectionMode,\n __defaultRightSection,\n loading,\n loadingPosition,\n ...others\n } = props;\n\n const { styleProps, rest } = extractStyleProps(others);\n const ctx = use(InputWrapperContext);\n const stylesCtx: InputStylesCtx = { offsetBottom: ctx?.offsetBottom, offsetTop: ctx?.offsetTop };\n\n const getStyles = useStyles<InputFactory>({\n name: ['Input', __staticSelector],\n props: __stylesApiProps || props,\n classes,\n className,\n style,\n classNames,\n styles,\n unstyled,\n attributes,\n stylesCtx,\n rootSelector: 'wrapper',\n vars,\n varsResolver,\n });\n\n const ariaAttributes = withAria\n ? {\n required,\n disabled,\n 'aria-invalid': !!error,\n 'aria-describedby': ctx?.describedBy,\n id: ctx?.inputId || id,\n }\n : {};\n\n const loadingIndicator = loading ? (\n <Loader\n size={\n loadingPosition === 'left'\n ? 'calc(var(--input-left-section-size) / 2)'\n : 'calc(var(--input-right-section-size) / 2)'\n }\n />\n ) : null;\n\n const _leftSection = loading && loadingPosition === 'left' ? loadingIndicator : leftSection;\n const _rightSection: React.ReactNode = InputClearSection({\n __clearable,\n __clearSection,\n rightSection: loading && loadingPosition === 'right' ? loadingIndicator : rightSection,\n __defaultRightSection,\n size,\n __clearSectionMode,\n });\n\n return (\n <InputContext value={{ size: size || 'sm' }}>\n <Box\n {...getStyles('wrapper')}\n {...styleProps}\n {...wrapperProps}\n mod={[\n {\n error: !!error && withErrorStyles,\n pointer,\n disabled,\n multiline,\n 'data-with-right-section': !!_rightSection,\n 'data-with-left-section': !!_leftSection,\n },\n mod,\n ]}\n variant={variant}\n size={size}\n >\n {_leftSection && (\n <div\n {...leftSectionProps}\n data-position=\"left\"\n {...getStyles('section', {\n className: leftSectionProps?.className,\n style: leftSectionProps?.style,\n })}\n >\n {_leftSection}\n </div>\n )}\n\n <Box\n component=\"input\"\n {...rest}\n {...ariaAttributes}\n required={required}\n mod={{ disabled, error: !!error && withErrorStyles }}\n variant={variant}\n __size={inputSize}\n {...getStyles('input')}\n />\n\n {_rightSection && (\n <div\n {...rightSectionProps}\n data-position=\"right\"\n {...getStyles('section', {\n className: rightSectionProps?.className,\n style: rightSectionProps?.style,\n })}\n >\n {_rightSection}\n </div>\n )}\n </Box>\n </InputContext>\n );\n});\n\nInput.classes = classes;\nInput.varsResolver = varsResolver;\nInput.Wrapper = InputWrapper;\nInput.Label = InputLabel;\nInput.Error = InputError;\nInput.Description = InputDescription;\nInput.Placeholder = InputPlaceholder;\nInput.ClearButton = InputClearButton;\nInput.displayName = '@mantine/core/Input';\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AA2KA,MAAM,eAAe;CACnB,SAAS;CACT,0BAA0B;CAC1B,2BAA2B;CAC3B,UAAU;CACV,iBAAiB;CACjB,MAAM;CACN,SAAS;CACT,iBAAiB;CAClB;AAED,MAAM,eAAeA,6BAAAA,oBAAkC,GAAG,OAAO,SAAS,EACxE,SAAS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