I am using React with Ant Design and I want to disable one Form.Item username field. Please see my sample code below,
<Form.Item disabled style={{ padding: '0px 20px' }}>
{getFieldDecorator('username', {
initialValue: this.state.userInfo.username,
validate: [
{
trigger: ['onChange', 'onBlur'],
rules: [
{
required: true,
message: t('pleaseInput.username'),
}
],
},
],
})
</Form.Item>
Disable the TextBox by adding the e-disabled to the input parent element and set disabled attribute to the input element.
valuePropName is the name of the prop of the child component that will contain the value. In most cases, components such as Input or Select use value as the prop for holding the value. However, certain components such as Checkbox or Switch use checked .
antd is 348kB uncompressed. The entire app including antd, React and stupidly large lodash plus lots of other stuff is 350kB gzipped.
You need to display some sort of UI component inside your <Form.Item>
.
I suppose you want to display an <Input>
since you have a username. Here's what could be possible with your code :
<Form.Item disabled style={{ padding: '0px 20px' }}>
{getFieldDecorator('username', {
initialValue: this.state.userInfo.username,
validate: [
{
trigger: ['onChange', 'onBlur'],
rules: [
{
required: true,
message: t('pleaseInput.username'),
}
],
},
],
})(
<Input
// This is where you want to disable your UI control
disabled={true}
placeholder="Username"
/>
)}
</Form.Item>
Edit 2021: this answer targets the use of ant design's v3 Form component. While the boolean to disable a field might be the same, the Form component in the v4 has seen a lot of changes. See the documentation to migrate from v3 to v4 here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With