Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ///Problemem w tym kodzie jest bardzo duża ilość inputów(42) i nie miałem pomysłu w jaki sposób obsłużyc ich zmiany.
- Na dole przykład kodu w jaki sposób udało mi się to skrócić ale czuję, że nie jest to optymalne
- ///handleChange
- handleChange(date, i, name) {
- if (name == `openAt_${i}`) {
- let temp = this.state.parkingPrices;
- temp[i].PStart = date;
- this.setState({ parkingPrices: temp });
- this.handleValidation();
- }
- if (name == `closeAt_${i}`) {
- let temp = this.state.parkingPrices;
- temp[i].PEnd = date;
- this.setState({ parkingPrices: temp });
- this.handleValidation();
- }
- if (name == `hourlyRate_${i}`) {
- date.target.value = date.target.value.replace(/,/g, '.')
- let temp = this.state.parkingPrices;
- temp[i].HourlyRate = date.target.value
- this.setState({ parkingPrices: temp })
- this.handleValidation();
- }
- if (name == `allDayRate_${i}`) {
- date.target.value = date.target.value.replace(/,/g, '.')
- let temp = this.state.parkingPrices;
- temp[i].DailyRate = date.target.value
- this.setState({ parkingPrices: temp })
- this.handleValidation();
- }
- }
- handleHourlyCheckBoxChange = (i) => {
- let temp = this.state.parkingPrices;
- let type = temp[i].Type;
- if(type == PricingType.HourlyRate)
- {
- temp[i].Type = PricingType.Closed;
- temp[i].IsActive = 0;
- }
- else if (type == PricingType.DailyRate)
- {
- temp[i].Type = PricingType.Unset;
- temp[i].IsActive = 1;
- }
- else if (type == PricingType.Unset)
- {
- temp[i].Type = PricingType.DailyRate;
- temp[i].IsActive = 1;
- }
- this.setState({ parkingPrices: temp});
- }
- handleDailyCheckBoxChange = (i) => {
- let temp = this.state.parkingPrices;
- let type = temp[i].Type;
- if(type == PricingType.HourlyRate)
- {
- temp[i].Type = PricingType.Unset;
- temp[i].IsActive = 1;
- }
- else if (type == PricingType.DailyRate)
- {
- temp[i].Type = PricingType.Closed;
- temp[i].IsActive = 0;
- }
- else if (type == PricingType.Unset)
- {
- temp[i].Type = PricingType.HourlyRate;
- temp[i].IsActive = 1;
- }
- this.setState({ parkingPrices: temp });
- }
- ///RENDER
- {this.state.parkingPrices.map((row, i) =>
- <tr key={`trowid_${i}`} id={`trowid_${i}`}>
- <td key={`chbid_${i}`} className="row CheckBox">
- <input type="checkbox" style={{ float: "left" }}
- className="form-check-input daysChb"
- defaultChecked={row.IsActive == 1 && row.Type != PricingType.Closed}
- checked={row.IsActive == 1 && row.Type != PricingType.Closed}
- onChange={() => this.handleCheckboxChange(i)} id={`checkbxid${i}`} />
- <span className="weekDay">{WeekDay[row.WeekDay]}</span>
- </td>
- <td key={`datepicker_${i}`} className="datePicker">
- <DatePicker
- selected={Moment.utc(row.PStart)}
- onChange={(date) => this.handleChange(date, i, `openAt_${i}`)}
- showTimeSelect
- showTimeSelectOnly
- name={`openAt_${i}`}
- timeIntervals={60}
- disabled={row.IsActive == 0}
- dateFormat="LT"
- minTime={Moment().hours(0).minutes(0)}
- maxTime={Moment.utc(row.PEnd).add(-1,'hours')}
- className="form-control datePicker "
- readOnly
- />
- </td>
- <td key={`datepicker2_${i}`} className="datePicker">
- <DatePicker
- selected={Moment.utc(row.PEnd)}
- onChange={(date) => this.handleChange(date, i, `closeAt_${i}`)}
- showTimeSelect
- showTimeSelectOnly
- name={`closeAt_${i}`}
- disabled={row.IsActive == 0}
- timeIntervals={60}
- dateFormat="LT"
- minTime={Moment.utc(row.PStart).add(1,'hours')}
- maxTime={Moment().hours(23).minutes(0)}
- className="form-control datePicker"
- readOnly
- />
- </td>
- <td key={`hourly_${i}`}>
- <input type="checkbox"
- className="form-check-input"
- disabled={row.IsActive == 0 || row.Type == PricingType.Closed}
- checked={(row.Type == PricingType.HourlyRate || row.Type == PricingType.Unset) && row.IsActive }
- onChange={() => this.handleHourlyCheckBoxChange(i)} />
- <span className="input-group pricesWithCheckbox">
- <span className="input-group-addon">$</span>
- <input
- defaultValue={String(row.HourlyRate)}
- value= {row.IsActive ==1 && (row.Type == PricingType.HourlyRate || row.Type == PricingType.Unset)? row.HourlyRate: ""}
- maxLength={7}
- disabled={row.IsActive == 0 || row.Type == PricingType.DailyRate || row.type == PricingType.Closed }
- style={this.state.parkingPrices[i].HourlyRate >= this.props.tezMin ? {} : invalid}
- type="text"
- onBlur={() =>this.formatHourlyValue(i)}
- onChange={(e) => this.handleChange(e, i, `hourlyRate_${i}`)}
- name={`hourlyRate_${i}`}
- onKeyUp={(e) => this.handleLength(e)}
- className={`form-control lowPaddInput priceRow${i}`} />
- </span>
- </td>
- <td key={`daily_${i}`}>
- <input type="checkbox"
- className="form-check-input"
- disabled={row.IsActive == 0 || row.Type == PricingType.Closed }
- defaultChecked={row.Type == PricingType.DailyRate || row.Type == PricingType.Unset}
- checked={(row.Type == PricingType.DailyRate || row.Type == PricingType.Unset) && row.IsActive }
- onChange={() => this.handleDailyCheckBoxChange(i)} />
- <span className="input-group pricesWithCheckbox">
- <span className="input-group-addon">$</span>
- <input type="text"
- onKeyUp={(e) => this.handleLength(e)}
- style={this.state.parkingPrices[i].DailyRate >= this.props.tezMin ? {} : invalid}
- value= {row.IsActive ==1? row.DailyRate: ""}
- maxLength={7}
- onBlur={() =>this.formatDailyValue(i)}
- disabled={row.IsActive == 0 || row.Type == PricingType.Closed || row.Type == PricingType.HourlyRate }
- onChange={(e) => this.handleChange(e, i, `allDayRate_${i}`)}
- name={`allDayRate_${i}`} className={`form-control lowPaddInput priceRow${i}`} />
- </span>
- </td>
- </tr>
- )}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement