Skip to content

Commit

Permalink
Merge pull request #192 from dabapps/form-group-offset-input
Browse files Browse the repository at this point in the history
FormGroup with no label
  • Loading branch information
JakeSidSmith authored Jan 29, 2018
2 parents daabc5a + 347d27f commit 75c6f9a
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 13 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@dabapps/roe",
"version": "0.8.30",
"version": "0.8.31",
"description": "A Collection of React Components for Project Development",
"main": "dist/js/index.js",
"types": "dist/js/index.d.ts",
Expand All @@ -13,7 +13,7 @@
"lint": "npm run lint-js && npm run lint-less",
"tests": "jest",
"test": "npm run lint && npm run tests -- --coverage --runInBand",
"postinstall": "./scripts/dist"
"prepublish": "./scripts/dist"
},
"repository": {
"type": "git",
Expand Down
8 changes: 8 additions & 0 deletions src/less/inputs.less
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,21 @@ textarea {
}
}

&.no-label > * { // lesshint universalSelector: false
margin-left: @no-label-offset;
}

& > textarea {
resize: vertical;
}

&.block {
width: 100%;

&.no-label > * { // lesshint universalSelector: false
margin-left: 0;
}

& > label,
& > input,
& > select,
Expand Down
1 change: 1 addition & 0 deletions src/less/variables.less
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@
@input-width: 200px;
@input-height: 32px;
@label-width: 100px;
@no-label-offset: @label-width + @margin-base;

@textarea-width: @input-width;
@textarea-height: @input-height * 2;
Expand Down
52 changes: 41 additions & 11 deletions src/ts/components/forms/form-group.examples.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
#### Example
#### Inline

```js
<FormGroup>
<label>
Label
</label>
<input type="text" />
</FormGroup>
```

#### No Label

```js
<div>
Expand All @@ -8,18 +19,36 @@
</label>
<input type="text" />
</FormGroup>

<FormGroup block>
<label>
Label
</label>
<select>
<option>
Option
</option>
</select>
<FormGroup noLabel>
<input type="text" />
</FormGroup>
<FormGroup noLabel>
<p>
Info text
</p>
</FormGroup>
</div>
```

#### Block

```js
<FormGroup block>
<label>
Label
</label>
<select>
<option>
Option
</option>
</select>
</FormGroup>
```

#### Checkboxes and Radios

```js
<div>
<FormGroup>
<label>
Label 1
Expand Down Expand Up @@ -47,6 +76,7 @@
@input-width: 200px;
@input-height: 32px;
@label-width: 100px;
@no-label-offset: @label-width + @margin-base;

@textarea-width: @input-width;
@textarea-height: @input-height * 2;
Expand Down
6 changes: 6 additions & 0 deletions src/ts/components/forms/form-group.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ export interface FormGroupProps extends ComponentProps, HTMLProps<HTMLElement> {
* Set the style `display: block;` with label above input.
*/
block?: boolean;
/**
* Offset the input, select, etc as if there was a label to the left of it
*/
noLabel?: boolean;
}

/**
Expand All @@ -19,13 +23,15 @@ export class FormGroup extends PureComponent<FormGroupProps, {}> {
children,
className,
block,
noLabel,
component: Component = 'div',
...remainingProps
} = this.props;

const myClassNames = [
'form-group',
block ? 'block' : null,
noLabel ? 'no-label' : null,
className
];

Expand Down
6 changes: 6 additions & 0 deletions tests/__snapshots__/form-group.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,9 @@ exports[`FormGroup should use a block prop (boolean) as a class name 1`] = `
className="form-group block"
/>
`;

exports[`FormGroup should use a noLabel prop (boolean) as a class name 1`] = `
<div
className="form-group no-label"
/>
`;
8 changes: 8 additions & 0 deletions tests/form-group.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ describe('FormGroup', () => {
expect(tree).toMatchSnapshot();
});

it('should use a noLabel prop (boolean) as a class name', () => {
const tree = renderer.create(
<FormGroup noLabel />
);

expect(tree).toMatchSnapshot();
});

it('should take regular element attributes', () => {
const tree = renderer.create(
<FormGroup className="my-class" />
Expand Down

0 comments on commit 75c6f9a

Please sign in to comment.