react-native-drum-picker 0.1.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.
Files changed (36) hide show
  1. package/CHANGELOG.md +36 -0
  2. package/LICENSE +21 -0
  3. package/README.md +252 -0
  4. package/android/build.gradle +68 -0
  5. package/android/src/main/AndroidManifest.xml +2 -0
  6. package/android/src/main/java/com/drumpicker/DrumPickerAdapter.kt +108 -0
  7. package/android/src/main/java/com/drumpicker/DrumPickerChangeEvent.kt +25 -0
  8. package/android/src/main/java/com/drumpicker/DrumPickerDefaults.kt +19 -0
  9. package/android/src/main/java/com/drumpicker/DrumPickerPackage.kt +17 -0
  10. package/android/src/main/java/com/drumpicker/DrumPickerView.kt +583 -0
  11. package/android/src/main/java/com/drumpicker/DrumPickerViewManager.kt +108 -0
  12. package/lib/module/DateDrumPicker.js +153 -0
  13. package/lib/module/DrumPicker.js +6 -0
  14. package/lib/module/DrumPicker.native.js +63 -0
  15. package/lib/module/DrumPickerViewNativeComponent.ts +31 -0
  16. package/lib/module/dateDrumPickerLogic.js +82 -0
  17. package/lib/module/index.js +5 -0
  18. package/lib/module/package.json +1 -0
  19. package/lib/module/types.js +4 -0
  20. package/lib/typescript/package.json +1 -0
  21. package/lib/typescript/src/DateDrumPicker.d.ts +32 -0
  22. package/lib/typescript/src/DrumPicker.d.ts +3 -0
  23. package/lib/typescript/src/DrumPicker.native.d.ts +3 -0
  24. package/lib/typescript/src/DrumPickerViewNativeComponent.d.ts +25 -0
  25. package/lib/typescript/src/dateDrumPickerLogic.d.ts +20 -0
  26. package/lib/typescript/src/index.d.ts +5 -0
  27. package/lib/typescript/src/types.d.ts +24 -0
  28. package/package.json +189 -0
  29. package/react-native.config.js +13 -0
  30. package/src/DateDrumPicker.tsx +267 -0
  31. package/src/DrumPicker.native.tsx +68 -0
  32. package/src/DrumPicker.tsx +7 -0
  33. package/src/DrumPickerViewNativeComponent.ts +31 -0
  34. package/src/dateDrumPickerLogic.ts +95 -0
  35. package/src/index.tsx +15 -0
  36. package/src/types.ts +25 -0
