upload button should not allow upload if fields are empty

huangapple go评论129阅读模式
英文:

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?

 &lt;div className=&#39;product&#39;&gt;       
&lt;&gt;
{apiFetched &amp;&amp; relatedPartyDetails?.map((party, index) =&gt; (
&lt;Row key={index}&gt;
&lt;&gt;
&lt;Col lg={3}&gt;
&lt;Autocomplete
options={names}
getOptionLabel={(option) =&gt; (option.details?.name)}
id={&quot;disable-clearable-buyer-&quot; + index}
label=&quot;Party&quot;
renderInput={(params) =&gt; (
&lt;TextField {...params} label=&quot;Party 1&quot; variant=&quot;standard&quot; /&gt;
)}
onChange={(event, newValue) =&gt; {
console.log(&#39;test1&#39;);
// names.forEach((ele) =&gt; { 
//     console.log(ele.details.name);
//     console.log(&#39;hurre&#39;,party.buyer==ele.details.name);
console.log(party.buyer);
// })
// console.log(names.find((ele) =&gt; ele.details.name == party.buyer))
handleParties(event, newValue,index,&#39;buyer&#39;);
}}
disabled={isView}
value={names.find((ele) =&gt; ele.details.name === party.buyer)}
disableClearable
/&gt;
{error &amp;&amp; error?.buyer &amp;&amp; &lt;span style={{ color: &#39;red&#39; }}&gt;{error.buyer}&lt;/span&gt;}
&lt;/Col&gt;
&lt;Col lg={3}&gt;
&lt;Autocomplete
options={names}
getOptionLabel={(option) =&gt; ( option.details?.name)}
id={&quot;disable-clearable-shipper-&quot; + index}
label=&quot;Party&quot;
renderInput={(params) =&gt; (
&lt;TextField {...params} label=&quot;Party 2&quot; variant=&quot;standard&quot; /&gt;
)}
onChange={(event, newValue) =&gt; {
handleParties(event, newValue, index,&#39;shipper&#39;);
}}
disabled={isView}
value={(names &amp;&amp; party.shipper) &amp;&amp; names.find((ele) =&gt; ele.details.name === party.shipper)}
disableClearable
/&gt;
{error &amp;&amp; error?.shipper &amp;&amp; &lt;span style={{ color: &#39;red&#39; }}&gt;{error.shipper}&lt;/span&gt;}
&lt;/Col&gt;
&lt;Col lg={4}&gt;
&lt;div className=&#39;d-flex align-items-center Related_parties&#39;&gt;
&lt;p className=&#39;mb-0 title-color&#39;&gt;Relation&lt;/p&gt;
&lt;Autocomplete
className=&#39;ms-3 mb-3&#39;
options={[...parties]}
getOptionLabel={(option) =&gt; option.label}
id={&quot;disable-clearable-relation-party-&quot; + party.party_relation}
label=&quot;Party Relation&quot;
renderInput={(params) =&gt; (
&lt;TextField {...params} label=&quot;Party Relation &quot; variant=&quot;standard&quot; /&gt;
)}
defaultValue={relatedPartyDetails.party_relation}
getOptionSelected={(option) =&gt; option.label === &#39;test&#39;}
onChange={(event, newValue) =&gt; { handleRelation(event, newValue, index); setRelation(parties); console.log(&#39;parties&#39;, parties);console.log(&#39;party&#39;, party); }}
value={parties.find((ele) =&gt; ele.value == party.party_relation)}
disableClearable
/&gt;
&lt;/div&gt;
{error &amp;&amp; error?.party_relation &amp;&amp; &lt;span style={{ color: &#39;red&#39; }}&gt;{error.party_relation}&lt;/span&gt;}
&lt;/Col&gt;
{relation &amp;&amp; &lt;Col lg={2}&gt;
&lt;div className=&#39;drag-and-drop&#39;&gt;
&lt;DropzoneArea
Icon=&quot;none&quot;
filesLimit={1}
showPreviews={true}
defaultValue={relatedPartyDetails.upload_evidence}
showPreviewsInDropzone={false}
useChipsForPreview
previewGridProps={{ container: { spacing: 1, } }}
dropzoneText=&#39;Upload Evidence&#39;
previewText=&quot;&quot;
onChange={(file) =&gt; handleChangeFile(file[0], index)}
/&gt;
&lt;/div&gt;
{error &amp;&amp; error?.upload_evidence &amp;&amp; &lt;span style={{ color: &#39;red&#39; }}&gt;{error.upload_evidence}&lt;/span&gt;}
&lt;/Col&gt;}
&lt;/&gt;
&lt;/Row&gt;
))}
&lt;/&gt;
&lt;/div&gt;
&lt;/div&gt;

答案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

 &lt;div className=&#39;drag-and-drop&#39; style={{ pointerEvents: checkIfConditionIsFulfilled? &quot;none&quot; : &quot;all }} &gt;
&lt;DropzoneArea
Icon=&quot;none&quot;
filesLimit={1}
showPreviews={true}
defaultValue={relatedPartyDetails.upload_evidence}
showPreviewsInDropzone={false}
useChipsForPreview
previewGridProps={{ container: { spacing: 1, } }}
dropzoneText=&#39;Upload Evidence&#39;
previewText=&quot;&quot;
onChange={(file) =&gt; handleChangeFile(file[0], index)}
/&gt;
&lt;/div&gt;
style={{ pointerEvents: checkIfConditionIsFulfilled? &quot;none&quot; : &quot;all }}

You will need to write checkIfConditionIsFulfilled on the basis of your states.

答案2

得分: 0

添加一个状态变量来保存字段的值,以及一个初始状态,其中上传按钮被禁用。

const [fieldValues, setFieldValues] = useState({ party1: '', party2: '', partyRelation: '', isUploadButtonDisabled: true });

更新handlePartieshandleRelation函数以更新字段的值并检查是否所有字段都已填写:

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: &#39;&#39;,party2: &#39;&#39;, partyRelation: &#39;&#39;, 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) =&gt; {
setFieldValues(prevValues =&gt; ({
...prevValues,
[partyType]: newValue ? newValue.details.name : &#39;&#39;
}));
};
const handleRelation = (event, newValue, index) =&gt; {
setFieldValues(prevValues =&gt; ({
...prevValues,
partyRelation: newValue ? newValue.value : &#39;&#39;
}));
};

Update the JSX:

&lt;Button disabled={ fieldValues.party1 === &#39;&#39; || fieldValues.party2 === &#39;&#39; || fieldValues.partyRelation === &#39;&#39; }&gt; Upload &lt;/Button&gt;

Hope it helps!! ^^

huangapple
  • 本文由 发表于 2023年6月26日 14:42:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76554118.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定