Skip to content

Commit

Permalink
Added data prop to Text, Input and Frequency (#270)
Browse files Browse the repository at this point in the history
  • Loading branch information
zecuria authored Oct 21, 2021
1 parent 253c73e commit 71f888c
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions src/components/Frequency.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ function Frequency(props) {
name,
mode,
label,
data,
annually,
quarterly,
monthly,
Expand Down Expand Up @@ -154,7 +155,7 @@ function Frequency(props) {
},
[isInputEmpty, isFrequencyEmpty]
);
const data = useMemo(
const validationData = useMemo(
() => ({
isInputEmpty,
isFrequencyEmpty,
Expand All @@ -176,13 +177,14 @@ function Frequency(props) {
disabled,
optional,
validate,
data,
data: validationData,
});
const inputComponent = (
<InternalInput
name={`${name}.amount`}
parentName={name}
variant="numeric"
data={data}
prefix={amountPrefix}
color={props.color}
placeholder={amountPlaceholder}
Expand Down Expand Up @@ -262,6 +264,7 @@ Frequency.propTypes = {
mode: PropTypes.oneOf(MODES),
label: PropTypes.string.isRequired,
annually: PropTypes.bool,
data: PropTypes.object,
quarterly: PropTypes.bool,
monthly: PropTypes.bool,
fortnightly: PropTypes.bool,
Expand Down
16 changes: 16 additions & 0 deletions src/components/Frequency.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,22 @@ describe("Frequency", () => {
});
});

it("with data attributes", () => {
render(
<FormWithFrequency
label="Salary"
amountPlaceholder="0.00"
data={{ "some-id": "true", "other-id": "other-value" }}
/>
);

const amountInput = screen.getByPlaceholderText("0.00");

expect(amountInput).toHaveAttribute("data-some-id", "true");

expect(amountInput).toHaveAttribute("data-other-id", "other-value");
});

it("with testId", () => {
const { container } = render(
<FormWithFrequency label="Salary" testId="my-frequency" />
Expand Down
7 changes: 5 additions & 2 deletions src/components/Input.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ function Input(props) {
name,
type,
variant,
data = {},
prefix,
suffix,
maxLength,
Expand All @@ -79,7 +80,7 @@ function Input(props) {
const [inputId] = useState(() => `input-${nanoid()}`);
const [auxId] = useState(() => `input-aux-${nanoid()}`);
const isEmpty = useCallback((value) => value.trim() === "", []);
const data = useMemo(
const validationData = useMemo(
() => ({
isEmpty,
variant,
Expand All @@ -99,7 +100,7 @@ function Input(props) {
disabled,
optional,
validate,
data,
data: validationData,
});
const onChange = useCallback(
(event) => {
Expand All @@ -124,6 +125,7 @@ function Input(props) {
id={label ? inputId : null}
name={name}
type={type}
data={data}
variant={variant}
prefix={prefix}
suffix={suffix}
Expand All @@ -149,6 +151,7 @@ Input.propTypes = {
name: PropTypes.string.isRequired,
type: PropTypes.oneOf(TYPES),
variant: PropTypes.oneOf(VARIANTS),
data: PropTypes.object,
prefix: PropTypes.string,
suffix: PropTypes.string,
maxLength: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
Expand Down
13 changes: 13 additions & 0 deletions src/components/Input.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,19 @@ describe("Input", () => {
});
});

it("with data attributes", () => {
render(
<FormWithInput
label="First name"
data={{ "some-id": "false", "other-id": "other-value" }}
/>
);

const input = screen.getByLabelText("First name");
expect(input).toHaveAttribute("data-some-id", "false");
expect(input).toHaveAttribute("data-other-id", "other-value");
});

it("with testId", () => {
const { container } = render(
<FormWithInput label="First name" testId="my-input" />
Expand Down
24 changes: 22 additions & 2 deletions src/components/Text.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { mergeProps } from "../utils/component";
import { hasOwnProperty } from "../utils/core";
import { TEXT_STYLES, TEXT_ALIGNS } from "../utils/constants";
import { formatArray } from "../utils/array";
import { getDataAttributes } from "../utils/getDataAttributes";

const AS = ["h1", "h2", "h3", "h4", "h5", "h6", "p"];
const COLORS = [
Expand Down Expand Up @@ -95,7 +96,16 @@ function Text(props) {
align: (align) => ALIGNS.includes(align),
wrap: (wrap) => typeof wrap === "boolean",
});
const { id, as, align, wrap, role, children, testId } = mergedProps;
const {
id,
as,
align,
wrap,
role,
children,
testId,
data = {},
} = mergedProps;
const css = useResponsivePropsCSS(mergedProps, DEFAULT_PROPS, {
color: (_, theme, bp) => {
const color =
Expand All @@ -112,10 +122,19 @@ function Text(props) {
margin: responsiveMargin,
textStyle: responsiveTextStyle,
});

const dataAttrs = getDataAttributes(data);

const Component = as;

return (
<Component id={id} css={css} role={role} data-testid={testId}>
<Component
id={id}
css={css}
role={role}
{...dataAttrs}
data-testid={testId}
>
{children}
</Component>
);
Expand All @@ -140,6 +159,7 @@ Text.propTypes = {
}
});
},
data: PropTypes.object,
align: PropTypes.oneOf(ALIGNS),
wrap: PropTypes.bool,
role: PropTypes.string,
Expand Down
12 changes: 12 additions & 0 deletions src/components/Text.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,18 @@ describe("Text", () => {
expect(node).toHaveAttribute("role", "alert");
});

it("with dataAttributes", () => {
render(
<Text data={{ "some-id": "false", "other-id": "other-value" }}>
Hello World
</Text>
);

const node = screen.getByText("Hello World");
expect(node).toHaveAttribute("data-some-id", "false");
expect(node).toHaveAttribute("data-other-id", "other-value");
});

it("with testId", () => {
const { container } = render(<Text testId="my-text">Hello World</Text>);

Expand Down
6 changes: 6 additions & 0 deletions src/components/internal/InternalInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import useTheme from "../../hooks/useTheme";
import useBackground from "../../hooks/useBackground";
import useResponsivePropsCSS from "../../hooks/useResponsivePropsCSS";
import { mergeProps } from "../../utils/component";
import { getDataAttributes } from "../../utils/getDataAttributes";

const TYPES = ["text", "password", "email", "tel"];
const VARIANTS = ["text", "numeric", "decimal"];
Expand Down Expand Up @@ -52,6 +53,7 @@ function InternalInput(props) {
name,
parentName,
id,
data = {},
type,
placeholder,
variant,
Expand Down Expand Up @@ -94,6 +96,8 @@ function InternalInput(props) {
},
[pasteAllowed]
);
const dataAttrs = getDataAttributes(data);

const variantProps =
variant === "numeric"
? {
Expand All @@ -120,6 +124,7 @@ function InternalInput(props) {
css={inputCSS}
id={id}
name={name}
{...dataAttrs}
data-parent-name={parentName}
aria-label={ariaLabel}
placeholder={placeholder}
Expand Down Expand Up @@ -155,6 +160,7 @@ InternalInput.propTypes = {
suffix: PropTypes.string,
maxLength: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
autoComplete: PropTypes.string,
data: PropTypes.object,
color: PropTypes.oneOf(COLORS),
disabled: PropTypes.bool,
pasteAllowed: PropTypes.bool,
Expand Down
4 changes: 4 additions & 0 deletions src/utils/getDataAttributes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const getDataAttributes = (data) =>
Object.fromEntries(
Object.entries(data).map(([key, value]) => [`data-${key}`, value])
);

1 comment on commit 71f888c

@vercel
Copy link

@vercel vercel bot commented on 71f888c Oct 21, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deployment failed with the following error:

The most recent charge for your active payment method has failed. Please update it here: https://vercel.com/teams/latitudefinancialoss/settings/billing.

Please sign in to comment.