Skip to content

Commit 22d316e

Browse files
committed
Merge pull request pagesource#4 in XTI/atomic-react-pattern-lib from feature/input-radio-atom to develop
* commit 'a5e7a0c321f73e8e74cf3405622cf8e282161ad5': removed fieldProps updated snapshot removing unnecessary prop from span gegerated snapshots again input choice default value is undefined move inputChoice to molecule create a single component InputChoice for radio and checkbox added test cases inputControl enhancement review comment incorporation review comment review comment incorporated initial changes for Inout Radio and checkbox
2 parents 4f8f340 + a5e7a0c commit 22d316e

20 files changed

+26274
-1
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// @flow
2+
import React from 'react';
3+
import type { Node } from 'react';
4+
import { Field, ErrorMessage } from 'formik';
5+
import styled from 'styled-components';
6+
import InputChoice from '../../InputChoice';
7+
import FieldError from '../../../atoms/FieldError';
8+
import styles from './FieldCheckbox.style';
9+
10+
type Props = {
11+
id?: string,
12+
className?: string,
13+
name: string,
14+
};
15+
16+
const FieldCheckbox = ({ id, className, name, ...others }: Props): Node => (
17+
<div className={className}>
18+
<Field
19+
name={name}
20+
render={({ field }) => <InputChoice type="checkbox" id={id || name} {...field} {...others} />}
21+
/>
22+
<ErrorMessage name={name} component={FieldError} />
23+
</div>
24+
);
25+
26+
FieldCheckbox.defaultProps = {
27+
id: '',
28+
className: '',
29+
};
30+
31+
export default styled(FieldCheckbox)`
32+
${styles};
33+
`;
34+
export { FieldCheckbox as FieldCheckboxVanilla };
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { css } from 'styled-components';
2+
3+
const styles = css`
4+
margin-bottom: 1rem;
5+
`;
6+
7+
export default styles;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// @flow
2+
3+
export { default } from './FieldCheckbox';
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// @flow
2+
import React from 'react';
3+
import type { Node } from 'react';
4+
import { Field, ErrorMessage, FieldArray } from 'formik';
5+
import styled from 'styled-components';
6+
import InputChoice from '../../InputChoice';
7+
import FieldError from '../../../atoms/FieldError';
8+
import styles from './FieldRadioGroup.style';
9+
10+
type Props = {
11+
id?: string,
12+
className?: string,
13+
name: string,
14+
radioGroupArray: Array,
15+
};
16+
17+
const FieldRadioGroup = ({ id, className, name, radioGroupArray, ...others }: Props): Node => (
18+
<FieldArray
19+
name={name}
20+
render={() => (
21+
<div className={className} id={id} {...others}>
22+
{radioGroupArray.map((radioGroup, index) => (
23+
<Field
24+
key={radioGroup.id || `${name}_${index}`}
25+
name={name}
26+
render={({ field }) => (
27+
<InputChoice
28+
type="radio"
29+
id={radioGroup.id || `${name}_${index}`}
30+
{...field}
31+
{...radioGroup}
32+
/>
33+
)}
34+
/>
35+
))}
36+
<ErrorMessage name={name} component={FieldError} />
37+
</div>
38+
)}
39+
/>
40+
);
41+
42+
FieldRadioGroup.defaultProps = {
43+
id: '',
44+
className: '',
45+
};
46+
47+
export default styled(FieldRadioGroup)`
48+
${styles};
49+
`;
50+
export { FieldRadioGroup as FieldRadioGroupVanila };
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { css } from 'styled-components';
2+
3+
const styles = css`
4+
margin-bottom: 1rem;
5+
`;
6+
7+
export default styles;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// @flow
2+
3+
export { default } from './FieldRadioGroup';

lib/components/molecules/Form/Form.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import { noop } from '../../../core/utils/noop';
77
import Debug from './Debug';
88
import FieldInput from './FieldInput';
99
import FieldSelect from './FieldSelect';
10+
import FieldRadioGroup from './FieldRadioGroup';
11+
import FieldCheckbox from './FieldCheckbox';
1012

