cal*_*ben 2 css forms reactjs antd
我有一个表单,我希望与填充前 60% 空间的选择和填充接下来 40% 的提交按钮内联。如果没有选择和按钮大小不正确,我无法弄清楚如何做到这一点。选择大小本身以匹配其输入,从几乎没有大小开始。按钮的大小小于其文本。在这种情况下你应该怎么做?
<Form layout="inline">
<Form.Item style={{width:'60%'}}>
{getFieldDecorator('studentId', {
rules: [{ required: true, message: 'You must select a student' }],
})(
<Select style={{width:'100%'}}>
{this.props.linkedStudents.map(x => <Select.Option value={x.id}>{x.fullName}</Select.Option>)}
</Select>
)}
</Form.Item>
<Form.Item style={{width:'30%'}}>
<Button
type="primary"
htmlType="submit"
style={{ width: '30%' }}
>
Remove from Team
</Button>
</Form.Item>
</Form>
Run Code Online (Sandbox Code Playgroud)
你需要改变ant-form-item-control-wrapper
风格。您可以通过CSS
或通过Form.Item
的wrapperCol
属性来完成。
为了使下面的工作,您需要将Select
's包装Form.Item
在一个元素中className="select-container"
.select-container.ant-form-item-control-wrapper {
width: 100%;
}
Run Code Online (Sandbox Code Playgroud)
或者
<Form.Item wrapperCol={{ sm: 24 }} style={{ width: "60%", marginRight: 0 }}>
工作示例:https : //codesandbox.io/s/w0voxxxzm5(假设您不希望选择和按钮之间有任何装订线)
组件/InlineForm.js
import React, { PureComponent } from "react";
import { Button, Form, Select } from "antd";
const { Item } = Form;
const { Option } = Select;
const students = [
{
id: 1,
fullName: "Bob Smith"
},
{
id: 2,
fullName: "Amber Johnson"
}
];
class InlineForm extends PureComponent {
handleSubmit = e => {
e.preventDefault();
this.props.form.validateFieldsAndScroll((err, values) => {
if (!err) {
alert(JSON.stringify(values, null, 4));
}
});
};
render = () => {
const { getFieldDecorator } = this.props.form;
return (
<Form onSubmit={this.handleSubmit} layout="inline">
<Item
wrapperCol={{ sm: 24 }}
style={{ width: "60%", height: 60, marginBottom: 0, marginRight: 0 }}
>
{getFieldDecorator("studentId", {
rules: [{ required: true, message: "You must select a student" }]
})(
<Select style={{ width: "100%" }}>
{students.map(({ id, fullName }) => (
<Option key={fullName} value={id}>
{fullName}
</Option>
))}
</Select>
)}
</Item>
<Item wrapperCol={{ sm: 24 }} style={{ width: "40%", marginRight: 0 }}>
<Button type="primary" htmlType="submit" style={{ width: "100%" }}>
Register
</Button>
</Item>
</Form>
);
};
}
export default Form.create({ name: "inline-form" })(InlineForm);
Run Code Online (Sandbox Code Playgroud)