Search code examples
cssreactjsclassname

Adding a className if a condition is true


i have a form with multiple fields and a submit button. once i click the submit button it will come to this filterProperties function. i have a if condition inside that filterProperties function. so in that if condition i am checking whether the check-out date is null. if the if-condition is true i want to add has-error class to the datepicker. if anyone knows how to do this please help.

      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>

Solution

  • Try this-

    <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}
                                />