@hitesh0009/react-native-datetime-picker 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +207 -0
- package/dist/Components/Wheel.d.ts +4 -0
- package/dist/Components/Wheel.d.ts.map +1 -0
- package/dist/Components/Wheel.js +104 -0
- package/dist/DataTimePicker.d.ts +3 -0
- package/dist/DataTimePicker.d.ts.map +1 -0
- package/dist/DataTimePicker.js +94 -0
- package/dist/Utils/month_name.d.ts +4 -0
- package/dist/Utils/month_name.d.ts.map +1 -0
- package/dist/Utils/month_name.js +23 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1 -0
- package/dist/types.d.ts +52 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +1 -0
- package/hitesh0009-react-native-datetime-picker-1.0.0.tgz +0 -0
- package/package.json +265 -0
- package/src/Components/Wheel.tsx +172 -0
- package/src/DataTimePicker.tsx +270 -0
- package/src/Utils/month_name.ts +25 -0
- package/src/index.ts +9 -0
- package/src/types.ts +59 -0
- package/tsconfig.json +26 -0
package/package.json
ADDED
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@hitesh0009/react-native-datetime-picker",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "A fully customizable wheel-based Date & Time picker for React Native with dynamic month handling, infinite scrolling wheels, and optional start/end date clamping.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"react-native",
|
|
7
|
+
"react-native-date-picker",
|
|
8
|
+
"react-native-time-picker",
|
|
9
|
+
"react-native-wheel-picker",
|
|
10
|
+
"react-native-wheel-date-picker",
|
|
11
|
+
"react-native-datetime-picker",
|
|
12
|
+
"react-native-scroll-picker",
|
|
13
|
+
"react-native-infinite-scroll",
|
|
14
|
+
"wheel-picker",
|
|
15
|
+
"date-time",
|
|
16
|
+
"datetime",
|
|
17
|
+
"calendar",
|
|
18
|
+
"custom-date-picker",
|
|
19
|
+
"mobile-ui",
|
|
20
|
+
"react-native-ui"
|
|
21
|
+
],
|
|
22
|
+
"homepage": "https://github.com/hiteshjangir0009/react-native-datetime-picker#readme",
|
|
23
|
+
"bugs": {
|
|
24
|
+
"url": "https://github.com/hiteshjangir0009/react-native-datetime-picker/issues"
|
|
25
|
+
},
|
|
26
|
+
"repository": {
|
|
27
|
+
"type": "git",
|
|
28
|
+
"url": "git+https://github.com/hiteshjangir0009/react-native-datetime-picker.git"
|
|
29
|
+
},
|
|
30
|
+
"license": "MIT",
|
|
31
|
+
"author": "Hitesh Jangir",
|
|
32
|
+
"type": "module",
|
|
33
|
+
"exports": {
|
|
34
|
+
".": {
|
|
35
|
+
"import": "./dist/index.js",
|
|
36
|
+
"types": "./dist/index.d.ts"
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
"main": "dist/index.js",
|
|
40
|
+
"types": "dist/index.d.ts",
|
|
41
|
+
"scripts": {
|
|
42
|
+
"build": "tsc",
|
|
43
|
+
"prepublishOnly": "npm run build"
|
|
44
|
+
},
|
|
45
|
+
"dependencies": {
|
|
46
|
+
"abort-controller": "^3.0.0",
|
|
47
|
+
"accepts": "^1.3.8",
|
|
48
|
+
"acorn": "^8.15.0",
|
|
49
|
+
"agent-base": "^7.1.4",
|
|
50
|
+
"anser": "^1.4.10",
|
|
51
|
+
"ansi-regex": "^5.0.1",
|
|
52
|
+
"ansi-styles": "^4.3.0",
|
|
53
|
+
"anymatch": "^3.1.3",
|
|
54
|
+
"argparse": "^1.0.10",
|
|
55
|
+
"asap": "^2.0.6",
|
|
56
|
+
"babel-jest": "^29.7.0",
|
|
57
|
+
"babel-plugin-istanbul": "^6.1.1",
|
|
58
|
+
"babel-plugin-jest-hoist": "^29.6.3",
|
|
59
|
+
"babel-plugin-syntax-hermes-parser": "^0.32.0",
|
|
60
|
+
"babel-preset-current-node-syntax": "^1.2.0",
|
|
61
|
+
"babel-preset-jest": "^29.6.3",
|
|
62
|
+
"balanced-match": "^1.0.2",
|
|
63
|
+
"base64-js": "^1.5.1",
|
|
64
|
+
"baseline-browser-mapping": "^2.9.19",
|
|
65
|
+
"brace-expansion": "^1.1.12",
|
|
66
|
+
"braces": "^3.0.3",
|
|
67
|
+
"browserslist": "^4.28.1",
|
|
68
|
+
"bser": "^2.1.1",
|
|
69
|
+
"buffer-from": "^1.1.2",
|
|
70
|
+
"camelcase": "^5.3.1",
|
|
71
|
+
"caniuse-lite": "^1.0.30001769",
|
|
72
|
+
"chalk": "^4.1.2",
|
|
73
|
+
"chrome-launcher": "^0.15.2",
|
|
74
|
+
"chromium-edge-launcher": "^0.2.0",
|
|
75
|
+
"ci-info": "^3.9.0",
|
|
76
|
+
"cliui": "^8.0.1",
|
|
77
|
+
"color-convert": "^2.0.1",
|
|
78
|
+
"color-name": "^1.1.4",
|
|
79
|
+
"commander": "^12.1.0",
|
|
80
|
+
"concat-map": "^0.0.1",
|
|
81
|
+
"connect": "^3.7.0",
|
|
82
|
+
"convert-source-map": "^2.0.0",
|
|
83
|
+
"cross-spawn": "^7.0.6",
|
|
84
|
+
"csstype": "^3.2.3",
|
|
85
|
+
"debug": "^4.4.3",
|
|
86
|
+
"depd": "^2.0.0",
|
|
87
|
+
"destroy": "^1.2.0",
|
|
88
|
+
"ee-first": "^1.1.1",
|
|
89
|
+
"electron-to-chromium": "^1.5.286",
|
|
90
|
+
"emoji-regex": "^8.0.0",
|
|
91
|
+
"encodeurl": "^1.0.2",
|
|
92
|
+
"error-stack-parser": "^2.1.4",
|
|
93
|
+
"escalade": "^3.2.0",
|
|
94
|
+
"escape-html": "^1.0.3",
|
|
95
|
+
"escape-string-regexp": "^4.0.0",
|
|
96
|
+
"esprima": "^4.0.1",
|
|
97
|
+
"etag": "^1.8.1",
|
|
98
|
+
"event-target-shim": "^5.0.1",
|
|
99
|
+
"exponential-backoff": "^3.1.3",
|
|
100
|
+
"fast-json-stable-stringify": "^2.1.0",
|
|
101
|
+
"fb-dotslash": "^0.5.8",
|
|
102
|
+
"fb-watchman": "^2.0.2",
|
|
103
|
+
"fill-range": "^7.1.1",
|
|
104
|
+
"finalhandler": "^1.1.2",
|
|
105
|
+
"find-up": "^4.1.0",
|
|
106
|
+
"flow-enums-runtime": "^0.0.6",
|
|
107
|
+
"fresh": "^0.5.2",
|
|
108
|
+
"fs.realpath": "^1.0.0",
|
|
109
|
+
"gensync": "^1.0.0-beta.2",
|
|
110
|
+
"get-caller-file": "^2.0.5",
|
|
111
|
+
"get-package-type": "^0.1.0",
|
|
112
|
+
"glob": "^7.2.3",
|
|
113
|
+
"graceful-fs": "^4.2.11",
|
|
114
|
+
"has-flag": "^4.0.0",
|
|
115
|
+
"hermes-compiler": "^250829098.0.7",
|
|
116
|
+
"hermes-estree": "^0.32.0",
|
|
117
|
+
"hermes-parser": "^0.32.0",
|
|
118
|
+
"http-errors": "^2.0.1",
|
|
119
|
+
"https-proxy-agent": "^7.0.6",
|
|
120
|
+
"image-size": "^1.2.1",
|
|
121
|
+
"imurmurhash": "^0.1.4",
|
|
122
|
+
"inflight": "^1.0.6",
|
|
123
|
+
"inherits": "^2.0.4",
|
|
124
|
+
"invariant": "^2.2.4",
|
|
125
|
+
"is-docker": "^2.2.1",
|
|
126
|
+
"is-fullwidth-code-point": "^3.0.0",
|
|
127
|
+
"is-number": "^7.0.0",
|
|
128
|
+
"is-wsl": "^2.2.0",
|
|
129
|
+
"isexe": "^2.0.0",
|
|
130
|
+
"istanbul-lib-coverage": "^3.2.2",
|
|
131
|
+
"istanbul-lib-instrument": "^5.2.1",
|
|
132
|
+
"jest-environment-node": "^29.7.0",
|
|
133
|
+
"jest-get-type": "^29.6.3",
|
|
134
|
+
"jest-haste-map": "^29.7.0",
|
|
135
|
+
"jest-message-util": "^29.7.0",
|
|
136
|
+
"jest-mock": "^29.7.0",
|
|
137
|
+
"jest-regex-util": "^29.6.3",
|
|
138
|
+
"jest-util": "^29.7.0",
|
|
139
|
+
"jest-validate": "^29.7.0",
|
|
140
|
+
"jest-worker": "^29.7.0",
|
|
141
|
+
"js-tokens": "^4.0.0",
|
|
142
|
+
"js-yaml": "^3.14.2",
|
|
143
|
+
"jsc-safe-url": "^0.2.4",
|
|
144
|
+
"jsesc": "^3.1.0",
|
|
145
|
+
"json5": "^2.2.3",
|
|
146
|
+
"leven": "^3.1.0",
|
|
147
|
+
"lighthouse-logger": "^1.4.2",
|
|
148
|
+
"locate-path": "^5.0.0",
|
|
149
|
+
"lodash.throttle": "^4.1.1",
|
|
150
|
+
"loose-envify": "^1.4.0",
|
|
151
|
+
"lru-cache": "^5.1.1",
|
|
152
|
+
"makeerror": "^1.0.12",
|
|
153
|
+
"marky": "^1.3.0",
|
|
154
|
+
"memoize-one": "^5.2.1",
|
|
155
|
+
"merge-stream": "^2.0.0",
|
|
156
|
+
"metro": "^0.83.3",
|
|
157
|
+
"metro-babel-transformer": "^0.83.3",
|
|
158
|
+
"metro-cache": "^0.83.3",
|
|
159
|
+
"metro-cache-key": "^0.83.3",
|
|
160
|
+
"metro-config": "^0.83.3",
|
|
161
|
+
"metro-core": "^0.83.3",
|
|
162
|
+
"metro-file-map": "^0.83.3",
|
|
163
|
+
"metro-minify-terser": "^0.83.3",
|
|
164
|
+
"metro-resolver": "^0.83.3",
|
|
165
|
+
"metro-runtime": "^0.83.3",
|
|
166
|
+
"metro-source-map": "^0.83.3",
|
|
167
|
+
"metro-symbolicate": "^0.83.3",
|
|
168
|
+
"metro-transform-plugins": "^0.83.3",
|
|
169
|
+
"metro-transform-worker": "^0.83.3",
|
|
170
|
+
"micromatch": "^4.0.8",
|
|
171
|
+
"mime": "^1.6.0",
|
|
172
|
+
"mime-db": "^1.52.0",
|
|
173
|
+
"mime-types": "^2.1.35",
|
|
174
|
+
"minimatch": "^3.1.2",
|
|
175
|
+
"mkdirp": "^1.0.4",
|
|
176
|
+
"ms": "^2.1.3",
|
|
177
|
+
"negotiator": "^0.6.3",
|
|
178
|
+
"node-int64": "^0.4.0",
|
|
179
|
+
"node-releases": "^2.0.27",
|
|
180
|
+
"normalize-path": "^3.0.0",
|
|
181
|
+
"nullthrows": "^1.1.1",
|
|
182
|
+
"ob1": "^0.83.3",
|
|
183
|
+
"on-finished": "^2.3.0",
|
|
184
|
+
"once": "^1.4.0",
|
|
185
|
+
"open": "^7.4.2",
|
|
186
|
+
"p-limit": "^2.3.0",
|
|
187
|
+
"p-locate": "^4.1.0",
|
|
188
|
+
"p-try": "^2.2.0",
|
|
189
|
+
"parseurl": "^1.3.3",
|
|
190
|
+
"path-exists": "^4.0.0",
|
|
191
|
+
"path-is-absolute": "^1.0.1",
|
|
192
|
+
"path-key": "^3.1.1",
|
|
193
|
+
"picocolors": "^1.1.1",
|
|
194
|
+
"picomatch": "^2.3.1",
|
|
195
|
+
"pirates": "^4.0.7",
|
|
196
|
+
"pretty-format": "^29.7.0",
|
|
197
|
+
"promise": "^8.3.0",
|
|
198
|
+
"queue": "^6.0.2",
|
|
199
|
+
"range-parser": "^1.2.1",
|
|
200
|
+
"react": ">=17",
|
|
201
|
+
"react-devtools-core": "^6.1.5",
|
|
202
|
+
"react-is": "^18.3.1",
|
|
203
|
+
"react-refresh": "^0.14.2",
|
|
204
|
+
"regenerator-runtime": "^0.13.11",
|
|
205
|
+
"require-directory": "^2.1.1",
|
|
206
|
+
"resolve-from": "^5.0.0",
|
|
207
|
+
"rimraf": "^3.0.2",
|
|
208
|
+
"scheduler": "^0.27.0",
|
|
209
|
+
"semver": "^7.7.4",
|
|
210
|
+
"send": "^0.19.2",
|
|
211
|
+
"serialize-error": "^2.1.0",
|
|
212
|
+
"serve-static": "^1.16.3",
|
|
213
|
+
"setprototypeof": "^1.2.0",
|
|
214
|
+
"shebang-command": "^2.0.0",
|
|
215
|
+
"shebang-regex": "^3.0.0",
|
|
216
|
+
"shell-quote": "^1.8.3",
|
|
217
|
+
"signal-exit": "^3.0.7",
|
|
218
|
+
"slash": "^3.0.0",
|
|
219
|
+
"source-map": "^0.5.7",
|
|
220
|
+
"source-map-support": "^0.5.21",
|
|
221
|
+
"sprintf-js": "^1.0.3",
|
|
222
|
+
"stack-utils": "^2.0.6",
|
|
223
|
+
"stackframe": "^1.3.4",
|
|
224
|
+
"stacktrace-parser": "^0.1.11",
|
|
225
|
+
"statuses": "^1.5.0",
|
|
226
|
+
"string-width": "^4.2.3",
|
|
227
|
+
"strip-ansi": "^6.0.1",
|
|
228
|
+
"supports-color": "^7.2.0",
|
|
229
|
+
"terser": "^5.46.0",
|
|
230
|
+
"test-exclude": "^6.0.0",
|
|
231
|
+
"throat": "^5.0.0",
|
|
232
|
+
"tinyglobby": "^0.2.15",
|
|
233
|
+
"tmpl": "^1.0.5",
|
|
234
|
+
"to-regex-range": "^5.0.1",
|
|
235
|
+
"toidentifier": "^1.0.1",
|
|
236
|
+
"type-detect": "^4.0.8",
|
|
237
|
+
"type-fest": "^0.7.1",
|
|
238
|
+
"undici-types": "^7.16.0",
|
|
239
|
+
"unpipe": "^1.0.0",
|
|
240
|
+
"update-browserslist-db": "^1.2.3",
|
|
241
|
+
"utils-merge": "^1.0.1",
|
|
242
|
+
"vlq": "^1.0.1",
|
|
243
|
+
"walker": "^1.0.8",
|
|
244
|
+
"whatwg-fetch": "^3.6.20",
|
|
245
|
+
"which": "^2.0.2",
|
|
246
|
+
"wrap-ansi": "^7.0.0",
|
|
247
|
+
"wrappy": "^1.0.2",
|
|
248
|
+
"write-file-atomic": "^4.0.2",
|
|
249
|
+
"ws": "^7.5.10",
|
|
250
|
+
"y18n": "^5.0.8",
|
|
251
|
+
"yallist": "^3.1.1",
|
|
252
|
+
"yaml": "^2.8.2",
|
|
253
|
+
"yargs": "^17.7.2",
|
|
254
|
+
"yargs-parser": "^21.1.1"
|
|
255
|
+
},
|
|
256
|
+
"devDependencies": {
|
|
257
|
+
"@types/react": "^19.2.10",
|
|
258
|
+
"react-native": "*",
|
|
259
|
+
"typescript": "^5.9.3"
|
|
260
|
+
},
|
|
261
|
+
"peerDependencies": {
|
|
262
|
+
"react": ">=17",
|
|
263
|
+
"react-native": ">=0.70"
|
|
264
|
+
}
|
|
265
|
+
}
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import { FlatList, StyleSheet, Text, View } from "react-native";
|
|
2
|
+
import React, { useState, useEffect, forwardRef, useRef } from "react";
|
|
3
|
+
import { Month_name, Week_name } from "../Utils/month_name";
|
|
4
|
+
import { WheelProps } from "../types";
|
|
5
|
+
|
|
6
|
+
const Wheel = <T extends number | string>({
|
|
7
|
+
data,
|
|
8
|
+
onChange,
|
|
9
|
+
value,
|
|
10
|
+
type,
|
|
11
|
+
height,
|
|
12
|
+
numRows,
|
|
13
|
+
centerItem,
|
|
14
|
+
textSizeActive,
|
|
15
|
+
textSizeInActive,
|
|
16
|
+
textWeightActive,
|
|
17
|
+
textWeightInActive,
|
|
18
|
+
fontFamily,
|
|
19
|
+
repeat,
|
|
20
|
+
format,
|
|
21
|
+
year,
|
|
22
|
+
month,
|
|
23
|
+
}: WheelProps<T>) => {
|
|
24
|
+
const listref = useRef<FlatList<T> | null>(null);
|
|
25
|
+
const [Data, setData] = useState<T[]>(data);
|
|
26
|
+
|
|
27
|
+
useEffect(() => {
|
|
28
|
+
setData(data);
|
|
29
|
+
}, [data]);
|
|
30
|
+
|
|
31
|
+
useEffect(() => {
|
|
32
|
+
if (!listref.current) return;
|
|
33
|
+
|
|
34
|
+
const index = data.indexOf(value);
|
|
35
|
+
if (index >= 0) {
|
|
36
|
+
listref.current.scrollToIndex({
|
|
37
|
+
index,
|
|
38
|
+
animated: true,
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
}, [value]);
|
|
42
|
+
const selectedIndex = Data.indexOf(value);
|
|
43
|
+
|
|
44
|
+
const loadMore = () => {
|
|
45
|
+
setData((prev) => [...prev, ...data]);
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
const renderItem = (item: T): number | string => {
|
|
49
|
+
if (type === "day") {
|
|
50
|
+
if (format?.day === "alphabetical") {
|
|
51
|
+
const selectedYear = year || new Date().getFullYear();
|
|
52
|
+
const selectedMonth = month ? month - 1 : new Date().getMonth();
|
|
53
|
+
|
|
54
|
+
const dayvalue = item as number;
|
|
55
|
+
|
|
56
|
+
const dateObj = new Date(selectedYear, selectedMonth, dayvalue);
|
|
57
|
+
const dayIndex = dateObj.getDay();
|
|
58
|
+
const dayName = Week_name[dayIndex].shortName;
|
|
59
|
+
|
|
60
|
+
return `${dayName} ${item}`;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return item;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
if (type === "month") {
|
|
67
|
+
const monthValue = item as number;
|
|
68
|
+
if (format?.month === "alphabeticalShort") {
|
|
69
|
+
const name = Month_name.find((e) => e.id === monthValue);
|
|
70
|
+
return name?.shortName || item;
|
|
71
|
+
}
|
|
72
|
+
if (format?.month === "alphabeticalLong") {
|
|
73
|
+
const name = Month_name.find((e) => e.id == item);
|
|
74
|
+
return name?.fullName || item;
|
|
75
|
+
}
|
|
76
|
+
return item;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
if (type === "year") {
|
|
80
|
+
if (format?.year === "short") {
|
|
81
|
+
return String(item).slice(-2);
|
|
82
|
+
}
|
|
83
|
+
return item;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
if (type === "hours") {
|
|
87
|
+
if (format?.hours === "hh") {
|
|
88
|
+
const result = String(item).padStart(2, "0");
|
|
89
|
+
return `${result} hrs`;
|
|
90
|
+
}
|
|
91
|
+
return `${item} hrs`;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
if (type === "minutes") {
|
|
95
|
+
if (format?.minutes === "mm") {
|
|
96
|
+
const result = String(item).padStart(2, "0");
|
|
97
|
+
return `${result} min`;
|
|
98
|
+
}
|
|
99
|
+
return `${item} min`;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
return item;
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
return (
|
|
106
|
+
<View
|
|
107
|
+
style={{
|
|
108
|
+
height: height * numRows,
|
|
109
|
+
width: data.length <= 2 ? 60 : undefined,
|
|
110
|
+
flex: data.length > 2 ? 1 : 0,
|
|
111
|
+
}}
|
|
112
|
+
>
|
|
113
|
+
<FlatList
|
|
114
|
+
ref={listref}
|
|
115
|
+
key={Data.length} // Force re-render when data length changes
|
|
116
|
+
data={Data}
|
|
117
|
+
bounces={false}
|
|
118
|
+
overScrollMode="never"
|
|
119
|
+
keyExtractor={(item, index) => `${type}-${item}-${index}`}
|
|
120
|
+
snapToInterval={height}
|
|
121
|
+
decelerationRate="fast"
|
|
122
|
+
showsVerticalScrollIndicator={false}
|
|
123
|
+
initialScrollIndex={Math.max(0, selectedIndex)}
|
|
124
|
+
onEndReached={repeat ? loadMore : null}
|
|
125
|
+
onEndReachedThreshold={0.7}
|
|
126
|
+
contentContainerStyle={{
|
|
127
|
+
paddingVertical: height * centerItem,
|
|
128
|
+
}}
|
|
129
|
+
getItemLayout={(_, index) => ({
|
|
130
|
+
length: height,
|
|
131
|
+
offset: height * index,
|
|
132
|
+
index,
|
|
133
|
+
})}
|
|
134
|
+
onMomentumScrollEnd={(e) => {
|
|
135
|
+
const index = Math.round(e.nativeEvent.contentOffset.y / height);
|
|
136
|
+
const nextValue = Data[index % data.length];
|
|
137
|
+
// if (isItemDisabled(nextValue)) return;
|
|
138
|
+
onChange?.(nextValue);
|
|
139
|
+
}}
|
|
140
|
+
renderItem={({ item }) => (
|
|
141
|
+
<View
|
|
142
|
+
style={{
|
|
143
|
+
height: height,
|
|
144
|
+
justifyContent: "center",
|
|
145
|
+
alignItems: "center",
|
|
146
|
+
}}
|
|
147
|
+
>
|
|
148
|
+
<Text
|
|
149
|
+
style={{
|
|
150
|
+
// opacity: isItemDisabled(item) ? 0.25 : item === value ? 1 : 0.4,
|
|
151
|
+
fontFamily,
|
|
152
|
+
fontSize:
|
|
153
|
+
item === value
|
|
154
|
+
? (textSizeActive ?? 18)
|
|
155
|
+
: (textSizeInActive ?? 14),
|
|
156
|
+
opacity: item === value ? 1 : 0.31,
|
|
157
|
+
fontWeight:
|
|
158
|
+
item === value
|
|
159
|
+
? (textWeightActive ?? "600")
|
|
160
|
+
: (textWeightInActive ?? "400"),
|
|
161
|
+
}}
|
|
162
|
+
>
|
|
163
|
+
{renderItem(item)}
|
|
164
|
+
</Text>
|
|
165
|
+
</View>
|
|
166
|
+
)}
|
|
167
|
+
/>
|
|
168
|
+
</View>
|
|
169
|
+
);
|
|
170
|
+
};
|
|
171
|
+
|
|
172
|
+
export default Wheel;
|
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
import { StyleSheet, Text, View } from "react-native";
|
|
2
|
+
import React, { useEffect, useState, useMemo, useRef } from "react";
|
|
3
|
+
import Wheel from "./Components/Wheel";
|
|
4
|
+
import { DateTimePickerProps } from "./types";
|
|
5
|
+
|
|
6
|
+
export const DateTimePicker = ({
|
|
7
|
+
start,
|
|
8
|
+
end,
|
|
9
|
+
initial,
|
|
10
|
+
selectorContainerStyle,
|
|
11
|
+
textSizeActive = 15,
|
|
12
|
+
textSizeInActive,
|
|
13
|
+
textWeightActive,
|
|
14
|
+
textWeightInActive,
|
|
15
|
+
fontFamily,
|
|
16
|
+
height = 44,
|
|
17
|
+
numRows = 5,
|
|
18
|
+
onChange,
|
|
19
|
+
format = {
|
|
20
|
+
day: "alphabetical",
|
|
21
|
+
month: "alphabeticalShort",
|
|
22
|
+
year: "short",
|
|
23
|
+
timeFormat: 24,
|
|
24
|
+
hours: "hh",
|
|
25
|
+
minutes: "mm",
|
|
26
|
+
},
|
|
27
|
+
}: DateTimePickerProps) => {
|
|
28
|
+
const initialdate =
|
|
29
|
+
initial instanceof Date ? initial : new Date(initial ?? Date.now());
|
|
30
|
+
const minDate = start instanceof Date ? start : null;
|
|
31
|
+
const maxDate = end instanceof Date ? end : null;
|
|
32
|
+
|
|
33
|
+
const hours24 = initialdate.getHours();
|
|
34
|
+
const hours12 = hours24 % 12 || 12;
|
|
35
|
+
const period = hours24 >= 12 ? "PM" : "AM";
|
|
36
|
+
|
|
37
|
+
// State declarations
|
|
38
|
+
const [day, setDay] = useState(initialdate.getDate());
|
|
39
|
+
const [month, setMonth] = useState(initialdate.getMonth() + 1);
|
|
40
|
+
const [year, setYear] = useState(initialdate.getFullYear());
|
|
41
|
+
const [hour, setHour] = useState(format.timeFormat == 24 ? hours24 : hours12);
|
|
42
|
+
const [minutes, setMinutes] = useState(initialdate.getMinutes());
|
|
43
|
+
const [meridiem, setMeridiem] = useState<"AM" | "PM">(period);
|
|
44
|
+
|
|
45
|
+
// Calculate days in month - THIS IS THE FIX
|
|
46
|
+
const days_DATA = useMemo(() => {
|
|
47
|
+
const daysInMonth = new Date(year, month, 0).getDate();
|
|
48
|
+
return Array.from({ length: daysInMonth }, (_, i) => i + 1);
|
|
49
|
+
}, [year, month]); // Recalculate when year or month changes
|
|
50
|
+
|
|
51
|
+
const month_DATA = Array.from({ length: 12 }, (_, i) => i + 1);
|
|
52
|
+
const year_DATA = Array.from({ length: 20 }, (_, i) => 2020 + i);
|
|
53
|
+
|
|
54
|
+
// Time
|
|
55
|
+
const hour24_DATA = Array.from({ length: 24 }, (_, i) => i);
|
|
56
|
+
const hour12_DATA = Array.from({ length: 12 }, (_, i) => (i === 0 ? 12 : i));
|
|
57
|
+
const minutes_DATA = Array.from({ length: 60 }, (_, i) => i);
|
|
58
|
+
|
|
59
|
+
const ITEM_HEIGHT = height || 44;
|
|
60
|
+
const VISIBLE_ITEMS = numRows || 3;
|
|
61
|
+
const CENTER_INDEX = Math.floor(VISIBLE_ITEMS / 2);
|
|
62
|
+
|
|
63
|
+
// Adjust day if it exceeds days in new month
|
|
64
|
+
useEffect(() => {
|
|
65
|
+
const maxDay = new Date(year, month, 0).getDate();
|
|
66
|
+
if (day > maxDay) {
|
|
67
|
+
setDay(maxDay);
|
|
68
|
+
}
|
|
69
|
+
}, [year, month]);
|
|
70
|
+
|
|
71
|
+
useEffect(() => {
|
|
72
|
+
let finalHour = hour;
|
|
73
|
+
if (format.timeFormat === 12) {
|
|
74
|
+
finalHour = to24Hour(hour, meridiem);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
let selected = new Date(year, month - 1, day, finalHour, minutes, 0);
|
|
78
|
+
|
|
79
|
+
if (minDate && selected < minDate) selected = new Date(minDate);
|
|
80
|
+
if (maxDate && selected > maxDate) selected = new Date(maxDate);
|
|
81
|
+
|
|
82
|
+
// 🔒 Prevent infinite loop
|
|
83
|
+
if (
|
|
84
|
+
selected.getTime() !==
|
|
85
|
+
new Date(year, month - 1, day, finalHour, minutes, 0).getTime()
|
|
86
|
+
) {
|
|
87
|
+
setDay(selected.getDate());
|
|
88
|
+
setMonth(selected.getMonth() + 1);
|
|
89
|
+
setYear(selected.getFullYear());
|
|
90
|
+
setHour(
|
|
91
|
+
format.timeFormat === 12
|
|
92
|
+
? selected.getHours() % 12 || 12
|
|
93
|
+
: selected.getHours(),
|
|
94
|
+
);
|
|
95
|
+
setMinutes(selected.getMinutes());
|
|
96
|
+
setMeridiem(selected.getHours() >= 12 ? "PM" : "AM");
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
onChange?.(selected);
|
|
100
|
+
}, [day, month, year, hour, minutes, meridiem]);
|
|
101
|
+
|
|
102
|
+
const to24Hour = (hour12: number, meridiem: "AM" | "PM") => {
|
|
103
|
+
if (meridiem === "AM") {
|
|
104
|
+
return hour12 === 12 ? 0 : hour12;
|
|
105
|
+
}
|
|
106
|
+
return hour12 === 12 ? 12 : hour12 + 12;
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
return (
|
|
110
|
+
<View style={styles.container}>
|
|
111
|
+
{/* Selection window */}
|
|
112
|
+
<View
|
|
113
|
+
pointerEvents="none"
|
|
114
|
+
style={{
|
|
115
|
+
...selectorContainerStyle,
|
|
116
|
+
position: "absolute",
|
|
117
|
+
top: ITEM_HEIGHT * CENTER_INDEX,
|
|
118
|
+
height: ITEM_HEIGHT,
|
|
119
|
+
left: 0,
|
|
120
|
+
right: 0,
|
|
121
|
+
backgroundColor: "#d9d9db8d",
|
|
122
|
+
borderRadius: 30,
|
|
123
|
+
}}
|
|
124
|
+
/>
|
|
125
|
+
|
|
126
|
+
{format.day && (
|
|
127
|
+
<Wheel
|
|
128
|
+
type="day"
|
|
129
|
+
data={days_DATA}
|
|
130
|
+
onChange={setDay}
|
|
131
|
+
value={day}
|
|
132
|
+
height={height}
|
|
133
|
+
numRows={numRows}
|
|
134
|
+
centerItem={CENTER_INDEX}
|
|
135
|
+
textSizeActive={textSizeActive}
|
|
136
|
+
textSizeInActive={textSizeInActive}
|
|
137
|
+
textWeightActive={textWeightActive}
|
|
138
|
+
textWeightInActive={textWeightInActive}
|
|
139
|
+
fontFamily={fontFamily}
|
|
140
|
+
repeat={true}
|
|
141
|
+
format={format}
|
|
142
|
+
year={year}
|
|
143
|
+
month={month}
|
|
144
|
+
minDate={start}
|
|
145
|
+
maxDate={end}
|
|
146
|
+
/>
|
|
147
|
+
)}
|
|
148
|
+
|
|
149
|
+
{format.month && (
|
|
150
|
+
<Wheel
|
|
151
|
+
type="month"
|
|
152
|
+
data={month_DATA}
|
|
153
|
+
onChange={setMonth}
|
|
154
|
+
value={month}
|
|
155
|
+
height={height}
|
|
156
|
+
numRows={numRows}
|
|
157
|
+
centerItem={CENTER_INDEX}
|
|
158
|
+
textSizeActive={textSizeActive}
|
|
159
|
+
textSizeInActive={textSizeInActive}
|
|
160
|
+
textWeightActive={textWeightActive}
|
|
161
|
+
textWeightInActive={textWeightInActive}
|
|
162
|
+
fontFamily={fontFamily}
|
|
163
|
+
repeat={true}
|
|
164
|
+
format={format}
|
|
165
|
+
year={year}
|
|
166
|
+
month={month}
|
|
167
|
+
minDate={start}
|
|
168
|
+
maxDate={end}
|
|
169
|
+
/>
|
|
170
|
+
)}
|
|
171
|
+
|
|
172
|
+
{format.year && (
|
|
173
|
+
<Wheel
|
|
174
|
+
type="year"
|
|
175
|
+
data={year_DATA}
|
|
176
|
+
onChange={setYear}
|
|
177
|
+
value={year}
|
|
178
|
+
height={height}
|
|
179
|
+
numRows={numRows}
|
|
180
|
+
centerItem={CENTER_INDEX}
|
|
181
|
+
textSizeActive={textSizeActive}
|
|
182
|
+
textSizeInActive={textSizeInActive}
|
|
183
|
+
textWeightActive={textWeightActive}
|
|
184
|
+
textWeightInActive={textWeightInActive}
|
|
185
|
+
fontFamily={fontFamily}
|
|
186
|
+
repeat={true}
|
|
187
|
+
format={format}
|
|
188
|
+
year={year}
|
|
189
|
+
month={month}
|
|
190
|
+
minDate={start}
|
|
191
|
+
maxDate={end}
|
|
192
|
+
/>
|
|
193
|
+
)}
|
|
194
|
+
|
|
195
|
+
{format.hours && (
|
|
196
|
+
<Wheel
|
|
197
|
+
type="hours"
|
|
198
|
+
data={format.timeFormat == 12 ? hour12_DATA : hour24_DATA}
|
|
199
|
+
onChange={setHour}
|
|
200
|
+
value={hour}
|
|
201
|
+
height={height}
|
|
202
|
+
numRows={numRows}
|
|
203
|
+
centerItem={CENTER_INDEX}
|
|
204
|
+
textSizeActive={textSizeActive}
|
|
205
|
+
textSizeInActive={textSizeInActive}
|
|
206
|
+
textWeightActive={textWeightActive}
|
|
207
|
+
textWeightInActive={textWeightInActive}
|
|
208
|
+
fontFamily={fontFamily}
|
|
209
|
+
repeat={true}
|
|
210
|
+
format={format}
|
|
211
|
+
year={year}
|
|
212
|
+
month={month}
|
|
213
|
+
minDate={start}
|
|
214
|
+
maxDate={end}
|
|
215
|
+
/>
|
|
216
|
+
)}
|
|
217
|
+
|
|
218
|
+
{format.minutes && (
|
|
219
|
+
<Wheel
|
|
220
|
+
type="minutes"
|
|
221
|
+
data={minutes_DATA}
|
|
222
|
+
onChange={setMinutes}
|
|
223
|
+
value={minutes}
|
|
224
|
+
height={height}
|
|
225
|
+
numRows={numRows}
|
|
226
|
+
centerItem={CENTER_INDEX}
|
|
227
|
+
textSizeActive={textSizeActive}
|
|
228
|
+
textSizeInActive={textSizeInActive}
|
|
229
|
+
textWeightActive={textWeightActive}
|
|
230
|
+
textWeightInActive={textWeightInActive}
|
|
231
|
+
fontFamily={fontFamily}
|
|
232
|
+
repeat={true}
|
|
233
|
+
format={format}
|
|
234
|
+
year={year}
|
|
235
|
+
month={month}
|
|
236
|
+
minDate={start}
|
|
237
|
+
maxDate={end}
|
|
238
|
+
/>
|
|
239
|
+
)}
|
|
240
|
+
|
|
241
|
+
{format.timeFormat == 12 && (
|
|
242
|
+
<Wheel<"AM" | "PM">
|
|
243
|
+
data={["AM", "PM"]}
|
|
244
|
+
onChange={setMeridiem}
|
|
245
|
+
value={meridiem}
|
|
246
|
+
height={height}
|
|
247
|
+
numRows={numRows}
|
|
248
|
+
centerItem={CENTER_INDEX}
|
|
249
|
+
textSizeActive={textSizeActive}
|
|
250
|
+
textSizeInActive={textSizeInActive}
|
|
251
|
+
textWeightActive={textWeightActive}
|
|
252
|
+
textWeightInActive={textWeightInActive}
|
|
253
|
+
fontFamily={fontFamily}
|
|
254
|
+
year={year}
|
|
255
|
+
month={month}
|
|
256
|
+
minDate={start}
|
|
257
|
+
maxDate={end}
|
|
258
|
+
/>
|
|
259
|
+
)}
|
|
260
|
+
</View>
|
|
261
|
+
);
|
|
262
|
+
};
|
|
263
|
+
|
|
264
|
+
const styles = StyleSheet.create({
|
|
265
|
+
container: {
|
|
266
|
+
flexDirection: "row",
|
|
267
|
+
justifyContent: "center",
|
|
268
|
+
paddingHorizontal: 20,
|
|
269
|
+
},
|
|
270
|
+
});
|