Skip to content

Commit 0670e5b

Browse files
author
Mike Ngu
committed
initial commit
1 parent ad95c2b commit 0670e5b

23 files changed

+3059
-57
lines changed

package-lock.json

+2,015-46
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+9-1
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@
55
"main": "index.js",
66
"scripts": {
77
"start": "webpack-dev-server --config webpack/webpack.config.js --env.env=dev",
8+
"style-guide": "styleguidist server",
9+
"build-guide": "styleguidist build",
810
"build": "webpack --config webpack/webpack.config.js --env.env=prod",
9-
"build:analyze": "npm run build -- --env.plugins=bundleanalyze --env.plugins=bundlevisualizer"
11+
"build:analyze": "npm run build -- --env.plugins=bundleanalyze --env.plugins=bundlevisualizer",
12+
"lint-fix": "eslint src/ --fix"
1013
},
1114
"author": "Mike Ngu",
1215
"license": "MIT",
@@ -27,6 +30,7 @@
2730
"postcss-loader": "^3.0.0",
2831
"postcss-preset-env": "^6.7.0",
2932
"prop-types": "^15.7.2",
33+
"react-styleguidist": "^10.6.0",
3034
"style-loader": "^1.1.2",
3135
"terser-webpack-plugin": "^2.3.2",
3236
"webpack": "^4.41.5",
@@ -37,6 +41,10 @@
3741
"webpack-visualizer-plugin": "^0.1.11"
3842
},
3943
"dependencies": {
44+
"@material-ui/core": "^4.8.3",
45+
"@material-ui/icons": "^4.5.1",
46+
"daterangepicker": "^3.0.5",
47+
"jquery": "^3.4.1",
4048
"react": "^16.12.0",
4149
"react-dom": "^16.12.0"
4250
}

src/App.js

-6
This file was deleted.

src/Date/DatePicker.js

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import {useMemo, useEffect} from 'react'
2+
import PropTypes from 'prop-types'
3+
import 'daterangepicker'
4+
import './daterangepicker.css'
5+
import $ from 'jquery'
6+
import moment from 'moment'
7+
8+
const isDateValid = date => date && date.isValid && date.isValid()
9+
10+
const DatePicker = props => {
11+
const {
12+
applyLabel,
13+
cancelLabel,
14+
changeShowPicker,
15+
dropDirection,
16+
elementId,
17+
format,
18+
handleOnChange,
19+
name,
20+
single,
21+
startDate,
22+
timePicker
23+
} = props
24+
25+
const initializePicker = useMemo(() => {
26+
return () => {
27+
const $input = $(`#${elementId}`)
28+
29+
$input.daterangepicker(
30+
{
31+
autoUpdateInput: false,
32+
drops: dropDirection,
33+
showDropdowns: true,
34+
singleDatePicker: single,
35+
startDate,
36+
timePicker,
37+
locale: {
38+
applyLabel: applyLabel,
39+
cancelLabel: cancelLabel,
40+
format: format
41+
}
42+
},
43+
(start, end) => {
44+
if (!single && isDateValid(start) && isDateValid(end)) {
45+
handleOnChange({
46+
name,
47+
value: `${start.format(format)} - ${end.format(format)}`
48+
})
49+
} else if (isDateValid(start) && single) {
50+
handleOnChange({
51+
name,
52+
value: start.format(format)
53+
})
54+
}
55+
}
56+
)
57+
58+
$input.on(
59+
'hide.daterangepicker',
60+
() => {
61+
changeShowPicker(false)
62+
}
63+
)
64+
}
65+
// eslint-disable-next-line
66+
}, [applyLabel, cancelLabel, changeShowPicker, dropDirection, elementId, format, handleOnChange, name, single, startDate, timePicker])
67+
68+
useEffect(() => {
69+
initializePicker()
70+
return () => {
71+
const $portal = $('.daterangepicker')
72+
if ($portal) $portal.remove()
73+
const $input = $(`#${elementId}`)
74+
if ($input) $input.off()
75+
}
76+
}, [elementId, initializePicker])
77+
78+
return null
79+
}
80+
81+
export default DatePicker
82+
83+
DatePicker.propTypes = {
84+
elementId: PropTypes.string,
85+
handleOnChange: PropTypes.func,
86+
changeShowPicker: PropTypes.func,
87+
dropDirection: PropTypes.string,
88+
name: PropTypes.string,
89+
timePicker: PropTypes.bool,
90+
startDate: PropTypes.instanceOf(moment),
91+
format: PropTypes.string
92+
}

0 commit comments

Comments
 (0)