Skip to content

Commit

Permalink
XS-25 fix(input): adds support for prefix and suffix while showin…
Browse files Browse the repository at this point in the history
…g isRequired indicator
  • Loading branch information
kirtesh-xola committed Mar 22, 2024
1 parent 594bb2a commit 3bc9ba9
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions src/components/Forms/BaseInput.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import clsx from "clsx";
import PropTypes from "prop-types";
import React, { forwardRef, useMemo } from "react";
import React, { forwardRef } from "react";
import { isEmpty, isString } from "lodash";
import { Dot } from "../Dot/Dot";

Expand All @@ -12,7 +12,7 @@ const sizes = {

export const BaseInput = forwardRef(
({ as: Tag, size = "medium", isError, className, isRequired, value, prefix, suffix, ...rest }, ref) => {
const stringValue = useMemo(() => {
const stringValue = () => {
if (!isString(value)) return undefined;

let result = value;
Expand All @@ -37,15 +37,17 @@ export const BaseInput = forwardRef(
if (suffix) result = result.replace(new RegExp(`${suffix}$`), "");

// Remove whitespace characters currency sign for currency input
return result.
// Remove one or more whitespace characters that are not followed by a period
replace(/[^.\S]+/g, "").
// Remove any currency symbold
replace(/[\p{Sc}]/u, "");
}, [value, prefix, suffix]);
return (
result
// Remove one or more whitespace characters that are not followed by a period
.replace(/[^.\S]+/g, "")
// Remove any currency symbold
.replace(/[\p{Sc}]/u, "")
);
};

// Since the input can only be a string or a number, added the toString method for a numeric value, because lodash's IsEmpty method returns true for any number.
const isEmptyValue = isString(value) ? isEmpty(stringValue) : isEmpty(value?.toString());
const isEmptyValue = isString(value) ? isEmpty(stringValue()) : isEmpty(value?.toString());

return (
<div className="relative flex w-full items-center">
Expand Down Expand Up @@ -77,8 +79,8 @@ BaseInput.propTypes = {
isRequired: PropTypes.bool,
// eslint-disable-next-line react/require-default-props
value: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
prefix: PropTypes.string,
suffix: PropTypes.string,
prefix: PropTypes.string, // eslint-disable-next-line react/require-default-props
suffix: PropTypes.string, // eslint-disable-next-line react/require-default-props
};

BaseInput.defaultProps = {
Expand All @@ -87,6 +89,4 @@ BaseInput.defaultProps = {
className: "",
isError: false,
isRequired: false,
prefix: "",
suffix: "",
};

0 comments on commit 3bc9ba9

Please sign in to comment.