Skip to content
This repository was archived by the owner on Feb 27, 2024. It is now read-only.

Commit aaee233

Browse files
author
Greg Rickaby
authored
Merge pull request #109 from WebDevStudios/feature/45-input-error-component
Feature/45 input error component
2 parents e2366d9 + e3860e1 commit aaee233

File tree

20 files changed

+295
-140
lines changed

20 files changed

+295
-140
lines changed

components/atoms/Icon/Icon.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,25 @@ function sizeToPx(size) {
1919
return sizeToRem[size] * parseFloat(tailwindConfig.theme.fontSize['root-em'])
2020
}
2121

22+
/**
23+
* Render the Icon component.
24+
*
25+
* @param {object} props The component attributes as props.
26+
* @param {boolean} props.ariaHidden If aria-hidden is true.
27+
* @param {string} props.className Icon className.
28+
* @param {string} props.icon Icon svg.
29+
* @param {string} props.size Icon size.
30+
* @param {string} props.style Icon style variation.
31+
* @param {string} props.title Icon title.
32+
* @return {Element} The Icon component.
33+
*/
2234
export default function Icon({
35+
ariaHidden,
36+
className,
2337
icon,
2438
size,
2539
style,
26-
title,
27-
className,
28-
ariaHidden
40+
title
2941
}) {
3042
return (
3143
<svg
@@ -46,8 +58,8 @@ Icon.propTypes = {
4658
ariaHidden: PropTypes.bool,
4759
className: PropTypes.string,
4860
icon: PropTypes.string.isRequired,
49-
style: PropTypes.oneOf(['fill', 'line']),
5061
size: PropTypes.oneOf(['sm', 'md', 'lg']),
62+
style: PropTypes.oneOf(['fill', 'line']),
5163
title: PropTypes.string.isRequired
5264
}
5365

components/atoms/Inputs/Checkbox/Checkbox.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@ import {Field} from 'formik'
99
* @param {string|number} props.id Input id.
1010
* @param {string} props.label Input label.
1111
* @param {string} props.name Input name.
12+
* @param {string} props.value Input value.
1213
* @return {Element} The Checkbox component.
1314
*/
14-
export default function Checkbox({className, id, label, name}) {
15+
export default function Checkbox({className, id, label, name, value}) {
1516
return (
1617
<div className={className}>
1718
{label && (
1819
<label htmlFor={id}>
19-
<Field name={name} type="checkbox" value={`value${id}`} />
20+
<Field name={name} type="checkbox" value={value || name} />
2021
{label}
2122
</label>
2223
)}
@@ -28,5 +29,6 @@ Checkbox.propTypes = {
2829
className: PropTypes.string,
2930
id: PropTypes.oneOfType([PropTypes.string, PropTypes.number]).isRequired,
3031
label: PropTypes.string.isRequired,
31-
name: PropTypes.string.isRequired
32+
name: PropTypes.string.isRequired,
33+
value: PropTypes.string
3234
}

components/atoms/Inputs/CheckboxGroup/CheckboxGroup.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,16 @@ import Checkbox from '@/components/atoms/Inputs/Checkbox'
1111
* @param {string|number} props.id CheckboxGroup id.
1212
* @param {string} props.label CheckboxGroup input label.
1313
* @param {string} props.description CheckboxGroup input name.
14+
* @param {boolean} props.isRequired If input is required.
1415
* @return {Element} The CheckboxGroup component.
1516
*/
1617
export default function CheckboxGroup({
1718
checkboxes,
1819
className,
1920
description,
2021
id: groupId,
21-
label
22+
label,
23+
isRequired
2224
}) {
2325
return (
2426
<div
@@ -27,18 +29,23 @@ export default function CheckboxGroup({
2729
id={groupId}
2830
role="group"
2931
>
30-
{label && <label htmlFor={groupId}>{label}</label>}
32+
{label && (
33+
<label htmlFor={groupId} required={isRequired}>
34+
{label}
35+
</label>
36+
)}
3137
{description && <p>{description}</p>}
3238
{!!checkboxes.length > 0 &&
3339
checkboxes.map((checkbox) => {
34-
const {id, label} = checkbox
40+
const {id, label, value} = checkbox
3541

3642
return (
3743
<Checkbox
3844
id={id}
3945
key={`${groupId}-${id}`}
4046
label={label}
4147
name={groupId}
48+
value={value}
4249
/>
4350
)
4451
})}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import PropTypes from 'prop-types'
2+
import {ErrorMessage} from 'formik'
3+
import styles from './InputError.module.css'
4+
5+
/**
6+
* Render the InputError component.
7+
*
8+
* @param {object} props The component attributes as props.
9+
* @param {string|number} props.name Input id.
10+
* @return {Element} The InputError component.
11+
*/
12+
export default function InputError({name}) {
13+
return (
14+
<span className={styles.inputError}>
15+
<ErrorMessage name={name} />
16+
</span>
17+
)
18+
}
19+
20+
InputError.propTypes = {
21+
name: PropTypes.oneOfType([PropTypes.number, PropTypes.string])
22+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.inputError {
2+
color: #df1642;
3+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export {default} from './InputError'

components/atoms/Inputs/Select/Select.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import styles from './Select.module.css'
77
* Render the Select component.
88
*
99
* @author WebDevStudios
10-
* @param {object} props props.
10+
* @param {object} props The component attributes as props.
1111
* @param {string} props.className Select wrapper className.
1212
* @param {string} props.description Select description.
1313
* @param {string} props.label Select input label.
@@ -26,7 +26,7 @@ export default function Select({
2626
}) {
2727
return (
2828
<div className={cn(styles.select, className)}>
29-
{label && <label>{label}</label>}
29+
{label && <label required={isRequired}>{label}</label>}
3030
<Field as="select" id={id} name={id} required={isRequired}>
3131
{!!options?.length > 0 &&
3232
options.map((option, key) => {

components/atoms/Inputs/Text/Text.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import PropTypes from 'prop-types'
2-
import {Field, ErrorMessage} from 'formik'
2+
import {Field} from 'formik'
3+
import InputError from '@/components/atoms/Inputs/InputError'
4+
import styles from './Text.module.css'
5+
import cn from 'classnames'
36

47
/**
58
* Render the Text component.
@@ -23,8 +26,12 @@ export default function Text({
2326
type
2427
}) {
2528
return (
26-
<div className={className}>
27-
{label && <label htmlFor={id}>{label}</label>}
29+
<div className={cn(styles.text, className)}>
30+
{label && (
31+
<label htmlFor={id} required={isRequired}>
32+
{label}
33+
</label>
34+
)}
2835
<Field
2936
aria-required={isRequired}
3037
id={id}
@@ -33,7 +40,7 @@ export default function Text({
3340
type={type}
3441
/>
3542
{description && <p>{description}</p>}
36-
<ErrorMessage name={id} />
43+
<InputError name={id} />
3744
</div>
3845
)
3946
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.text {
2+
& input {
3+
@apply rounded;
4+
}
5+
}

components/molecules/Form/Form.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export default function Form({
4242
}
4343

4444
Form.propTypes = {
45-
children: PropTypes.object,
45+
children: PropTypes.node,
4646
className: PropTypes.string,
4747
formDefaults: PropTypes.object,
4848
id: PropTypes.string,

0 commit comments

Comments
 (0)