Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gt - Form label group additions #378

Merged
merged 2 commits into from
Apr 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 25 additions & 3 deletions src/components/FormLabelGroup.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,25 @@ import PropTypes from 'prop-types';
import React from 'react';
import Col from './Col';
import FormGroup from './FormGroup';
import FormText from './FormText';
import FormFeedback from './FormFeedback';
import Label from './Label';
import Row from './Row';

const FormLabelGroup = props => {
const FormLabelGroup = (props) => {
const {
children,
color,
feedback,
inputId,
invalid,
hint,
label,
required,
rowClassName,
size,
stacked,
width
} = props;

const rowColor = color || (invalid && 'danger');
Copy link
Contributor

Choose a reason for hiding this comment

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

I added the invalid prop because I wasn't sure whether we wanted to include the FormFeedback piece, but I do agree that it makes sense to have that. As a result, I think we can get rid of the invalid prop and just change this check from:

const rowColor = color || (invalid && 'danger');

to:

const rowColor = color || (feedback && 'danger');

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Makes sense. My view on including the form feedback and hint is I'd like to see this as a general purpose component that can be used in place of FormRow. This helps by doing all the various feedback, etc in the correct place and tweaks, just without the FormRow 'magic' of type, state, value stuff

Expand All @@ -25,15 +31,19 @@ const FormLabelGroup = props => {
return (
<FormGroup row className={rowClassName} color={rowColor}>
{label && (
<Label for={inputId} sm={labelWidth} className={labelAlignment}>
<Label for={inputId} sm={labelWidth} size={size} className={labelAlignment}>
{label}
{required && label ? <span className="text-danger">&nbsp;*</span> : null}
</Label>
)}
<Col sm={inputContainerWidth}>
<Row>
{children}
<Col {...width}>
{children}
</Col>
</Row>
{hint && <FormText color="muted">{hint}</FormText>}
{feedback && <FormFeedback>{feedback}</FormFeedback>}
</Col>
</FormGroup>
);
Expand All @@ -42,16 +52,28 @@ const FormLabelGroup = props => {
FormLabelGroup.propTypes = {
children: PropTypes.node.isRequired,
color: PropTypes.string,
feedback: PropTypes.oneOfType([
PropTypes.string,
PropTypes.object
]),
inline: PropTypes.bool,
inputId: PropTypes.string,
invalid: PropTypes.bool,
hint: PropTypes.string,
label: PropTypes.node,
required: PropTypes.bool,
rowClassName: PropTypes.string,
size: PropTypes.string,
stacked: PropTypes.bool,
width: PropTypes.object
};

FormLabelGroup.defaultProps = {
inline: false,
invalid: false,
required: false,
stacked: false,
width: { xs: 12 }
};

export default FormLabelGroup;
4 changes: 2 additions & 2 deletions src/components/FormRow.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function parseFeedback(feedback) {
[feedback, {}];
}

const FormRow = props => {
const FormRow = (props) => {
const {
children,
color,
Expand Down Expand Up @@ -74,7 +74,7 @@ const FormRow = props => {
)}
<Col sm={inputContainerWidth}>
<Row>
<Col {...width} >
<Col {...width}>
<InputElement
id={id}
size={size}
Expand Down
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import Fade from './components/Fade';
import Form from './components/Form';
import FormFeedback from './components/FormFeedback';
import FormGroup from './components/FormGroup';
import FormLabelGroup from './components/FormLabelGroup';
import FormText from './components/FormText';
import Input from './components/Input';
import InputGroup from './components/InputGroup';
Expand Down Expand Up @@ -154,6 +155,7 @@ export {
Form,
FormFeedback,
FormGroup,
FormLabelGroup,
FormText,
Input,
InputGroup,
Expand Down
21 changes: 21 additions & 0 deletions stories/FormLabelGroup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import React from 'react';
import { storiesOf } from '@storybook/react';
import { boolean, text, select } from '@storybook/addon-knobs';
import { AddressInput, FormLabelGroup } from '../src';

storiesOf('FormLabelGroup', module)
.addWithInfo('Live example', () => (
<div>
<FormLabelGroup
label={text('label', 'Some Input')}
feedback={text('feedback', 'You must give a first name')}
color={select('color', ['', 'success', 'warning', 'danger'], 'danger')}
hint={text('hint', '')}
required={boolean('required', false)}
inline={boolean('inline', false)}
stacked={boolean('stacked', false)}
>
<img alt="Sample" src="https://placeimg.com/320/240/tech" width="320" height="240" />
</FormLabelGroup>
</div>
));
1 change: 1 addition & 0 deletions stories/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import './FeatureBanner';
import './FilterList';
import './Flag';
import './FormRow';
import './FormLabelGroup';
import './Forms';
import './HasManyFields';
import './HelpBubble';
Expand Down