Ant Design - Vertical Group of Checkboxes
I am trying to make a column of checkboxes using Ant design. I have tried:
<Checkbox.Group>
<Checkbox>Hello</Checkbox>
<Checkbox>Hello1</Checkbox>
<Checkbox>Hello2</Checkbox>
</Checkbox.Group>
And:
<CheckboxGroup options={plainOptions} value={this.state.checkedList} onChange={this.onChange} />
However, there does not seem to be an option to make checkboxes appear in a column instead of a row. I know I can use flexbox, but I was hoping Ant design would have a builtin method for making checkboxes appear vertically (in a column) instead of appearing in a horizontal row.
Is there a simple solution for stacking the checkboxes vertically?
reactjs jsx antd
add a comment |
I am trying to make a column of checkboxes using Ant design. I have tried:
<Checkbox.Group>
<Checkbox>Hello</Checkbox>
<Checkbox>Hello1</Checkbox>
<Checkbox>Hello2</Checkbox>
</Checkbox.Group>
And:
<CheckboxGroup options={plainOptions} value={this.state.checkedList} onChange={this.onChange} />
However, there does not seem to be an option to make checkboxes appear in a column instead of a row. I know I can use flexbox, but I was hoping Ant design would have a builtin method for making checkboxes appear vertically (in a column) instead of appearing in a horizontal row.
Is there a simple solution for stacking the checkboxes vertically?
reactjs jsx antd
add a comment |
I am trying to make a column of checkboxes using Ant design. I have tried:
<Checkbox.Group>
<Checkbox>Hello</Checkbox>
<Checkbox>Hello1</Checkbox>
<Checkbox>Hello2</Checkbox>
</Checkbox.Group>
And:
<CheckboxGroup options={plainOptions} value={this.state.checkedList} onChange={this.onChange} />
However, there does not seem to be an option to make checkboxes appear in a column instead of a row. I know I can use flexbox, but I was hoping Ant design would have a builtin method for making checkboxes appear vertically (in a column) instead of appearing in a horizontal row.
Is there a simple solution for stacking the checkboxes vertically?
reactjs jsx antd
I am trying to make a column of checkboxes using Ant design. I have tried:
<Checkbox.Group>
<Checkbox>Hello</Checkbox>
<Checkbox>Hello1</Checkbox>
<Checkbox>Hello2</Checkbox>
</Checkbox.Group>
And:
<CheckboxGroup options={plainOptions} value={this.state.checkedList} onChange={this.onChange} />
However, there does not seem to be an option to make checkboxes appear in a column instead of a row. I know I can use flexbox, but I was hoping Ant design would have a builtin method for making checkboxes appear vertically (in a column) instead of appearing in a horizontal row.
Is there a simple solution for stacking the checkboxes vertically?
reactjs jsx antd
reactjs jsx antd
asked Nov 22 '18 at 5:11
RoymunsonRoymunson
1,35321643
1,35321643
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Doesn't appear like it does since the <label>
s it renders are display: inline-block
. The simplest solution (other than rewriting the component) is to change .ant-checkbox-group-item
's attributes from (you can always wrap the CheckBox
with another class name to apply selectively, instead of globally):
.ant-checkbox-group-item {
display: inline-block;
margin-right: 8px;
}
to:
.ant-checkbox-group-item {
display: block;
margin-right: 0;
}
Which will make it appear like so:
Optionally, you can also add a display: inline-block
attribute to .ant-checkbox-group
's styles.
Which will then make it appear like so:
Another solution is to create some reusable components and apply the above via the style
property (admittedly, not an elegant solution since CheckboxGroup
's onChange
function expects to handle options
, but you'd have to intercept them in order to alter the Checkbox
's style property).
Working example: https://codesandbox.io/s/w0voxxxzm5
Checkbox.js
import React from "react";
import { Checkbox } from "antd";
export default ({ disabled, label, value, handleChange }) => (
<Checkbox
style={{ display: "block", marginLeft: 0 }}
disabled={disabled || false}
label={label}
checked={value}
onChange={handleChange}
>
{label}
</Checkbox>
);
CheckboxGroup.js
import React from "react";
import Checkbox from "./Checkbox";
export default ({ options, ...props }) => (
<div
className="ant-checkbox-group"
style={{ display: "inline-block", marginRight: 10 }}
>
{options.map(label => (
<Checkbox
key={label}
label={label}
disabled={props.disabled}
handleChange={props.handleChange}
value={props[label]}
/>
))}
</div>
);
Form.js
import React, { Component } from "react";
import CheckboxGroup from "./CheckboxGroup";
const options1 = ["Apple", "Pear", "Orange"];
const options2 = ["Strawberry", "Grape", "Mango"];
const options3 = ["Kiwi", "Banana", "Cherry"];
export default class Form extends Component {
state = {};
handleChange = ({ target: { label, checked } }) =>
this.setState({ [label]: checked });
handleSubmit = e => {
e.preventDefault();
alert(JSON.stringify(this.state, null, 4));
};
render = () => (
<form onSubmit={this.handleSubmit}>
<CheckboxGroup
{...this.state}
options={options1}
handleChange={this.handleChange}
/>
<CheckboxGroup
{...this.state}
options={options2}
handleChange={this.handleChange}
/>
<CheckboxGroup
{...this.state}
options={options3}
handleChange={this.handleChange}
disabled
/>
<div style={{ marginTop: 20 }}>
<button className="ant-btn ant-btn-primary" type="submit">
Submit
</button>
</div>
</form>
);
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53424246%2fant-design-vertical-group-of-checkboxes%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Doesn't appear like it does since the <label>
s it renders are display: inline-block
. The simplest solution (other than rewriting the component) is to change .ant-checkbox-group-item
's attributes from (you can always wrap the CheckBox
with another class name to apply selectively, instead of globally):
.ant-checkbox-group-item {
display: inline-block;
margin-right: 8px;
}
to:
.ant-checkbox-group-item {
display: block;
margin-right: 0;
}
Which will make it appear like so:
Optionally, you can also add a display: inline-block
attribute to .ant-checkbox-group
's styles.
Which will then make it appear like so:
Another solution is to create some reusable components and apply the above via the style
property (admittedly, not an elegant solution since CheckboxGroup
's onChange
function expects to handle options
, but you'd have to intercept them in order to alter the Checkbox
's style property).
Working example: https://codesandbox.io/s/w0voxxxzm5
Checkbox.js
import React from "react";
import { Checkbox } from "antd";
export default ({ disabled, label, value, handleChange }) => (
<Checkbox
style={{ display: "block", marginLeft: 0 }}
disabled={disabled || false}
label={label}
checked={value}
onChange={handleChange}
>
{label}
</Checkbox>
);
CheckboxGroup.js
import React from "react";
import Checkbox from "./Checkbox";
export default ({ options, ...props }) => (
<div
className="ant-checkbox-group"
style={{ display: "inline-block", marginRight: 10 }}
>
{options.map(label => (
<Checkbox
key={label}
label={label}
disabled={props.disabled}
handleChange={props.handleChange}
value={props[label]}
/>
))}
</div>
);
Form.js
import React, { Component } from "react";
import CheckboxGroup from "./CheckboxGroup";
const options1 = ["Apple", "Pear", "Orange"];
const options2 = ["Strawberry", "Grape", "Mango"];
const options3 = ["Kiwi", "Banana", "Cherry"];
export default class Form extends Component {
state = {};
handleChange = ({ target: { label, checked } }) =>
this.setState({ [label]: checked });
handleSubmit = e => {
e.preventDefault();
alert(JSON.stringify(this.state, null, 4));
};
render = () => (
<form onSubmit={this.handleSubmit}>
<CheckboxGroup
{...this.state}
options={options1}
handleChange={this.handleChange}
/>
<CheckboxGroup
{...this.state}
options={options2}
handleChange={this.handleChange}
/>
<CheckboxGroup
{...this.state}
options={options3}
handleChange={this.handleChange}
disabled
/>
<div style={{ marginTop: 20 }}>
<button className="ant-btn ant-btn-primary" type="submit">
Submit
</button>
</div>
</form>
);
}
add a comment |
Doesn't appear like it does since the <label>
s it renders are display: inline-block
. The simplest solution (other than rewriting the component) is to change .ant-checkbox-group-item
's attributes from (you can always wrap the CheckBox
with another class name to apply selectively, instead of globally):
.ant-checkbox-group-item {
display: inline-block;
margin-right: 8px;
}
to:
.ant-checkbox-group-item {
display: block;
margin-right: 0;
}
Which will make it appear like so:
Optionally, you can also add a display: inline-block
attribute to .ant-checkbox-group
's styles.
Which will then make it appear like so:
Another solution is to create some reusable components and apply the above via the style
property (admittedly, not an elegant solution since CheckboxGroup
's onChange
function expects to handle options
, but you'd have to intercept them in order to alter the Checkbox
's style property).
Working example: https://codesandbox.io/s/w0voxxxzm5
Checkbox.js
import React from "react";
import { Checkbox } from "antd";
export default ({ disabled, label, value, handleChange }) => (
<Checkbox
style={{ display: "block", marginLeft: 0 }}
disabled={disabled || false}
label={label}
checked={value}
onChange={handleChange}
>
{label}
</Checkbox>
);
CheckboxGroup.js
import React from "react";
import Checkbox from "./Checkbox";
export default ({ options, ...props }) => (
<div
className="ant-checkbox-group"
style={{ display: "inline-block", marginRight: 10 }}
>
{options.map(label => (
<Checkbox
key={label}
label={label}
disabled={props.disabled}
handleChange={props.handleChange}
value={props[label]}
/>
))}
</div>
);
Form.js
import React, { Component } from "react";
import CheckboxGroup from "./CheckboxGroup";
const options1 = ["Apple", "Pear", "Orange"];
const options2 = ["Strawberry", "Grape", "Mango"];
const options3 = ["Kiwi", "Banana", "Cherry"];
export default class Form extends Component {
state = {};
handleChange = ({ target: { label, checked } }) =>
this.setState({ [label]: checked });
handleSubmit = e => {
e.preventDefault();
alert(JSON.stringify(this.state, null, 4));
};
render = () => (
<form onSubmit={this.handleSubmit}>
<CheckboxGroup
{...this.state}
options={options1}
handleChange={this.handleChange}
/>
<CheckboxGroup
{...this.state}
options={options2}
handleChange={this.handleChange}
/>
<CheckboxGroup
{...this.state}
options={options3}
handleChange={this.handleChange}
disabled
/>
<div style={{ marginTop: 20 }}>
<button className="ant-btn ant-btn-primary" type="submit">
Submit
</button>
</div>
</form>
);
}
add a comment |
Doesn't appear like it does since the <label>
s it renders are display: inline-block
. The simplest solution (other than rewriting the component) is to change .ant-checkbox-group-item
's attributes from (you can always wrap the CheckBox
with another class name to apply selectively, instead of globally):
.ant-checkbox-group-item {
display: inline-block;
margin-right: 8px;
}
to:
.ant-checkbox-group-item {
display: block;
margin-right: 0;
}
Which will make it appear like so:
Optionally, you can also add a display: inline-block
attribute to .ant-checkbox-group
's styles.
Which will then make it appear like so:
Another solution is to create some reusable components and apply the above via the style
property (admittedly, not an elegant solution since CheckboxGroup
's onChange
function expects to handle options
, but you'd have to intercept them in order to alter the Checkbox
's style property).
Working example: https://codesandbox.io/s/w0voxxxzm5
Checkbox.js
import React from "react";
import { Checkbox } from "antd";
export default ({ disabled, label, value, handleChange }) => (
<Checkbox
style={{ display: "block", marginLeft: 0 }}
disabled={disabled || false}
label={label}
checked={value}
onChange={handleChange}
>
{label}
</Checkbox>
);
CheckboxGroup.js
import React from "react";
import Checkbox from "./Checkbox";
export default ({ options, ...props }) => (
<div
className="ant-checkbox-group"
style={{ display: "inline-block", marginRight: 10 }}
>
{options.map(label => (
<Checkbox
key={label}
label={label}
disabled={props.disabled}
handleChange={props.handleChange}
value={props[label]}
/>
))}
</div>
);
Form.js
import React, { Component } from "react";
import CheckboxGroup from "./CheckboxGroup";
const options1 = ["Apple", "Pear", "Orange"];
const options2 = ["Strawberry", "Grape", "Mango"];
const options3 = ["Kiwi", "Banana", "Cherry"];
export default class Form extends Component {
state = {};
handleChange = ({ target: { label, checked } }) =>
this.setState({ [label]: checked });
handleSubmit = e => {
e.preventDefault();
alert(JSON.stringify(this.state, null, 4));
};
render = () => (
<form onSubmit={this.handleSubmit}>
<CheckboxGroup
{...this.state}
options={options1}
handleChange={this.handleChange}
/>
<CheckboxGroup
{...this.state}
options={options2}
handleChange={this.handleChange}
/>
<CheckboxGroup
{...this.state}
options={options3}
handleChange={this.handleChange}
disabled
/>
<div style={{ marginTop: 20 }}>
<button className="ant-btn ant-btn-primary" type="submit">
Submit
</button>
</div>
</form>
);
}
Doesn't appear like it does since the <label>
s it renders are display: inline-block
. The simplest solution (other than rewriting the component) is to change .ant-checkbox-group-item
's attributes from (you can always wrap the CheckBox
with another class name to apply selectively, instead of globally):
.ant-checkbox-group-item {
display: inline-block;
margin-right: 8px;
}
to:
.ant-checkbox-group-item {
display: block;
margin-right: 0;
}
Which will make it appear like so:
Optionally, you can also add a display: inline-block
attribute to .ant-checkbox-group
's styles.
Which will then make it appear like so:
Another solution is to create some reusable components and apply the above via the style
property (admittedly, not an elegant solution since CheckboxGroup
's onChange
function expects to handle options
, but you'd have to intercept them in order to alter the Checkbox
's style property).
Working example: https://codesandbox.io/s/w0voxxxzm5
Checkbox.js
import React from "react";
import { Checkbox } from "antd";
export default ({ disabled, label, value, handleChange }) => (
<Checkbox
style={{ display: "block", marginLeft: 0 }}
disabled={disabled || false}
label={label}
checked={value}
onChange={handleChange}
>
{label}
</Checkbox>
);
CheckboxGroup.js
import React from "react";
import Checkbox from "./Checkbox";
export default ({ options, ...props }) => (
<div
className="ant-checkbox-group"
style={{ display: "inline-block", marginRight: 10 }}
>
{options.map(label => (
<Checkbox
key={label}
label={label}
disabled={props.disabled}
handleChange={props.handleChange}
value={props[label]}
/>
))}
</div>
);
Form.js
import React, { Component } from "react";
import CheckboxGroup from "./CheckboxGroup";
const options1 = ["Apple", "Pear", "Orange"];
const options2 = ["Strawberry", "Grape", "Mango"];
const options3 = ["Kiwi", "Banana", "Cherry"];
export default class Form extends Component {
state = {};
handleChange = ({ target: { label, checked } }) =>
this.setState({ [label]: checked });
handleSubmit = e => {
e.preventDefault();
alert(JSON.stringify(this.state, null, 4));
};
render = () => (
<form onSubmit={this.handleSubmit}>
<CheckboxGroup
{...this.state}
options={options1}
handleChange={this.handleChange}
/>
<CheckboxGroup
{...this.state}
options={options2}
handleChange={this.handleChange}
/>
<CheckboxGroup
{...this.state}
options={options3}
handleChange={this.handleChange}
disabled
/>
<div style={{ marginTop: 20 }}>
<button className="ant-btn ant-btn-primary" type="submit">
Submit
</button>
</div>
</form>
);
}
edited Nov 22 '18 at 7:57
answered Nov 22 '18 at 5:48
Matt CarlottaMatt Carlotta
2,5481611
2,5481611
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53424246%2fant-design-vertical-group-of-checkboxes%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown