首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ReactJS:禁用字段

ReactJS:禁用字段
EN

Stack Overflow用户
提问于 2020-05-14 15:25:57
回答 3查看 43关注 0票数 0

我有一个带有字段的表单,可以从列表中进行选择。

代码语言:javascript
复制
     <Column col={'12'} p={'x-0'}>
        {
          this.props.fields.map( (detailLine, index) => {
            return(
              <Column key={`GoodsServicesDataLines_${index}`} id={`GoodsServicesDataLines_${index}`} className="fieldGroup" col="12">
                <Row m={['l-0','r-0']}>
                  <button type={'button'} className={'w-100 close sub-form-delete-button'} onClick={ () => this.removeLine(index) } />

                    <Column col={["lg-9", "md-8"]}>
                      {
                            utility.field({
                              component: Fields.Select,
                              name: `${detailLine}.code`,
                              label: utility.t("PRODUCT", 'product'),
                              col: ["lg-9", "md-8"],
                              data: this.props.ProductsList || [],
                              disabled: true,
                           }
     </Column>
)

我的问题是,当data字段为this.props.ProductsList时,我应该禁用该字段,而如果数据等于[],则不应该禁用该字段。在你看来,我该怎么做呢?

谢谢。

编辑:

如果我尝试这样的东西:

disabled: this.props.ProductsList

该字段是禁用的,但如果我添加另一个带有加号按钮的字段,则新字段也是禁用的。

EN

回答 3

Stack Overflow用户

发布于 2020-05-14 15:27:11

简单地说

代码语言:javascript
复制
disabled: this.props.ProductsList.length === 0,
票数 0
EN

Stack Overflow用户

发布于 2020-05-14 15:27:42

您可以将disabled:true更改为

代码语言:javascript
复制
disabled: !this.props.ProductsList.length
票数 0
EN

Stack Overflow用户

发布于 2020-05-14 15:43:05

您可以对表单域使用Hooks - useState,并在输入内容时使用onChange方法。

代码语言:javascript
复制
const [data,setData]=useState([])

更新:

您还可以尝试:

禁用: this.props.ProductsList.length >0?false:true

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61791637

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档