Встроенная форма 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>
2 ответа
Вам нужно изменить ant-form-item-control-wrapper
стиль. Вы можете сделать это через CSS
или через Form.Item
"s wrapperCol
имущество.
Чтобы ниже работало, вы захотите обернуть Select
"s Form.Item
в элементе с className="select-container"
.select-container.ant-form-item-control-wrapper {
width: 100%;
}
или же
<Form.Item wrapperCol={{ sm: 24 }} style={{ width: "60%", marginRight: 0 }}>
sm
ссылается на макет столбца и 24
относится к схеме.
Рабочий пример: 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);
В
style = {{width:" 60% "}}
работал у меня, но в конце концов я предпочел использовать
style = {{flex: 1}}
, так как в моем случае я разделял
<InlineForm/>
на 3 поля.
import React from "react";
import ReactDOM from "react-dom";
import "antd/dist/antd.css";
import { Button, Form, Select } from "antd";
const students = [
{ id: 1, fullName: "Bob Smith" },
{ id: 2, fullName: "Amber Johnson" }
];
const InlineForm = () => {
function handleSubmit(e) {
e.preventDefault();
}
return (
<Form onSubmit={handleSubmit} layout="inline">
<Form.Item style={{ width: "60%", height: 90, margin: 0 }}>
<Select style={{ width: "100%" }}>
{students.map((student) => (
<Select.Option key={student.id} value={student.id}>
{student.fullName}
</Select.Option>
))}
</Select>
</Form.Item>
<Form.Item style={{ width: "40%", marginRight: 0 }}>
<Button type="primary" htmlType="submit" style={{ width: "100%" }}>
Register
</Button>
</Form.Item>
</Form>
);
};
const App = () => (
<div>
App using InlineForm
<br />
<InlineForm />
</div>
);
ReactDOM.render(<App />, document.getElementById("container"));