1113
type Props = {
1214
className?: string,
@@ -40,6 +42,10 @@ class Form extends React.PureComponent<Props> {
4042

4143
static Select = FieldSelect;
4244

45+
static RadioGroup = FieldRadioGroup;
46+
47+
static Checkbox = FieldCheckbox;
48+
4349
static Field = Field;
4450

4551
getSchema = (

lib/components/molecules/Form/Form.mock.js

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ const userFormProps = {
55
email: '',
66
username: '',
77
typeOfUser: '',
8+
ageGroup: '',
9+
termsAndConditions: false,
810
},
911
debug: true,
1012
action: '/',
@@ -18,6 +20,13 @@ const userFormProps = {
1820
email: 'Invalid email format',
1921
required: 'Required',
2022
},
23+
ageGroup: {
24+
required: 'Please select an Age group',
25+
},
26+
termsAndConditions: {
27+
type: 'bool',
28+
test: ['termsAndConditions', 'please accept terms and conditions!!!', value => value === true],
29+
},
2130
},
2231
handleSubmit: action('On Submit Fired'),
2332
};
@@ -40,4 +49,30 @@ const userFormEmailProps = {
4049
label: 'Email',
4150
};
4251

43-
export { userFormProps, userFormSelectProps, userFormNameInputProps, userFormEmailProps };
52+
const userFormAgeGroupProps = {
53+
name: 'ageGroup',
54+
radioGroupArray: [{
55+
value: '30-',
56+
label: '< 30',
57+
}, {
58+
value: '30-60',
59+
label: '30-60',
60+
}, {
61+
value: '60+',
62+
label: '> 60',
63+
}],
64+
};
65+
66+
const userFormTnCProps = {
67+
name: 'termsAndConditions',
68+
label: 'I accept terms & conditions',
69+
};
70+
71+
export {
72+
userFormProps,
73+
userFormSelectProps,
74+
userFormNameInputProps,
75+
userFormEmailProps,
76+
userFormAgeGroupProps,
77+
userFormTnCProps,
78+
};

lib/components/molecules/Form/Form.story.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import {
66
userFormSelectProps,
77
userFormNameInputProps,
88
userFormEmailProps,
9+
userFormAgeGroupProps,
10+
userFormTnCProps,
911
} from './Form.mock';
1012

1113
// Import Vanilla Component to display markup and props
@@ -24,6 +26,8 @@ storiesOf('Molecules', module).addWithChapters('Form', {
2426
<Form.Select {...userFormSelectProps} />
2527
<Form.Input {...userFormNameInputProps} />
2628
<Form.Input {...userFormEmailProps} />
29+
<Form.RadioGroup {...userFormAgeGroupProps} />
30+
<Form.Checkbox {...userFormTnCProps} />
2731
</div>
2832
<Button primary type="submit">
2933
Submit
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// @flow
2+
import React from 'react';
3+
import type { Node } from 'react';
4+
import styled from 'styled-components';
5+
import { InputVanilla as Input } from '../../atoms/Input';
6+
import Label from '../../atoms/Label';
7+
import styles from './InputChoice.style';
8+
9+
type Props = {
10+
id: string,
11+
type: string,
12+
disabled?: boolean,
13+
checked?: boolean,
14+
className?: string,
15+
children?: Node,
16+
label?: string,
17+
labelProps?: {},
18+
showLabelBeforeInput?: boolean,
19+
};
20+
21+
const InputChoice = ({
22+
id,
23+
type,
24+
checked,
25+
disabled,
26+
className,
27+
label,
28+
children,
29+
showLabelBeforeInput,
30+
labelProps,
31+
...otherProps
32+
}: Props): Node => (
33+
<Label
34+
className={className}
35+
htmlFor={id}
36+
showLabelBeforeInput={showLabelBeforeInput}
37+
{...labelProps}
38+
>
39+
<Input id={id} type={type} defaultChecked={checked} disabled={disabled} {...otherProps} />
40+
<span type={type} />
41+
<div>{children || label}</div>
42+
</Label>
43+
);
44+
45+
InputChoice.defaultProps = {
46+
checked: undefined,
47+
disabled: false,
48+
className: '',
49+
children: '',
50+
label: '',
51+
labelProps: {},
52+
showLabelBeforeInput: false,
53+
};
54+
55+
export default styled(InputChoice)`
56+
${styles};
57+
`;
58+
export { InputChoice as InputChoiceVanilla };

0 commit comments

Comments
 (0)