@@ -0,0 +1,267 @@
1
+ import { useCallback, useMemo, useState } from 'react';
2
+ import {
3
+ StyleSheet,
4
+ View,
5
+ type NativeSyntheticEvent,
6
+ type StyleProp,
7
+ type ViewStyle,
8
+ } from 'react-native';
9
+ import {
10
+ buildDayItems,
11
+ buildMonthItems,
12
+ buildYearItems,
13
+ clampDateDrumPickerValue,
14
+ clampDayForMonth,
15
+ clampYear,
16
+ normalizeYearRange,
17
+ parseMonthFromLabel,
18
+ type DateDrumPickerMonthFormat,
19
+ type DateDrumPickerValue,
20
+ } from './dateDrumPickerLogic';
21
+ import { DrumPicker } from './DrumPicker';
22
+ import type { DrumPickerChangeEvent } from './types';
23
+
24
+ export type DateDrumPickerMode =
25
+ | 'day'
26
+ | 'month'
27
+ | 'year'
28
+ | 'day-month'
29
+ | 'month-year'
30
+ | 'day-month-year'
31
+ | 'month-day-year'
32
+ | 'year-month-day';
33
+
34
+ export type { DateDrumPickerMonthFormat, DateDrumPickerValue };
35
+ export {
36
+ clampDateDrumPickerValue,
37
+ getDaysInMonth,
38
+ normalizeYearRange,
39
+ } from './dateDrumPickerLogic';
40
+
41
+ export type DateDrumPickerColumnKey = 'day' | 'month' | 'year';
42
+
43
+ export type DateDrumPickerProps = {
44
+ mode?: DateDrumPickerMode;
45
+ value?: DateDrumPickerValue;
46
+ onChange?: (value: DateDrumPickerValue) => void;
47
+ minYear?: number;
48
+ maxYear?: number;
49
+ monthFormat?: DateDrumPickerMonthFormat;
50
+ locale?: string;
51
+ itemHeight?: number;
52
+ visibleItemCount?: number;
53
+ textColor?: string;
54
+ selectedTextColor?: string;
55
+ textSize?: number;
56
+ selectedTextSize?: number;
57
+ showSelectionIndicator?: boolean;
58
+ selectionIndicatorColor?: string;
59
+ selectionIndicatorHeight?: number;
60
+ backgroundColor?: string;
61
+ itemBackgroundColor?: string;
62
+ containerBackgroundColor?: string;
63
+ style?: StyleProp<ViewStyle>;
64
+ columnStyle?: StyleProp<ViewStyle>;
65
+ columnStyles?: Partial<Record<DateDrumPickerColumnKey, StyleProp<ViewStyle>>>;
66
+ };
67
+
68
+ type DateColumnKey = DateDrumPickerColumnKey;
69
+
70
+ const COLUMN_ORDER: Record<DateDrumPickerMode, DateColumnKey[]> = {
71
+ 'day': ['day'],
72
+ 'month': ['month'],
73
+ 'year': ['year'],
74
+ 'day-month': ['day', 'month'],
75
+ 'month-year': ['month', 'year'],
76
+ 'day-month-year': ['day', 'month', 'year'],
77
+ 'month-day-year': ['month', 'day', 'year'],
78
+ 'year-month-day': ['year', 'month', 'day'],
79
+ };
80
+
81
+ const COLUMN_WIDTH: Record<DateColumnKey, number> = {
82
+ day: 64,
83
+ month: 110,
84
+ year: 86,
85
+ };
86
+
87
+ const DEFAULT_ITEM_HEIGHT = 44;
88
+ const DEFAULT_VISIBLE_ITEM_COUNT = 5;
89
+
90
+ export function DateDrumPicker({
91
+ mode = 'day-month-year',
92
+ value,
93
+ onChange,
94
+ minYear: minYearProp,
95
+ maxYear: maxYearProp,
96
+ monthFormat = 'short',
97
+ locale = 'en',
98
+ itemHeight = DEFAULT_ITEM_HEIGHT,
99
+ visibleItemCount = DEFAULT_VISIBLE_ITEM_COUNT,
100
+ textColor,
101
+ selectedTextColor,
102
+ textSize,
103
+ selectedTextSize,
104
+ showSelectionIndicator,
105
+ selectionIndicatorColor,
106
+ selectionIndicatorHeight,
107
+ backgroundColor = 'transparent',
108
+ itemBackgroundColor = 'transparent',
109
+ containerBackgroundColor = 'transparent',
110
+ style,
111
+ columnStyle,
112
+ columnStyles,
113
+ }: DateDrumPickerProps) {
114
+ const currentYear = new Date().getFullYear();
115
+ const { minYear, maxYear } = useMemo(
116
+ () =>
117
+ normalizeYearRange(
118
+ minYearProp ?? currentYear - 100,
119
+ maxYearProp ?? currentYear + 50
120
+ ),
121
+ [minYearProp, maxYearProp, currentYear]
122
+ );
123
+
124
+ const isControlled = value !== undefined;
125
+ const [internalValue, setInternalValue] = useState(() =>
126
+ clampDateDrumPickerValue(value ?? {}, minYear, maxYear)
127
+ );
128
+
129
+ const resolvedValue = useMemo(() => {
130
+ if (isControlled) {
131
+ return clampDateDrumPickerValue(value, minYear, maxYear);
132
+ }
133
+ return internalValue;
134
+ }, [isControlled, value, internalValue, minYear, maxYear]);
135
+
136
+ const columns = COLUMN_ORDER[mode];
137
+ const dayItems = useMemo(
138
+ () => buildDayItems(resolvedValue.month, resolvedValue.year),
139
+ [resolvedValue.month, resolvedValue.year]
140
+ );
141
+ const monthItems = useMemo(
142
+ () => buildMonthItems(monthFormat, locale),
143
+ [monthFormat, locale]
144
+ );
145
+ const yearItems = useMemo(
146
+ () => buildYearItems(minYear, maxYear),
147
+ [minYear, maxYear]
148
+ );
149
+
150
+ const pickerHeight = itemHeight * visibleItemCount;
151
+
152
+ const emitChange = useCallback(
153
+ (patch: Partial<DateDrumPickerValue>) => {
154
+ const next = clampDateDrumPickerValue(
155
+ { ...resolvedValue, ...patch },
156
+ minYear,
157
+ maxYear
158
+ );
159
+ if (!isControlled) {
160
+ setInternalValue(next);
161
+ }
162
+ onChange?.(next);
163
+ },
164
+ [isControlled, onChange, resolvedValue, minYear, maxYear]
165
+ );
166
+
167
+ const sharedPickerProps = {
168
+ itemHeight,
169
+ visibleItemCount,
170
+ textColor,
171
+ selectedTextColor,
172
+ textSize,
173
+ selectedTextSize,
174
+ showSelectionIndicator,
175
+ selectionIndicatorColor,
176
+ selectionIndicatorHeight,
177
+ backgroundColor,
178
+ itemBackgroundColor,
179
+ containerBackgroundColor,
180
+ };
181
+
182
+ const columnContainerStyle = (
183
+ column: DateColumnKey
184
+ ): StyleProp<ViewStyle> => [
185
+ styles.column,
186
+ { width: COLUMN_WIDTH[column], height: pickerHeight },
187
+ columnStyle,
188
+ columnStyles?.[column],
189
+ ];
190
+
191
+ const renderColumn = (column: DateColumnKey) => {
192
+ if (column === 'day') {
193
+ return (
194
+ <DrumPicker
195
+ key="day"
196
+ {...sharedPickerProps}
197
+ style={columnContainerStyle('day')}
198
+ items={dayItems}
199
+ selectedIndex={Math.min(resolvedValue.day - 1, dayItems.length - 1)}
200
+ onChange={(event: NativeSyntheticEvent<DrumPickerChangeEvent>) => {
201
+ const day = clampDayForMonth(
202
+ event.nativeEvent.index + 1,
203
+ resolvedValue.month,
204
+ resolvedValue.year
205
+ );
206
+ emitChange({ day });
207
+ }}
208
+ />
209
+ );
210
+ }
211
+
212
+ if (column === 'month') {
213
+ return (
214
+ <DrumPicker
215
+ key="month"
216
+ {...sharedPickerProps}
217
+ style={columnContainerStyle('month')}
218
+ items={monthItems}
219
+ selectedIndex={resolvedValue.month - 1}
220
+ onChange={(event: NativeSyntheticEvent<DrumPickerChangeEvent>) => {
221
+ const month = parseMonthFromLabel(
222
+ event.nativeEvent.value,
223
+ monthFormat,
224
+ monthItems
225
+ );
226
+ emitChange({ month });
227
+ }}
228
+ />
229
+ );
230
+ }
231
+
232
+ return (
233
+ <DrumPicker
234
+ key="year"
235
+ {...sharedPickerProps}
236
+ style={columnContainerStyle('year')}
237
+ items={yearItems}
238
+ selectedIndex={resolvedValue.year - minYear}
239
+ onChange={(event: NativeSyntheticEvent<DrumPickerChangeEvent>) => {
240
+ const year = clampYear(
241
+ minYear + event.nativeEvent.index,
242
+ minYear,
243
+ maxYear
244
+ );
245
+ emitChange({ year });
246
+ }}
247
+ />
248
+ );
249
+ };
250
+
251
+ return (
252
+ <View style={[styles.row, style]}>
253
+ {columns.map((column) => renderColumn(column))}
254
+ </View>
255
+ );
256
+ }
257
+
258
+ const styles = StyleSheet.create({
259
+ row: {
260
+ flexDirection: 'row',
261
+ alignItems: 'center',
262
+ backgroundColor: 'transparent',
263
+ },
264
+ column: {
265
+ backgroundColor: 'transparent',
266
+ },
267
+ });
@@ -0,0 +1,68 @@
1
+ import { StyleSheet } from 'react-native';
2
+ import DrumPickerNative from './DrumPickerViewNativeComponent';
3
+ import type { DrumPickerProps } from './types';
4
+
5
+ const DEFAULTS = {
6
+ selectedIndex: 0,
7
+ itemHeight: 44,
8
+ visibleItemCount: 5,
9
+ textColor: '#8E8E93',
10
+ selectedTextColor: '#1C1C1E',
11
+ textSize: 20,
12
+ selectedTextSize: 22,
13
+ showSelectionIndicator: true,
14
+ selectionIndicatorColor: '#D1D1D6',
15
+ selectionIndicatorHeight: 1,
16
+ backgroundColor: 'transparent',
17
+ itemBackgroundColor: 'transparent',
18
+ containerBackgroundColor: 'transparent',
19
+ } as const;
20
+
21
+ export function DrumPicker({
22
+ items,
23
+ selectedIndex = DEFAULTS.selectedIndex,
24
+ itemHeight = DEFAULTS.itemHeight,
25
+ visibleItemCount = DEFAULTS.visibleItemCount,
26
+ textColor = DEFAULTS.textColor,
27
+ selectedTextColor = DEFAULTS.selectedTextColor,
28
+ textSize = DEFAULTS.textSize,
29
+ selectedTextSize = DEFAULTS.selectedTextSize,
30
+ showSelectionIndicator = DEFAULTS.showSelectionIndicator,
31
+ selectionIndicatorColor = DEFAULTS.selectionIndicatorColor,
32
+ selectionIndicatorHeight = DEFAULTS.selectionIndicatorHeight,
33
+ backgroundColor = DEFAULTS.backgroundColor,
34
+ itemBackgroundColor = DEFAULTS.itemBackgroundColor,
35
+ containerBackgroundColor = DEFAULTS.containerBackgroundColor,
36
+ onChange,
37
+ style,
38
+ }: DrumPickerProps) {
39
+ const pickerHeight = itemHeight * visibleItemCount;
40
+ const pickerStyle = StyleSheet.flatten([
41
+ {
42
+ height: pickerHeight,
43
+ },
44
+ style,
45
+ ]);
46
+
47
+ return (
48
+ <DrumPickerNative
49
+ collapsable={false}
50
+ items={items}
51
+ selectedIndex={selectedIndex}
52
+ itemHeight={itemHeight}
53
+ visibleItemCount={visibleItemCount}
54
+ textColor={textColor}
55
+ selectedTextColor={selectedTextColor}
56
+ textSize={textSize}
57
+ selectedTextSize={selectedTextSize}
58
+ showSelectionIndicator={showSelectionIndicator}
59
+ selectionIndicatorColor={selectionIndicatorColor}
60
+ selectionIndicatorHeight={selectionIndicatorHeight}
61
+ backgroundColor={backgroundColor}
62
+ itemBackgroundColor={itemBackgroundColor}
63
+ containerBackgroundColor={containerBackgroundColor}
64
+ onValueChange={onChange}
65
+ style={pickerStyle}
66
+ />
67
+ );
68
+ }
@@ -0,0 +1,7 @@
1
+ import type { DrumPickerProps } from './types';
2
+
3
+ export function DrumPicker(_props: DrumPickerProps): never {
4
+ throw new Error(
5
+ "'react-native-drum-picker' is only supported on native platforms."
6
+ );
7
+ }
@@ -0,0 +1,31 @@
1
+ import {
2
+ codegenNativeComponent,
3
+ type CodegenTypes,
4
+ type ColorValue,
5
+ type ViewProps,
6
+ } from 'react-native';
7
+
8
+ export type DrumPickerChangeEventPayload = {
9
+ index: CodegenTypes.Int32;
10
+ value: string;
11
+ };
12
+
13
+ interface NativeProps extends ViewProps {
14
+ items: ReadonlyArray<string>;
15
+ selectedIndex?: CodegenTypes.Int32;
16
+ itemHeight?: CodegenTypes.Float;
17
+ visibleItemCount?: CodegenTypes.Int32;
18
+ textColor?: ColorValue;
19
+ selectedTextColor?: ColorValue;
20
+ textSize?: CodegenTypes.Float;
21
+ selectedTextSize?: CodegenTypes.Float;
22
+ showSelectionIndicator?: CodegenTypes.WithDefault<boolean, true>;
23
+ selectionIndicatorColor?: ColorValue;
24
+ selectionIndicatorHeight?: CodegenTypes.Float;
25
+ backgroundColor?: ColorValue;
26
+ containerBackgroundColor?: ColorValue;
27
+ itemBackgroundColor?: ColorValue;
28
+ onValueChange?: CodegenTypes.DirectEventHandler<DrumPickerChangeEventPayload>;
29
+ }
30
+
31
+ export default codegenNativeComponent<NativeProps>('DrumPickerView');
@@ -0,0 +1,95 @@
1
+ export type DateDrumPickerValue = {
2
+ day?: number;
3
+ month?: number;
4
+ year?: number;
5
+ };
6
+
7
+ export type DateDrumPickerMonthFormat = 'short' | 'long' | 'number';
8
+
9
+ export function getDaysInMonth(month: number, year: number): number {
10
+ return new Date(year, clampMonth(month), 0).getDate();
11
+ }
12
+
13
+ export function normalizeYearRange(
14
+ minYear: number,
15
+ maxYear: number
16
+ ): { minYear: number; maxYear: number } {
17
+ if (minYear <= maxYear) {
18
+ return { minYear, maxYear };
19
+ }
20
+ return { minYear: maxYear, maxYear: minYear };
21
+ }
22
+
23
+ export function clampMonth(month: number): number {
24
+ return Math.min(12, Math.max(1, Math.round(month)));
25
+ }
26
+
27
+ export function clampYear(
28
+ year: number,
29
+ minYear: number,
30
+ maxYear: number
31
+ ): number {
32
+ return Math.min(maxYear, Math.max(minYear, Math.round(year)));
33
+ }
34
+
35
+ export function clampDayForMonth(
36
+ day: number,
37
+ month: number,
38
+ year: number
39
+ ): number {
40
+ const maxDay = getDaysInMonth(month, year);
41
+ return Math.min(maxDay, Math.max(1, Math.round(day)));
42
+ }
43
+
44
+ export function clampDateDrumPickerValue(
45
+ value: DateDrumPickerValue,
46
+ minYear: number,
47
+ maxYear: number
48
+ ): Required<DateDrumPickerValue> {
49
+ const { minYear: min, maxYear: max } = normalizeYearRange(minYear, maxYear);
50
+ const now = new Date();
51
+ const month = clampMonth(value.month ?? now.getMonth() + 1);
52
+ const year = clampYear(value.year ?? now.getFullYear(), min, max);
53
+ const day = clampDayForMonth(value.day ?? now.getDate(), month, year);
54
+ return { day, month, year };
55
+ }
56
+
57
+ export function buildDayItems(month: number, year: number): string[] {
58
+ const count = getDaysInMonth(month, year);
59
+ return Array.from({ length: count }, (_, index) => String(index + 1));
60
+ }
61
+
62
+ export function buildMonthItems(
63
+ monthFormat: DateDrumPickerMonthFormat,
64
+ locale: string
65
+ ): string[] {
66
+ if (monthFormat === 'number') {
67
+ return Array.from({ length: 12 }, (_, index) =>
68
+ String(index + 1).padStart(2, '0')
69
+ );
70
+ }
71
+
72
+ const monthStyle = monthFormat === 'long' ? 'long' : 'short';
73
+ return Array.from({ length: 12 }, (_, index) => {
74
+ const date = new Date(2020, index, 1);
75
+ return new Intl.DateTimeFormat(locale, { month: monthStyle }).format(date);
76
+ });
77
+ }
78
+
79
+ export function buildYearItems(minYear: number, maxYear: number): string[] {
80
+ const { minYear: min, maxYear: max } = normalizeYearRange(minYear, maxYear);
81
+ const length = max - min + 1;
82
+ return Array.from({ length }, (_, index) => String(min + index));
83
+ }
84
+
85
+ export function parseMonthFromLabel(
86
+ label: string,
87
+ monthFormat: DateDrumPickerMonthFormat,
88
+ monthItems: string[]
89
+ ): number {
90
+ if (monthFormat === 'number') {
91
+ return clampMonth(Number.parseInt(label, 10));
92
+ }
93
+ const index = monthItems.indexOf(label);
94
+ return index >= 0 ? index + 1 : 1;
95
+ }
package/src/index.tsx ADDED
@@ -0,0 +1,15 @@
1
+ export { DrumPicker } from './DrumPicker';
2
+ export {
3
+ DateDrumPicker,
4
+ clampDateDrumPickerValue,
5
+ getDaysInMonth,
6
+ normalizeYearRange,
7
+ } from './DateDrumPicker';
8
+ export type { DrumPickerChangeEvent, DrumPickerProps } from './types';
9
+ export type {
10
+ DateDrumPickerColumnKey,
11
+ DateDrumPickerMode,
12
+ DateDrumPickerMonthFormat,
13
+ DateDrumPickerProps,
14
+ DateDrumPickerValue,
15
+ } from './DateDrumPicker';
package/src/types.ts ADDED
@@ -0,0 +1,25 @@
1
+ import type { NativeSyntheticEvent, StyleProp, ViewStyle } from 'react-native';
2
+
3
+ export type DrumPickerChangeEvent = {
4
+ index: number;
5
+ value: string;
6
+ };
7
+
8
+ export type DrumPickerProps = {
9
+ items: string[];
10
+ selectedIndex?: number;
11
+ itemHeight?: number;
12
+ visibleItemCount?: number;
13
+ textColor?: string;
14
+ selectedTextColor?: string;
15
+ textSize?: number;
16
+ selectedTextSize?: number;
17
+ showSelectionIndicator?: boolean;
18
+ selectionIndicatorColor?: string;
19
+ selectionIndicatorHeight?: number;
20
+ backgroundColor?: string;
21
+ itemBackgroundColor?: string;
22
+ containerBackgroundColor?: string;
23
+ onChange?: (event: NativeSyntheticEvent<DrumPickerChangeEvent>) => void;
24
+ style?: StyleProp<ViewStyle>;
25
+ };