Skip to content

Commit

Permalink
range calendar attached to input
Browse files Browse the repository at this point in the history
  • Loading branch information
Prasidha Karki committed Aug 12, 2020
1 parent 80a45d2 commit 311d46a
Show file tree
Hide file tree
Showing 4 changed files with 332 additions and 11 deletions.
17 changes: 10 additions & 7 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import { NepaliDatePicker } from './nepali_date_picker/index.js'
import moment from 'moment';
import NepaliCalendarRange from './nepali_date_picker/calendar_range';
import { getCalendarType, get_ad_bs_listener } from './nepali_date_picker/ad_bs_date_render';
import { Switch } from 'antd';
import { Switch, Space } from 'antd';
// import 'antd/dist/antd.css';
import NepaliRangeInputPicker from './nepali_date_picker/range_input_picker';

class App extends React.Component {
constructor(props) {
Expand All @@ -19,7 +21,10 @@ class App extends React.Component {
render() {

return (
<div >
<Space direction='vertical' size={40} style={{
width:'100%',
padding:80
}}>
<Switch checked={this.state.checked}
unCheckedChildren="AD"
checkedChildren="BS"
Expand All @@ -37,8 +42,6 @@ class App extends React.Component {
checked: checked
})
}}></Switch>
<br></br>
<br></br>
<NepaliDatePicker
value={this.state.date}
onChange={(val) => {
Expand All @@ -52,13 +55,13 @@ class App extends React.Component {
// }
// }}
/>
<br></br>
<NepaliRangeInputPicker />

<NepaliCalendarRange />
<br></br>
{/* <NepaliDatePicker /> */}


</div>
</Space>
);
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/nepali_date_picker/index.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import React, { Component } from 'react'
import PropTypes from 'prop-types'
import 'antd/lib/input/style/css'
import 'antd/lib/popover/style/css'
import './nepali_date_picker.css'
import NepaliCalendar from './calendar.js'
import { calendarFunctions, calendarData } from './helper_bs.js'
import { Input, Popover } from 'antd';
import 'antd/lib/input/style/css'
import 'antd/lib/popover/style/css'

import moment from 'moment';
import { getCalendarType, get_ad_bs_listener, padDateMonth } from './ad_bs_date_render'
import CalendarIcon from './assets/calendar.svg';


/**
* Represents a book.
* @constructor
* @augments {Component<Props, State>}
Expand Down
16 changes: 15 additions & 1 deletion src/nepali_date_picker/nepali_date_picker.css
Original file line number Diff line number Diff line change
Expand Up @@ -215,4 +215,18 @@
display: flex;
flex-wrap: nowrap;
width: fit-content;
}
}


.rl-nepali-rangepicker-wrapper .input-split {
background-color: #fff;
}

.rl-nepali-rangepicker-wrapper .input-right {
border-left-width: 0;
}

.rl-nepali-rangepicker-wrapper .input-right:hover,
.rl-nepali-rangepicker-wrapper .input-right:focus {
border-left-width: 1px;
}
304 changes: 304 additions & 0 deletions src/nepali_date_picker/range_input_picker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,304 @@
import React, { Component } from 'react';
import { Input, Popover } from 'antd';
import { dateObjectToString, adDateObjectToMoment, getCalendarType, padDateMonth, adDateStringToObject } from './ad_bs_date_render';
import moment from 'moment';
import { calendarFunctions, calendarData } from './helper_bs';
import CalendarIcon from './assets/calendar.svg';
import NepaliCalendarForRange from './calendar_for_range';


/**
* @constructor
* @augments {Component<Props, State>}
*/


class NepaliRangeInputPicker extends Component {
constructor(props) {
super(props)

this.state = {
selected_date_1: '4-9-2020',
selected_date_2: '8-10-2020',
focused1: false,
focused2: false,
temp_value1: "",
temp_value2: "",
calendarVisible: false,
calendarType: props.calendarType
}

this.separator = props.separator || " ";
// this.size=props.size||""
let ad_date = moment();
this.ad_extras = {
day: ad_date.date(),
month: ad_date.month() + 1,
year: ad_date.year(),
};
let bs_date = calendarFunctions.getBsDateByAdDate(ad_date.year(), ad_date.month() + 1, ad_date.date())
this.bs_extras = {
day: bs_date.bsDate,
month: bs_date.bsMonth,
year: bs_date.bsYear,
}
}


onDateSelect = (ad_date) => {
if (this.state.selected_date_2 && this.state.selected_date_1) {
// Both date selected, so set to date 1
this.setState({
selected_date_1: dateObjectToString(ad_date),
selected_date_2: null
})
}
else
if (this.state.selected_date_1) {
// Date 1 selected, so select date 2
let dt_1_m = moment(this.state.selected_date_1, "DD-MM-YYYY");
let dt_2_m = adDateObjectToMoment(ad_date);
let new_dt_group = {
selected_date_1: this.state.selected_date_1,
selected_date_2: null
}
if (dt_1_m == dt_2_m) {
new_dt_group.selected_date_2 = this.state.selected_date_1
// same date selected
} else if (dt_1_m < dt_2_m) {
new_dt_group.selected_date_2 = dateObjectToString(ad_date)
// selected date is greater than

} else {
//selected date is less than date 1
new_dt_group.selected_date_1 = dateObjectToString(ad_date);
new_dt_group.selected_date_2 = this.state.selected_date_1
}

this.setState({
...new_dt_group
})

} else {
this.setState({
selected_date_1: dateObjectToString(ad_date),
selected_date_2: null
})

}
}
render() {

const { selected_date_1, selected_date_2, calendarType, focused1, focused2, temp_value1, temp_value2, calendarVisible } = this.state;

let rendering_val_1 = selected_date_1;
let rendering_val_2 = selected_date_2;
if (focused1) {
rendering_val_1 = temp_value1
}
if (focused2) {
rendering_val_2 = temp_value2
}
if (calendarType == "BS" && moment(selected_date_1, "DD-MM-YYYY").isValid() && !focused1) {
let adDateObj = moment(selected_date_1, "DD-MM-YYYY");
let bsDateObj = calendarFunctions.getBsDateByAdDate(adDateObj.year(), adDateObj.month() + 1, adDateObj.date());
rendering_val_1 = (`${padDateMonth(bsDateObj.bsDate)}-${padDateMonth(bsDateObj.bsMonth)}-${(bsDateObj.bsYear)}`)
}
if (calendarType == "BS" && moment(selected_date_2, "DD-MM-YYYY").isValid() && !focused2) {
let adDateObj = moment(selected_date_2, "DD-MM-YYYY");
let bsDateObj = calendarFunctions.getBsDateByAdDate(adDateObj.year(), adDateObj.month() + 1, adDateObj.date());
rendering_val_2 = (`${padDateMonth(bsDateObj.bsDate)}-${padDateMonth(bsDateObj.bsMonth)}-${(bsDateObj.bsYear)}`)
}



return (
<>
<div>{JSON.stringify(this.state)}</div>
<div className='rl-nepali-rangepicker-wrapper'>
<Input.Group compact style={{
display: 'flex'
}}>
<Input
ref={(r) => {
this.left_inp = r;
}}
onFocus={() => {
this.setState({
focused1: true
})
}}
onBlur={() => {
this.setState({
focused1: false
})
}}
value={rendering_val_1}
className='input-left'
placeholder="Date From"
onChange={(e) => {
this.setState({
temp_value1: e.target.value
})
}}
onKeyDown={(e) => {
console.log("key down", e.key)
if (e.key == 'Enter' || e.key == 'Tab') {
let _temp_value = this.state.temp_value1;

// _temp_value.split("-");
const temp_value = _temp_value.split(this.separator).map((it) => parseInt(it))
console.log("temp", temp_value, this.ad_extras, this.bs_extras)
// temp_value=temp_value
let _day = (temp_value[0] && temp_value[0] > 0 && temp_value[0] <= (calendarType == "AD" ? 31 : 32)) ? temp_value[0] : calendarType == "AD" ? this.ad_extras.day : this.bs_extras.day;
let _month = (temp_value[1] && temp_value[1] > 0 && temp_value[1] <= 12) ? temp_value[1] : calendarType == "AD" ? this.ad_extras.month : this.bs_extras.month;
let _year = (temp_value[2] && temp_value[2] > (calendarData.minBsYear - (calendarType == "AD" ? 57 : 0)) && temp_value[2] <= (calendarData.maxBsYear - (calendarType == "AD" ? 57 : 0))) ? temp_value[2] : calendarType == "AD" ? this.ad_extras.year : this.bs_extras.year;
console.log("new temp should be", {
_day, _month, _year, calendarType
})
let _new_selected_date = ""
if (calendarType == "AD") {
let new_date = moment().date(_day).month(_month - 1).year(_year);
_new_selected_date = new_date.format("DD-MM-YYYY");
this.onDateSelect(adDateStringToObject(_new_selected_date))
} else {
let respectiveADObject = calendarFunctions.getAdDateObjectByBsDate(_year, _month, _day);
let new_date = moment().date(respectiveADObject.adDate).month(respectiveADObject.adMonth - 1).year(respectiveADObject.adYear);
_new_selected_date = new_date.format("DD-MM-YYYY");
this.onDateSelect(adDateStringToObject(_new_selected_date))
}

this.left_inp.blur();
this.right_inp.focus()
this.setState({
temp_value1: "",
// selected_date: _new_selected_date
}, () => {
// typeof this.props.onChange === 'function' && this.props.onChange(_new_selected_date)
})
}
}}
/>
<Input
className="input-split"
style={{
width: 50,
borderLeft: 0,
borderRight: 0,
pointerEvents: 'none',
}}
placeholder="~"
disabled
/>
<Input
ref={(r) => {
this.right_inp = r;
}}
onFocus={() => {
this.setState({
focused2: true
})
}}
onBlur={() => {
this.setState({
focused2: false
})
}}
value={rendering_val_2}
onChange={(e) => {
this.setState({
temp_value2: e.target.value
})
}}
onKeyDown={(e) => {
console.log("key down", e.key)
if (e.key == 'Enter' || e.key == 'Tab') {
let _temp_value = this.state.temp_value2;

// _temp_value.split("-");
const temp_value = _temp_value.split(this.separator).map((it) => parseInt(it))
console.log("temp", temp_value, this.ad_extras, this.bs_extras)
// temp_value=temp_value
let _day = (temp_value[0] && temp_value[0] > 0 && temp_value[0] <= (calendarType == "AD" ? 31 : 32)) ? temp_value[0] : calendarType == "AD" ? this.ad_extras.day : this.bs_extras.day;
let _month = (temp_value[1] && temp_value[1] > 0 && temp_value[1] <= 12) ? temp_value[1] : calendarType == "AD" ? this.ad_extras.month : this.bs_extras.month;
let _year = (temp_value[2] && temp_value[2] > (calendarData.minBsYear - (calendarType == "AD" ? 57 : 0)) && temp_value[2] <= (calendarData.maxBsYear - (calendarType == "AD" ? 57 : 0))) ? temp_value[2] : calendarType == "AD" ? this.ad_extras.year : this.bs_extras.year;
console.log("new temp should be", {
_day, _month, _year, calendarType
})
let _new_selected_date = ""
if (calendarType == "AD") {
let new_date = moment().date(_day).month(_month - 1).year(_year);
_new_selected_date = new_date.format("DD-MM-YYYY");
this.onDateSelect(adDateStringToObject(_new_selected_date))
} else {
let respectiveADObject = calendarFunctions.getAdDateObjectByBsDate(_year, _month, _day);
let new_date = moment().date(respectiveADObject.adDate).month(respectiveADObject.adMonth - 1).year(respectiveADObject.adYear);
_new_selected_date = new_date.format("DD-MM-YYYY");
this.onDateSelect(adDateStringToObject(_new_selected_date))
}

this.right_inp.blur();
this.setState({
temp_value2: "",
// selected_date: _new_selected_date
}, () => {
// typeof this.props.onChange === 'function' && this.props.onChange(_new_selected_date)
})
}
}}
className='input-right'
placeholder="Date To"

suffix={<Popover overlayClassName='popovercalendar'
visible={calendarVisible}
onVisibleChange={(visible) => {
this.setState({ calendarVisible: visible })
}}
trigger='click' placement='bottomRight'
content={<div className='rl-range-calendar'>
<NepaliCalendarForRange
selected_date_1={this.state.selected_date_1}
initialDate={this.state.selected_date_1}
// disableDate={(d)=>{
// return this.state.selected_date_1&&d<moment(this.state.selected_date_1,"DD-MM-YYYY")
// }}
selected_date_2={this.state.selected_date_2}
onSelect={(ad_date) => {
this.onDateSelect(ad_date)
}}
calendarFor={1}
showToday={false} />
<NepaliCalendarForRange
selected_date_1={this.state.selected_date_1}
initialDate={this.state.selected_date_2 == null ? this.state.selected_date_1 : this.state.selected_date_2}
// disableDate={(d)=>{
// return this.state.selected_date_1&&d<moment(this.state.selected_date_1,"DD-MM-YYYY")
// }}
selected_date_2={this.state.selected_date_2}
onSelect={(ad_date) => {
this.onDateSelect(ad_date)
}}
calendarFor={2}
showToday={false} />
</div>}>
<img alt='calendar' onClick={() => {
this.setState({
calendarVisible: true
})
}} className='rl-nepali-datepicker-icon hand-cursor' src={CalendarIcon} />
</Popover>}
/>
</Input.Group>
</div>
</>
);
}
}
NepaliRangeInputPicker.defaultProps = {
separator: ' ',
size: 'small',
calendarType: getCalendarType()
}

export default NepaliRangeInputPicker;

0 comments on commit 311d46a

Please sign in to comment.