英文:
upload button should not allow upload if fields are empty
问题
我有三个输入字段和一个上传按钮。我希望上传按钮在其他三个字段填写后能够禁用和启用。我不知道如何实现这个逻辑。我已经附上了包含字段的代码部分。
上传按钮是代码中的最后一个字段。我该如何编写这个逻辑?
<div className='product'>
{/* ...其他代码... */}
{apiFetched && relatedPartyDetails?.map((party, index) => (
<Row key={index}>
{/* ...其他代码... */}
<Col lg={4}>
<div className='d-flex align-items-center Related_parties'>
<p className='mb-0 title-color'>Relation</p>
<Autocomplete
className='ms-3 mb-3'
options={[...parties]}
getOptionLabel={(option) => option.label}
id={"disable-clearable-relation-party-" + party.party_relation}
label="Party Relation"
renderInput={(params) => (
<TextField {...params} label="Party Relation" variant="standard" />
)}
defaultValue={relatedPartyDetails.party_relation}
getOptionSelected={(option) => option.label === 'test'}
onChange={(event, newValue) => { handleRelation(event, newValue, index); setRelation(parties); console.log('parties', parties); console.log('party', party); }}
value={parties.find((ele) => ele.value == party.party_relation)}
disableClearable
/>
</div>
{error && error?.party_relation && <span style={{ color: 'red' }}>{error.party_relation}</span>}
</Col>
{relation && <Col lg={2}>
<div className='drag-and-drop'>
<DropzoneArea
Icon="none"
filesLimit={1}
showPreviews={true}
defaultValue={relatedPartyDetails.upload_evidence}
showPreviewsInDropzone={false}
useChipsForPreview
previewGridProps={{ container: { spacing: 1, } }}
dropzoneText='Upload Evidence'
previewText=""
onChange={(file) => handleChangeFile(file[0], index)}
/>
</div>
{error && error?.upload_evidence && <span style={{ color: 'red' }}>{error.upload_evidence}</span>}
</Col>}
</Row>
))}
</div>
英文:
I have three input fields and an upload button.i want the upload button to be disabled and enabled if other three fields have been filled. i don't know how to go about this. i have attached the section of my code that contains the fields
the upload button is the last field in the code. how do I write this logic?
<div className='product'>
<>
{apiFetched && relatedPartyDetails?.map((party, index) => (
<Row key={index}>
<>
<Col lg={3}>
<Autocomplete
options={names}
getOptionLabel={(option) => (option.details?.name)}
id={"disable-clearable-buyer-" + index}
label="Party"
renderInput={(params) => (
<TextField {...params} label="Party 1" variant="standard" />
)}
onChange={(event, newValue) => {
console.log('test1');
// names.forEach((ele) => {
// console.log(ele.details.name);
// console.log('hurre',party.buyer==ele.details.name);
console.log(party.buyer);
// })
// console.log(names.find((ele) => ele.details.name == party.buyer))
handleParties(event, newValue,index,'buyer');
}}
disabled={isView}
value={names.find((ele) => ele.details.name === party.buyer)}
disableClearable
/>
{error && error?.buyer && <span style={{ color: 'red' }}>{error.buyer}</span>}
</Col>
<Col lg={3}>
<Autocomplete
options={names}
getOptionLabel={(option) => ( option.details?.name)}
id={"disable-clearable-shipper-" + index}
label="Party"
renderInput={(params) => (
<TextField {...params} label="Party 2" variant="standard" />
)}
onChange={(event, newValue) => {
handleParties(event, newValue, index,'shipper');
}}
disabled={isView}
value={(names && party.shipper) && names.find((ele) => ele.details.name === party.shipper)}
disableClearable
/>
{error && error?.shipper && <span style={{ color: 'red' }}>{error.shipper}</span>}
</Col>
<Col lg={4}>
<div className='d-flex align-items-center Related_parties'>
<p className='mb-0 title-color'>Relation</p>
<Autocomplete
className='ms-3 mb-3'
options={[...parties]}
getOptionLabel={(option) => option.label}
id={"disable-clearable-relation-party-" + party.party_relation}
label="Party Relation"
renderInput={(params) => (
<TextField {...params} label="Party Relation " variant="standard" />
)}
defaultValue={relatedPartyDetails.party_relation}
getOptionSelected={(option) => option.label === 'test'}
onChange={(event, newValue) => { handleRelation(event, newValue, index); setRelation(parties); console.log('parties', parties);console.log('party', party); }}
value={parties.find((ele) => ele.value == party.party_relation)}
disableClearable
/>
</div>
{error && error?.party_relation && <span style={{ color: 'red' }}>{error.party_relation}</span>}
</Col>
{relation && <Col lg={2}>
<div className='drag-and-drop'>
<DropzoneArea
Icon="none"
filesLimit={1}
showPreviews={true}
defaultValue={relatedPartyDetails.upload_evidence}
showPreviewsInDropzone={false}
useChipsForPreview
previewGridProps={{ container: { spacing: 1, } }}
dropzoneText='Upload Evidence'
previewText=""
onChange={(file) => handleChangeFile(file[0], index)}
/>
</div>
{error && error?.upload_evidence && <span style={{ color: 'red' }}>{error.upload_evidence}</span>}
</Col>}
</>
</Row>
))}
</>
</div>
</div>
答案1
得分: 0
如果您正在使用material-ui-dropzone
,实际上它创建了一个不受控制的Material-UI Dropzone。如果它是不受控制的,那么您将无法获得任何默认支持以禁用组件,而是可以在DropzoneArea的父div上应用CSS属性。
<div className='drag-and-drop' style={{ pointerEvents: checkIfConditionIsFulfilled ? "none" : "all" }}>
<DropzoneArea
Icon="none"
filesLimit={1}
showPreviews={true}
defaultValue={relatedPartyDetails.upload_evidence}
showPreviewsInDropzone={false}
useChipsForPreview
previewGridProps={{ container: { spacing: 1 } }}
dropzoneText='Upload Evidence'
previewText=""
onChange={(file) => handleChangeFile(file[0], index)}
/>
</div>
style={{ pointerEvents: checkIfConditionIsFulfilled ? "none" : "all" }}
您需要根据您的状态编写checkIfConditionIsFulfilled
。
英文:
if you are using material-ui-dropzone, it actually creates an uncontrolled Material-UI Dropzone. If it is uncontrolled, you are not getting any out of the box support for disabling the component but instead you can apply a css property on the parent div of DropzoneArea
<div className='drag-and-drop' style={{ pointerEvents: checkIfConditionIsFulfilled? "none" : "all }} >
<DropzoneArea
Icon="none"
filesLimit={1}
showPreviews={true}
defaultValue={relatedPartyDetails.upload_evidence}
showPreviewsInDropzone={false}
useChipsForPreview
previewGridProps={{ container: { spacing: 1, } }}
dropzoneText='Upload Evidence'
previewText=""
onChange={(file) => handleChangeFile(file[0], index)}
/>
</div>
style={{ pointerEvents: checkIfConditionIsFulfilled? "none" : "all }}
You will need to write checkIfConditionIsFulfilled on the basis of your states.
答案2
得分: 0
添加一个状态变量来保存字段的值,以及一个初始状态,其中上传按钮被禁用。
const [fieldValues, setFieldValues] = useState({ party1: '', party2: '', partyRelation: '', isUploadButtonDisabled: true });
更新handleParties
和handleRelation
函数以更新字段的值并检查是否所有字段都已填写:
const handleParties = (event, newValue, index, partyType) => {
setFieldValues(prevValues => ({
...prevValues,
[partyType]: newValue ? newValue.details.name : ''
}));
};
const handleRelation = (event, newValue, index) => {
setFieldValues(prevValues => ({
...prevValues,
partyRelation: newValue ? newValue.value : ''
}));
};
更新JSX:
<Button disabled={fieldValues.party1 === '' || fieldValues.party2 === '' || fieldValues.partyRelation === ''}>上传</Button>
希望对你有所帮助! ^^
英文:
Add a state variable to hold the field values and an initial state where the upload button is disabled
const [fieldValues, setFieldValues] = useState({ party1: '',party2: '', partyRelation: '', isUploadButtonDisabled: true });
Update the handleParties and handleRelation functions to update the field values and check if all fields are filled:
const handleParties = (event, newValue, index, partyType) => {
setFieldValues(prevValues => ({
...prevValues,
[partyType]: newValue ? newValue.details.name : ''
}));
};
const handleRelation = (event, newValue, index) => {
setFieldValues(prevValues => ({
...prevValues,
partyRelation: newValue ? newValue.value : ''
}));
};
Update the JSX:
<Button disabled={ fieldValues.party1 === '' || fieldValues.party2 === '' || fieldValues.partyRelation === '' }> Upload </Button>
Hope it helps!! ^^
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论