简体   繁体   English

可选择输入作为复选框反应

[英]Selectable Inputs in react as checkboxes

Im trying to make a row of custom check boxes but I can't seem to trigger a change on selection:我正在尝试制作一行自定义复选框,但似乎无法触发选择更改:

在此处输入图片说明

I can get it to render with the correct default option but I cannnot trigger any change in selection.我可以让它使用正确的默认选项进行渲染,但我无法触发选择的任何更改。 I was hoping to not use the native dropdown Select that react offers and wanted to build my own.我希望不使用 react 提供的本机下拉列表 Select 并希望构建我自己的。

const getVariances = async () => {
    /// gets and sets list of variants ///
    setProductVariances(variances)
}
const changeVariance = (e) => {
    const { value } = e.target;
    const newProduct = productVariances.find(o => o.id == value)
    setSelected(newProduct.id)
    setProduct(newProduct)
}

return (
    <div className='product-variances'>
        {productVariances.length ? productVariances.map(variance => (
            <div key={variance.id} className="col-sm-3">
                <div className="quiz_card_area">
                    <input className="quiz_checkbox variance-checkbox" onChange={(e) => changeVariance(e)} type='checkbox' defaultChecked={product.id === variance.id} value={variance.id} />
                    <div className="single_quiz_card">
                        <div className="quiz_card_content">
                            <div className="quiz_card_title">
                                <h3><i className="fa fa-check" aria-hidden="true"></i>{variance.name}</h3>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        )) : null}
    </div>
)

} }

Using 'onClick' to handle the action is not allowed with an input tag.不允许使用“onClick”来处理输入标签的操作。 Whats the best way to trigger the 'changeVariances' functionality with these custom check-boxes?使用这些自定义复选框触发“changeVariances”功能的最佳方法是什么?

"onChange" always works for input elements. “onChange”始终适用于输入元素。 I had also created a custom MultiSelect for my app.我还为我的应用程序创建了一个自定义 MultiSelect。 I'm sure you will get value if you console.log in我相信如果你在 console.log 中你会得到价值

const changeVariance = (e) => {
    console.log(e.target.value)
    const { value } = e.target;
    ....
}

By the looks of it, seems that you want a controlled component but you have not provided the checked attribute.从外观上看,您似乎想要一个受控组件,但您还没有提供checked属性。 I would suggest that you add it like this:我建议你像这样添加它:

<input className="quiz_checkbox variance-checkbox" 
  onChange={changeVariance} 
  type='checkbox'
  value={variance.id} 
  checked={selected===variance.id} 
/>

Note the removal of defaultChecked which is used for uncontrolled components, where you need to specify the first value but let the DOM take care of further value updates.请注意删除了用于不受控制的组件的defaultChecked ,您需要指定第一个值,但让 DOM 负责进一步的值更新。 To specify the initial value, you should set it in the state (in your selected state variable)要指定初始值,您应该将其设置在状态中(在您selected状态变量中)

Also, you don't need to pass an anonymous function to the onChange attribute, you can change it to: onChange={changeVariance} and it will work the same.此外,您不需要将匿名函数传递给onChange属性,您可以将其更改为: onChange={changeVariance}并且它的工作方式相同。

Also... Do you really need to keep two states, one for the selected variance id and another for the selected variance object?另外...你真的需要保持两种状态,一种用于选定的方差 id,另一种用于选定的方差对象? I suggest you keep just the one with the whole object.我建议你只保留一个和整个对象。 It would result in something like this:它会导致这样的事情:


const changeVariance = (e) => {
    const { value } = e.target;
    const newProduct = productVariances.find(o => o.id == value)
    setProduct(newProduct)
}

return (
    <div className='product-variances'>
        {productVariances.length ? productVariances.map(variance => (
            <div key={variance.id} className="col-sm-3">
                <div className="quiz_card_area">
                    <input className="quiz_checkbox variance-checkbox" 
                           onChange={changeVariance} 
                           type='checkbox' 
                           value={variance.id}
                           checked={variance.id === product.id} 
                    />
                    <div className="single_quiz_card">
                        <div className="quiz_card_content">
                            <div className="quiz_card_title">
                                <h3><i className="fa fa-check" aria-hidden="true"></i>{variance.name}</h3>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        )) : null}
    </div>
)

And lastly, I think you want radio buttons instead of checkboxes, because it seems like it doesn't make sense to check two variances at the same time!最后,我认为您需要单选按钮而不是复选框,因为同时检查两个差异似乎没有意义! If you decide to use radio buttons, make sure to check for e.target.checked before updating state on the onChange function, otherwise it will not work because onChange will be triggered for both checked and unchecked radio.如果您决定使用单选按钮,请确保在更新onChange函数的状态之前检查e.target.checked ,否则它将不起作用,因为选中和未选中的单选框都会触发 onChange。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM