Skip to content

Commit db8e585

Browse files
committed
Added Molecules CheckboxFormField
1 parent e42adee commit db8e585

5 files changed

Lines changed: 121 additions & 10 deletions

File tree

src/atoms/forms/checkbox-input.tsx

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
import { InputTypes } from "../constants/input-types";
22
import React from "react";
33

4+
// -----------------------------------------------------------------------------------------
5+
// #region Constants
6+
// -----------------------------------------------------------------------------------------
7+
8+
const ELEMENT_CLASS = "e-checkbox";
9+
10+
// #endregion Constants
11+
412
// -----------------------------------------------------------------------------------------
513
// #region Interfaces
614
// -----------------------------------------------------------------------------------------
@@ -14,14 +22,6 @@ export interface CheckboxInputProperties {
1422

1523
// #endregion Interfaces
1624

17-
// -----------------------------------------------------------------------------------------
18-
// #region Constants
19-
// -----------------------------------------------------------------------------------------
20-
21-
const BASE_CLASS_NAME = "e-checkbox";
22-
23-
// #endregion Constants
24-
2525
// -----------------------------------------------------------------------------------------
2626
// #region Component
2727
// -----------------------------------------------------------------------------------------
@@ -30,7 +30,8 @@ const CheckboxInput: React.FC<CheckboxInputProperties> = (
3030
props: CheckboxInputProperties
3131
) => {
3232
const { checked, disabled, label, onChange } = props;
33-
let className = BASE_CLASS_NAME;
33+
34+
let className = ELEMENT_CLASS;
3435
if (disabled) {
3536
className += " -disabled";
3637
}
@@ -45,7 +46,7 @@ const CheckboxInput: React.FC<CheckboxInputProperties> = (
4546
type={InputTypes.Checkbox}
4647
value={checked.toString()}
4748
/>
48-
<span className={`${BASE_CLASS_NAME}__checkmark`}></span>
49+
<span className={`${ELEMENT_CLASS}__checkmark`}></span>
4950
</label>
5051
);
5152
};

src/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ export { Card } from "./molecules/cards/card";
6363
export { DropdownButton } from "./molecules/dropdown-button/dropdown-button";
6464
export { ErrorBanner } from "./molecules/errors/error-banner";
6565

66+
// Form Fields
67+
68+
export { CheckboxFormField } from "./molecules/form-fields/checkbox-form-field";
69+
6670
// #endregion Molecules
6771

6872
// -----------------------------------------------------------------------------------------
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import React from "react";
2+
import { boolean, text } from "@storybook/addon-knobs";
3+
import { CheckboxFormField } from "./checkbox-form-field";
4+
5+
export default {
6+
component: CheckboxFormField,
7+
title: "Molecules | Forms / Checkbox Form Field",
8+
};
9+
10+
export const checkboxFormFieldKnobs = () => (
11+
<CheckboxFormField
12+
checked={boolean("Checked", false)}
13+
label={text("Label", "Remember Me")}
14+
disabled={boolean("Disabled", false)}
15+
onChange={() => {}}
16+
/>
17+
);
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import React from "react";
2+
import { render } from "@testing-library/react";
3+
import { CheckboxFormField } from "./checkbox-form-field";
4+
import faker from "faker";
5+
6+
describe("CheckboxFormField", () => {
7+
test("when default props, renders input with label", () => {
8+
// Arrange
9+
const expected = faker.random.words();
10+
11+
// Act
12+
const { getByLabelText } = render(
13+
<CheckboxFormField
14+
checked={false}
15+
label={expected}
16+
onChange={() => {}}
17+
/>
18+
);
19+
20+
// Assert
21+
expect(getByLabelText(expected)).not.toBeNull();
22+
});
23+
});
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import {
2+
CheckboxInput,
3+
CheckboxInputProperties,
4+
} from "../../atoms/forms/checkbox-input";
5+
import React from "react";
6+
import { StringUtils } from "andculturecode-javascript-core";
7+
8+
// -----------------------------------------------------------------------------------------
9+
// #region Constants
10+
// -----------------------------------------------------------------------------------------
11+
12+
const COMPONENT_CLASS = "c-form-field";
13+
14+
// #endregion Constants
15+
16+
// -----------------------------------------------------------------------------------------
17+
// #region Interfaces
18+
// -----------------------------------------------------------------------------------------
19+
20+
export interface CheckboxFormFieldProperties extends CheckboxInputProperties {
21+
/**
22+
* An error message to display under the current checkbox with more information.
23+
*
24+
* @type {string}
25+
* @memberof CheckboxFormFieldProperties
26+
*/
27+
errorMessage?: string;
28+
}
29+
30+
// #endregion Interfaces
31+
32+
// -----------------------------------------------------------------------------------------
33+
// #region Component
34+
// -----------------------------------------------------------------------------------------
35+
36+
const CheckboxFormField: React.FC<CheckboxFormFieldProperties> = (props) => {
37+
const { checked, disabled, errorMessage, label, onChange } = props;
38+
39+
const hasError = StringUtils.hasValue(errorMessage);
40+
41+
return (
42+
<div className={COMPONENT_CLASS}>
43+
<CheckboxInput
44+
checked={checked}
45+
disabled={disabled}
46+
label={label}
47+
onChange={onChange}
48+
/>
49+
{hasError && (
50+
<div className={`${COMPONENT_CLASS}__errors`}>
51+
<label>{errorMessage}</label>
52+
</div>
53+
)}
54+
</div>
55+
);
56+
};
57+
58+
// #endregion Component
59+
60+
// -----------------------------------------------------------------------------------------
61+
// #region Exports
62+
// -----------------------------------------------------------------------------------------
63+
64+
export { CheckboxFormField };
65+
66+
// #endregion Exports

0 commit comments

Comments
 (0)