Skip to content

Commit 3d6df28

Browse files
authored
feat:新增内联模式的datetime属性功能 (#23)
1 parent cc1f5dc commit 3d6df28

3 files changed

Lines changed: 47 additions & 13 deletions

File tree

harmony/date_picker.har

365 Bytes
Binary file not shown.

harmony/date_picker/BuildProfile.ets

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* Use these variables when you tailor your ArkTS code. They must be of the const type.
33
*/
4-
export const HAR_VERSION = '1.0.0';
4+
export const HAR_VERSION = '5.0.4-0.0.1';
55
export const BUILD_MODE_NAME = 'debug';
66
export const DEBUG = true;
77
export const TARGET_NAME = 'default';

harmony/date_picker/src/main/ets/components/index.ets

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@ import { RNC } from "@rnoh/react-native-openharmony/generated/ts"
1111

1212

1313
interface NativeDatePickerProps extends ViewRawProps {
14-
mode?: string;
15-
date?: string;
16-
open?: boolean;
17-
modal?:boolean;
14+
mode?: string | undefined;
15+
date?: string | undefined;
16+
open?: boolean | undefined;
17+
modal?:boolean | undefined;
1818
onConfirm?:(timestamp:string) => void
1919
onDateChange?:(timestamp:string) => void
20-
onChange?:() => void
20+
onChange?:(timestamp:string) => void
2121
onCancel?:() => void
22-
maximumDate?: string
23-
minimumDate?: string
22+
maximumDate?: string | undefined
23+
minimumDate?: string | undefined
2424
}
2525
export interface AutoLayoutProps {}
2626
export type NativeDatePickerDescriptor = Descriptor<"NativeDatePickerView", ViewBaseProps,AutoLayoutProps,NativeDatePickerProps>
@@ -31,7 +31,7 @@ export struct NativeDatePickerView {
3131
public ctx!: RNComponentContext
3232
public tag: number = 0
3333
@State descriptor: NativeDatePickerDescriptor = {} as NativeDatePickerDescriptor
34-
@State fontSize: number | undefined = undefined
34+
@State date: string = new Date().toString()
3535
private cleanUpCallbacks: (() => void)[] = []
3636
private eventEmitter: RNC.NativeDatePickerView.EventEmitter | undefined = undefined
3737
aboutToAppear() {
@@ -47,7 +47,12 @@ export struct NativeDatePickerView {
4747
aboutToDisappear() {
4848
this.cleanUpCallbacks.forEach(cb => cb())
4949
}
50-
50+
addZero(e:number) : string{
51+
if(e < 10){
52+
return 0 +''+e
53+
}
54+
return e +''
55+
}
5156
build() {
5257
RNViewBase({ ctx: this.ctx, tag: this.tag }) {
5358
if(!this.descriptor.rawProps.modal){
@@ -58,12 +63,42 @@ export struct NativeDatePickerView {
5863
end: new Date(this.descriptor.rawProps.maximumDate),
5964
}).onDateChange((value: Date) => {
6065
this.eventEmitter!.emit("dateChange", {timestamp: JSON.stringify(value)})
61-
}).height(300)
66+
}).height(200)
67+
}
68+
else if(this.descriptor.rawProps.mode === 'datetime'){
69+
Column(){
70+
Flex() { // 子组件在容器主轴上反向行布局
71+
DatePicker({
72+
start: new Date(this.descriptor.rawProps.minimumDate),
73+
end: new Date(this.descriptor.rawProps.maximumDate),
74+
}).onDateChange((value: Date) => {
75+
let y = new Date(value).getFullYear()
76+
let M = this.addZero(new Date(value).getMonth() + 1)
77+
let d = new Date(value).getDate()
78+
let h = this.addZero(new Date(this.date).getHours())
79+
let m = this.addZero(new Date(this.date).getMinutes())
80+
let s = this.addZero(new Date(this.date).getSeconds())
81+
let stDate = y + '-' + M + '-' + d + ' '+ h + ':' + m + ':' + s
82+
Logger.info(stDate, 'datetimeChange')
83+
this.date = new Date(stDate).toString()
84+
this.eventEmitter!.emit("dateChange", { timestamp: stDate })
85+
}).width('53%').height(200)
86+
TimePicker().onChange((value: TimePickerResult) => {
87+
let y = new Date(this.date).getFullYear()
88+
let M = this.addZero(new Date(this.date).getMonth() + 1)
89+
let d = new Date(this.date).getDate()
90+
let stDate = y + '-' + M + '-' + d + '-' +' '+ value.hour+':'+ value.minute+':'+ '00'
91+
this.date = new Date(stDate).toString()
92+
Logger.info(stDate, 'datetimeChange')
93+
this.eventEmitter!.emit("dateChange", { timestamp: this.date })
94+
}).width('47%').height(200)
95+
}
96+
}
6297
}
6398
else if(this.descriptor.rawProps.mode === 'time'){
6499
TimePicker().onChange((value: TimePickerResult) => {
65100
this.eventEmitter!.emit("dateChange", {timestamp: JSON.stringify(value)})
66-
}).height(300)
101+
}).height(200)
67102
}
68103
}else{
69104
// modal
@@ -129,7 +164,6 @@ export struct NativeDatePickerView {
129164
})
130165
})
131166
}
132-
133167
}
134168
}
135169
}

0 commit comments

Comments
 (0)