布局
使用我们的表单布局选项,为您的表单提供一些结构 - 从内联到水平再到自定义网格实现。
表单
每组表单字段都应该位于 <Form>
元素中。Bootstrap 不会为 <Form>
元素提供默认样式,但默认情况下会提供一些强大的浏览器功能。
- 浏览器表单新手?考虑查看 MDN 表单文档 以获取概述和可用属性的完整列表。
<Form>
中的<button>
默认情况下为type="submit"
,因此要努力做到具体,并始终包含type
。- 您可以通过将所有控件包装在
<fieldset>
中并在其上设置disabled
属性来禁用表单中的所有控件。
由于 Bootstrap 将 display: block
和 width: 100%
应用于几乎所有我们的表单控件,因此表单默认情况下会垂直堆叠。可以使用其他类来根据每个表单的基础改变此布局。
表单组
FormGroup
组件是为表单添加一些结构的最简单方法。它为标签、控件、可选帮助文本和表单验证消息的组合提供了一个灵活的容器。将其与 fieldset
、div
或几乎任何其他元素一起使用。
您还可以添加 controlId
道具,通过 id
可访问地将嵌套的标签和输入连接在一起。
import Form from 'react-bootstrap/Form';function FormGroupExample() {return (<Form><Form.Group className="mb-3" controlId="formGroupEmail"><Form.Label>Email address</Form.Label><Form.Control type="email" placeholder="Enter email" /></Form.Group><Form.Group className="mb-3" controlId="formGroupPassword"><Form.Label>Password</Form.Label><Form.Control type="password" placeholder="Password" /></Form.Group></Form>);}export default FormGroupExample;
表单网格
可以使用网格组件构建更复杂的表单。将它们用于需要多列、不同宽度和额外对齐选项的表单布局。
import Col from 'react-bootstrap/Col';import Form from 'react-bootstrap/Form';import Row from 'react-bootstrap/Row';function GridBasicExample() {return (<Form><Row><Col><Form.Control placeholder="First name" /></Col><Col><Form.Control placeholder="Last name" /></Col></Row></Form>);}export default GridBasicExample;
也可以使用网格系统创建更复杂的布局。
import Button from 'react-bootstrap/Button';import Col from 'react-bootstrap/Col';import Form from 'react-bootstrap/Form';import Row from 'react-bootstrap/Row';function GridComplexExample() {return (<Form><Row className="mb-3"><Form.Group as={Col} controlId="formGridEmail"><Form.Label>Email</Form.Label><Form.Control type="email" placeholder="Enter email" /></Form.Group><Form.Group as={Col} controlId="formGridPassword"><Form.Label>Password</Form.Label><Form.Control type="password" placeholder="Password" /></Form.Group></Row><Form.Group className="mb-3" controlId="formGridAddress1"><Form.Label>Address</Form.Label><Form.Control placeholder="1234 Main St" /></Form.Group><Form.Group className="mb-3" controlId="formGridAddress2"><Form.Label>Address 2</Form.Label><Form.Control placeholder="Apartment, studio, or floor" /></Form.Group><Row className="mb-3"><Form.Group as={Col} controlId="formGridCity"><Form.Label>City</Form.Label><Form.Control /></Form.Group><Form.Group as={Col} controlId="formGridState"><Form.Label>State</Form.Label><Form.Select defaultValue="Choose..."><option>Choose...</option><option>...</option></Form.Select></Form.Group><Form.Group as={Col} controlId="formGridZip"><Form.Label>Zip</Form.Label><Form.Control /></Form.Group></Row><Form.Group className="mb-3" id="formGridCheckbox"><Form.Check type="checkbox" label="Check me out" /></Form.Group><Button variant="primary" type="submit">Submit</Button></Form>);}export default GridComplexExample;
水平表单
通过将 as={Row}
添加到表单组并使用 Col
指定标签和控件的宽度,使用网格创建水平表单。请务必将 column
属性添加到您的 FormLabel
中,以便它们与关联的表单控件垂直居中。
有时,您可能需要使用边距或填充实用程序来创建所需的完美对齐。例如,我们已从堆叠的单选输入标签中删除了 padding-top
,以更好地对齐文本基线。
import Button from 'react-bootstrap/Button';import Col from 'react-bootstrap/Col';import Form from 'react-bootstrap/Form';import Row from 'react-bootstrap/Row';function HorizontalExample() {return (<Form><Form.Group as={Row} className="mb-3" controlId="formHorizontalEmail"><Form.Label column sm={2}></Form.Label><Col sm={10}><Form.Control type="email" placeholder="Email" /></Col></Form.Group><Form.Group as={Row} className="mb-3" controlId="formHorizontalPassword"><Form.Label column sm={2}>Password</Form.Label><Col sm={10}><Form.Control type="password" placeholder="Password" /></Col></Form.Group><fieldset><Form.Group as={Row} className="mb-3"><Form.Label as="legend" column sm={2}>Radios</Form.Label><Col sm={10}><Form.Checktype="radio"label="first radio"name="formHorizontalRadios"id="formHorizontalRadios1"/><Form.Checktype="radio"label="second radio"name="formHorizontalRadios"id="formHorizontalRadios2"/><Form.Checktype="radio"label="third radio"name="formHorizontalRadios"id="formHorizontalRadios3"/></Col></Form.Group></fieldset><Form.Group as={Row} className="mb-3" controlId="formHorizontalCheck"><Col sm={{ span: 10, offset: 2 }}><Form.Check label="Remember me" /></Col></Form.Group><Form.Group as={Row} className="mb-3"><Col sm={{ span: 10, offset: 2 }}><Button type="submit">Sign in</Button></Col></Form.Group></Form>);}export default HorizontalExample;
水平表单标签大小
您可以使用 column
属性调整 <FormLabel>
的大小,如所示。
import Col from 'react-bootstrap/Col';import Form from 'react-bootstrap/Form';import Row from 'react-bootstrap/Row';function FormLabelSizingExample() {return (<><Row><Form.Label column="lg" lg={2}>Large Text</Form.Label><Col><Form.Control size="lg" type="text" placeholder="Large text" /></Col></Row><br /><Row><Form.Label column lg={2}>Normal Text</Form.Label><Col><Form.Control type="text" placeholder="Normal text" /></Col></Row><br /><Row><Form.Label column="sm" lg={2}>Small Text</Form.Label><Col><Form.Control size="sm" type="text" placeholder="Small text" /></Col></Row></>);}export default FormLabelSizingExample;
列大小
如前面的示例所示,我们的网格系统允许您在 <Row>
中放置任意数量的 <Col>
。它们将在它们之间平均分配可用宽度。您也可以选择一部分列来占用更多或更少的空间,而其余 <Col>
将平均分配剩余空间,使用特定的列类,例如 <Col xs={7}>
。
import Col from 'react-bootstrap/Col';import Form from 'react-bootstrap/Form';import Row from 'react-bootstrap/Row';function GridColSizesExample() {return (<Form><Row><Col xs={7}><Form.Control placeholder="City" /></Col><Col><Form.Control placeholder="State" /></Col><Col><Form.Control placeholder="Zip" /></Col></Row></Form>);}export default GridColSizesExample;
自动调整大小
下面的示例使用 flexbox 实用程序垂直居中内容,并将 <Col>
更改为 <Col xs="auto">
,以便您的列仅占用所需的空间。换句话说,列根据内容自行调整大小。
import Button from 'react-bootstrap/Button';import Col from 'react-bootstrap/Col';import Form from 'react-bootstrap/Form';import InputGroup from 'react-bootstrap/InputGroup';import Row from 'react-bootstrap/Row';function GridAutoSizingExample() {return (<Form><Row className="align-items-center"><Col xs="auto"><Form.Label htmlFor="inlineFormInput" visuallyHidden>Name</Form.Label><Form.ControlclassName="mb-2"id="inlineFormInput"placeholder="Jane Doe"/></Col><Col xs="auto"><Form.Label htmlFor="inlineFormInputGroup" visuallyHidden>Username</Form.Label><InputGroup className="mb-2"><InputGroup.Text>@</InputGroup.Text><Form.Control id="inlineFormInputGroup" placeholder="Username" /></InputGroup></Col><Col xs="auto"><Form.Checktype="checkbox"id="autoSizingCheck"className="mb-2"label="Remember me"/></Col><Col xs="auto"><Button type="submit" className="mb-2">Submit</Button></Col></Row></Form>);}export default GridAutoSizingExample;
然后,您可以使用特定大小的列类再次对其进行混合。
import Button from 'react-bootstrap/Button';import Col from 'react-bootstrap/Col';import Form from 'react-bootstrap/Form';import InputGroup from 'react-bootstrap/InputGroup';import Row from 'react-bootstrap/Row';function GridAutoSizingColMixExample() {return (<Form><Row className="align-items-center"><Col sm={3} className="my-1"><Form.Label htmlFor="inlineFormInputName" visuallyHidden>Name</Form.Label><Form.Control id="inlineFormInputName" placeholder="Jane Doe" /></Col><Col sm={3} className="my-1"><Form.Label htmlFor="inlineFormInputGroupUsername" visuallyHidden>Username</Form.Label><InputGroup><InputGroup.Text>@</InputGroup.Text><Form.Controlid="inlineFormInputGroupUsername"placeholder="Username"/></InputGroup></Col><Col xs="auto" className="my-1"><Form.Checktype="checkbox"id="autoSizingCheck2"label="Remember me"/></Col><Col xs="auto" className="my-1"><Button type="submit">Submit</Button></Col></Row></Form>);}export default GridAutoSizingColMixExample;