Skip to content

Commit

Permalink
Create FormLayout component and make form fields ready for it (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamkudrna committed Jun 2, 2020
1 parent e092552 commit 6ed876a
Show file tree
Hide file tree
Showing 47 changed files with 549 additions and 15 deletions.
185 changes: 185 additions & 0 deletions src/demo/pages/DemoContainer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
CTAStart,
CheckboxField,
ForgotPassword,
FormLayout,
LayoutCenter,
List,
ListItem,
Expand Down Expand Up @@ -344,6 +345,190 @@ class DemoContainer extends React.Component {
</CTA>
)}
/>
<h3 id="layout-components-form-layout" className="typography-size-4 mb-6">Form Layout</h3>
<p>
Vertical Form Layout works similar to List except that List Items are not needed.
Vertical form field layout is forced.
</p>
<Documentation
name="Vertical Form Layout"
component={(
<FormLayout>
<>
<TextField
id="formLayoutVerticalFirstName"
changeHandler={logger}
label="First Name"
/>
<TextField
id="formLayoutVerticalLastName"
changeHandler={logger}
label="Last Name"
/>
</>
<TextField
id="formLayoutVerticalEmail"
changeHandler={logger}
label="Email address"
type="email"
helperText="Optional"
/>
<>
<TextField
id="formLayoutVerticalAddress1"
changeHandler={logger}
label="Address"
placeholder="Address line 1"
/>
<TextField
id="formLayoutVerticalAddress2"
changeHandler={logger}
isLabelVisible={false}
label="Address 2"
placeholder="Address line 2"
/>
<TextField
id="formLayoutVerticalZip"
changeHandler={logger}
helperText="ZIP should be 5 to 6 digits long code."
label="ZIP"
inputSize={6}
validationState="invalid"
/>
<TextField
id="formLayoutVerticalCountry"
changeHandler={logger}
label="Country"
/>
<CheckboxField
id="formLayoutVerticalDelivery"
changeHandler={logger}
label="This is my delivery address"
/>
</>
<SelectField
id="formLayoutVerticalFruit"
changeHandler={logger}
label="Your favourite fruit"
options={this.exampleOptions}
/>
<TextArea
id="formLayoutVerticalMessage"
changeHandler={logger}
fullWidth
label="Message"
rows={3}
/>
<Toggle
id="formLayoutVerticalNewsletter"
changeHandler={logger}
checked
description="Only once per week!"
label="Receive weekly newsletter"
required
/>
<Radio
id="formLayoutVerticalFruit2"
changeHandler={logger}
label="And fruit again!"
options={this.exampleOptions}
value="apples"
/>
</FormLayout>
)}
/>
<p>
Horizontal Form Layout is designed for horizontal form fields.
It is applied starting from <code>md</code> viewport size onwards.
Horizontal form field layout is forced.
</p>
<Documentation
name="Horizontal Form Layout"
component={(
<FormLayout fieldLayout="horizontal">
<>
<TextField
id="formLayoutHorizontalFirstName"
changeHandler={logger}
label="First Name"
/>
<TextField
id="formLayoutHorizontalLastName"
changeHandler={logger}
label="Last Name"
/>
</>
<TextField
id="formLayoutHorizontalEmail"
changeHandler={logger}
label="Email address"
type="email"
helperText="Optional"
/>
<>
<TextField
id="formLayoutHorizontalAddress1"
changeHandler={logger}
label="Address"
placeholder="Address line 1"
/>
<TextField
id="formLayoutHorizontalAddress2"
changeHandler={logger}
isLabelVisible={false}
label="Address 2"
placeholder="Address line 2"
/>
<TextField
id="formLayoutHorizontalZip"
changeHandler={logger}
helperText="ZIP should be 5 to 6 digits long code."
label="ZIP"
inputSize={6}
validationState="invalid"
/>
<TextField
id="formLayoutHorizontalCountry"
changeHandler={logger}
label="Country"
/>
<CheckboxField
id="formLayoutHorizontalDelivery"
changeHandler={logger}
label="This is my delivery address"
/>
</>
<SelectField
id="formLayoutHorizontalFruit"
changeHandler={logger}
label="Your favourite fruit"
options={this.exampleOptions}
/>
<TextArea
id="formLayoutHorizontalMessage"
changeHandler={logger}
fullWidth
label="Message"
rows={3}
/>
<Toggle
id="formLayoutHorizontalNewsletter"
changeHandler={logger}
checked
label="Receive weekly newsletter"
required
description="Only once per week!"
/>
<Radio
id="formLayoutHorizontalFruit2"
changeHandler={logger}
label="And fruit again!"
options={this.exampleOptions}
value="apples"
/>
</FormLayout>
)}
/>
<h3 id="layout-components-list" className="typography-size-4 mb-6">List</h3>
<Documentation
name="Default list"
Expand Down
4 changes: 4 additions & 0 deletions src/demo/pages/navigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ export default [
link: '#layout-components-cta',
title: 'CTA',
},
{
link: '#layout-components-form-layout',
title: 'Form Layout',
},
{
link: '#layout-components-list',
title: 'List',
Expand Down
3 changes: 2 additions & 1 deletion src/lib/components/layout/CardList/CardList.scss
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
@import '../../../styles/settings/layouts';
@import '../../../styles/tools/breakpoints';
@import '../../../styles/tools/offset';
@import './theme';
Expand All @@ -8,7 +9,7 @@
grid-template-columns: 1fr;
grid-template-rows: auto;
grid-gap: $card-list-grid-gap;
margin-bottom: offset(4);
margin-bottom: $layout-common-bottom-offset;

@include breakpoint-up(sm) {
grid-template-columns: repeat(auto-fit, minmax($card-list-card-min-width, $card-list-card-max-width));
Expand Down
58 changes: 58 additions & 0 deletions src/lib/components/layout/FormLayout/FormLayout.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import PropTypes from 'prop-types';
import React from 'react';
import styles from './FormLayout.scss';

const FormLayout = (props) => {
const {
children,
fieldLayout,
id,
} = props;

if (!children) {
return null;
}

const fieldLayoutClass = (layout) => {
if (layout === 'horizontal') {
return styles.rootFieldLayoutHorizontal;
}

return styles.rootFieldLayoutVertical;
};

return (
<div
id={id}
className={[
styles.root,
fieldLayoutClass(fieldLayout),
].join(' ')}
>
{React.Children.map(children, (child) => {
if (!React.isValidElement(child)) {
return null;
}

return React.cloneElement(child, {
inFormLayout: true,
layout: fieldLayout,
});
})}
</div>
);
};

FormLayout.defaultProps = {
children: null,
fieldLayout: 'vertical',
id: undefined,
};

FormLayout.propTypes = {
children: PropTypes.node,
fieldLayout: PropTypes.oneOf(['horizontal', 'vertical']),
id: PropTypes.string,
};

export default FormLayout;
22 changes: 22 additions & 0 deletions src/lib/components/layout/FormLayout/FormLayout.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
@import '../../../styles/settings/forms';
@import '../../../styles/settings/forms-theme';
@import '../../../styles/settings/layouts';
@import '../../../styles/tools/breakpoints';
@import '../../../styles/tools/offset';

.root {
margin-bottom: $layout-common-bottom-offset;
}

.rootFieldLayoutVertical,
.rootFieldLayoutHorizontal {
display: grid;
grid-template-columns: 1fr;
grid-row-gap: $form-field-vertical-outer-spacing;
}

.rootFieldLayoutHorizontal {
@include breakpoint-up($form-field-horizontal-breakpoint) {
grid-template-columns: $form-layout-horizontal-label-width 1fr;
}
}
1 change: 1 addition & 0 deletions src/lib/components/layout/FormLayout/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from './FormLayout';
3 changes: 2 additions & 1 deletion src/lib/components/layout/List/List.scss
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
@import '../../../styles/settings/layouts';
@import '../../../styles/tools/offset';

.root {
margin-bottom: offset(4);
margin-bottom: $layout-common-bottom-offset;
}

.list {
Expand Down
3 changes: 2 additions & 1 deletion src/lib/components/layout/Media/Media.scss
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
@import '../../../styles/settings/layouts';
@import '../../../styles/tools/offset';

.media {
display: flex;
align-items: flex-start;
margin-bottom: offset(3);
margin-bottom: $layout-common-bottom-offset;
}

.object {
Expand Down
3 changes: 2 additions & 1 deletion src/lib/components/layout/Row/Row.scss
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
@import '../../../styles/settings/layouts';
@import '../../../styles/tools/offset';

.row {
display: flex;
flex-wrap: wrap;
align-items: baseline;
margin-bottom: offset(3);
margin-bottom: $layout-common-bottom-offset;
}

.start,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ exports[`rendering renders correctly 1`] = `
fullWidth={true}
helperText={null}
id="resetEmailInput"
inFormLayout={false}
inputSize={null}
isLabelVisible={true}
label="E-mail"
Expand All @@ -62,6 +63,7 @@ exports[`rendering renders correctly 1`] = `
<label
className="root
isRootFullWidth
rootLayoutVertical
isRootRequired
rootSizeMedium
Expand Down Expand Up @@ -198,6 +200,7 @@ exports[`rendering renders correctly 2`] = `
fullWidth={true}
helperText={null}
id="resetEmailInput"
inFormLayout={false}
inputSize={null}
isLabelVisible={true}
label="E-mail"
Expand All @@ -212,6 +215,7 @@ exports[`rendering renders correctly 2`] = `
<label
className="root
isRootFullWidth
rootLayoutVertical
isRootRequired
rootSizeMedium
Expand Down Expand Up @@ -376,6 +380,7 @@ exports[`rendering renders correctly with all props except translations 1`] = `
fullWidth={true}
helperText={null}
id="custom-id__resetEmailInput"
inFormLayout={false}
inputSize={null}
isLabelVisible={true}
label="E-mail"
Expand All @@ -390,6 +395,7 @@ exports[`rendering renders correctly with all props except translations 1`] = `
<label
className="root
isRootFullWidth
rootLayoutVertical
isRootRequired
rootSizeMedium
Expand Down
Loading

0 comments on commit 6ed876a

Please sign in to comment.