[英]Adding a className if a condition is true
我有一個包含多個字段和一個提交按鈕的表單。 一旦我點擊提交按鈕,它就會來到這個filterProperties
函數。 我在那個filterProperties
函數中有一個 if 條件。 因此,如果條件我正在檢查退房日期是否為空。 如果 if 條件為真,我想將 has-error 類添加到日期選擇器。 如果有人知道如何做到這一點,請幫忙。
filterProperties = () => {
let checkin = this.state.checkin;
let checkout = this.state.checkout;
let guestCount = this.state.guest_count;
let cityID = this.state.selectedcityID;
let budget = parseFloat(this.state.textthree).toFixed(2);
if (checkin !== "" && checkout === '') {
alert('plz enter the check out date');
}
const cityProperties = {
checkin: checkin,
checkout: checkout,
guestCount: guestCount,
cityID: cityID,
budget: budget
}
console.log(cityProperties);
this.props.getCityProperties(cityProperties);
}
<FormItem style={{ marginBottom: '0' }} label="Check Out">
<span>
{getFieldDecorator('check_out', {
rules: [
{
required: true,
message: 'Please select a Check Out Date',
}, {
validator: this.checkCheckoutDates,
}
],
})(<DatePicker
className={className}
disabled={(this.state.checkin === '')}
disabledDate={this.disabledDateCheckout}
onChange={(e) => this.dateSelected(e, 'checkout')}
defaultValue={moment(new Date((new Date()).valueOf() + 1000 * 60 * 60 * 24))}
format={dateFormat}
/>)}
</span>
</FormItem>
嘗試這個-
<DatePicker
className={(this.state.checkin !== "" && this.state.checkout === "")? "class-name": ""}
disabled={(this.state.checkin === '')}
disabledDate={this.disabledDateCheckout}
onChange={(e) => this.dateSelected(e, 'checkout')}
defaultValue={moment(new Date((new Date()).valueOf() + 1000 * 60 * 60 * 24))}
format={dateFormat}
/>
你可以使用這樣的東西
<Element className={`${this.state.flag ? className : ''}`} />
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.