superdesk-ui-framework 4.0.46 → 4.0.47

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.
@@ -15,7 +15,7 @@ jobs:
15
15
 
16
16
  - uses: actions/setup-node@v1
17
17
  with:
18
- node-version: '12.x'
18
+ node-version: '14.x'
19
19
 
20
20
  - uses: actions/cache@v3
21
21
  with:
@@ -7,122 +7,183 @@ import { IconButton } from "./IconButton";
7
7
  import { InputWrapper } from "./Form";
8
8
  import { IInputWrapper } from "./Form/InputWrapper";
9
9
  import nextId from "react-id-generator";
10
+ import {format} from 'date-fns';
11
+ import {assertNever} from '../helpers';
10
12
 
11
- interface IProps extends IInputWrapper {
12
- value: Date | null;
13
- dateFormat: string;
14
- onChange: (value: Date | null) => void;
15
- preview?: boolean;
16
- fullWidth?: boolean;
17
- allowSeconds?: boolean;
18
- required?: boolean;
19
- disabled?: boolean;
20
- ref?: React.LegacyRef<InputWrapper>;
21
- 'data-test-id'?: string;
13
+ interface IPropsValueDate extends IInputWrapper {
14
+ valueType: 'date';
15
+ value: Date | null;
16
+ dateFormat: string;
17
+ onChange: (value: Date | null) => void;
18
+ preview?: boolean;
19
+ fullWidth?: boolean;
20
+ allowSeconds?: boolean;
21
+ required?: boolean;
22
+ disabled?: boolean;
23
+ ref?: React.LegacyRef<InputWrapper>;
24
+ 'data-test-id'?: string;
22
25
  }
23
26
 
24
- export class DateTimePicker extends React.PureComponent<IProps> {
25
- private htmlId: string = nextId();
27
+ type IValue = {date?: string; time?: string};
26
28
 
27
- handleTimeChange = (time: string) => {
28
- const [hours, minutes] = time
29
- .split(":")
30
- .map((x) => defaultTo(parseInt(x, 10), 0)); // handle NaN value
31
- const origDate = this.props.value ? new Date(this.props.value) : new Date();
29
+ interface IPropsValueObject extends IInputWrapper {
30
+ valueType: 'object';
31
+ value: IValue;
32
+ dateFormat: string;
33
+ onChange: (value: IValue) => void; //
34
+ preview?: boolean;
35
+ fullWidth?: boolean;
36
+ allowSeconds?: boolean;
37
+ required?: boolean;
38
+ disabled?: boolean;
39
+ ref?: React.LegacyRef<InputWrapper>;
40
+ 'data-test-id'?: string;
41
+ }
32
42
 
33
- origDate.setHours(hours, minutes);
43
+ type IProps = IPropsValueDate | IPropsValueObject;
34
44
 
35
- this.props.onChange(origDate);
36
- }
45
+ export class DateTimePicker extends React.PureComponent<IProps> {
46
+ private htmlId: string = nextId();
37
47
 
38
- handleDateChange = (date: Date | null) => {
39
- if (date == null) {
40
- this.props.onChange(null);
48
+ handleTimeChange = (time: string) => {
49
+ if (this.props.valueType === 'date') {
50
+ const [hours, minutes] = time
51
+ .split(":")
52
+ .map((x) => defaultTo(parseInt(x, 10), 0));
53
+ const origDate = this.props.value ? new Date(this.props.value) : new Date();
41
54
 
42
- return;
55
+ origDate.setHours(hours, minutes);
56
+
57
+ this.props.onChange(origDate);
58
+ } else if (this.props.valueType === 'object') {
59
+ this.props.onChange({
60
+ ...this.props.value,
61
+ time,
62
+ });
63
+ } else {
64
+ assertNever(this.props);
65
+ }
43
66
  }
44
67
 
45
- const origDate = this.props.value ?? new Date();
46
- const selectedDate = new Date(date);
47
-
48
- selectedDate.setHours(origDate.getHours(), origDate.getMinutes());
49
-
50
- this.props.onChange(selectedDate);
51
- }
52
-
53
- prepareFormat(unitOfTime: number) {
54
- return unitOfTime.toString().padStart(2, "0");
55
- }
56
-
57
- render() {
58
- const convertedTimeValue =
59
- this.props.value != null
60
- ? `${this.prepareFormat(
61
- this.props.value.getHours(),
62
- )}:${this.prepareFormat(this.props.value.getMinutes())}`
63
- : "";
64
-
65
- return (
66
- <InputWrapper
67
- label={this.props.label}
68
- error={this.props.error}
69
- invalid={this.props.error != null}
70
- required={this.props.required}
71
- disabled={this.props.disabled}
72
- info={this.props.info}
73
- inlineLabel={this.props.inlineLabel}
74
- labelHidden={this.props.labelHidden}
75
- htmlId={this.htmlId}
76
- tabindex={this.props.tabindex}
77
- fullWidth={this.props.fullWidth}
78
- inputWrapper={this.props.inputWrapper}
79
- data-test-id={this.props["data-test-id"]}
80
- ref={this.props.ref}
81
- >
82
- <Spacer h gap="8" alignItems="end" noWrap>
83
- <div style={{ flexGrow: 1 }}>
84
- <DatePicker
85
- disabled={this.props.disabled}
86
- preview={this.props.preview}
87
- required={this.props.required}
88
- hideClearButton={true}
89
- value={this.props.value}
90
- onChange={(val) => {
91
- this.handleDateChange(val);
92
- }}
93
- dateFormat={this.props.dateFormat}
94
- inlineLabel
95
- labelHidden
96
- fullWidth={this.props.fullWidth}
97
- />
98
- </div>
99
- <div style={{ flexGrow: 1 }}>
100
- <TimePicker
101
- disabled={this.props.disabled}
102
- preview={this.props.preview}
103
- value={convertedTimeValue}
104
- onChange={(val) => {
105
- this.handleTimeChange(val);
106
- }}
107
- inlineLabel
108
- labelHidden
109
- allowSeconds={this.props.allowSeconds}
110
- fullWidth={this.props.fullWidth}
111
- required={this.props.required}
112
- />
113
- </div>
114
- {this.props.preview !== true && (
115
- <IconButton
116
- disabled={this.props.disabled}
117
- icon="remove-sign"
118
- onClick={() => {
68
+ handleDateChange = (date: Date | null) => {
69
+ if (this.props.valueType === 'date') {
70
+ if (date == null) {
119
71
  this.props.onChange(null);
120
- }}
121
- ariaValue="Clear"
122
- />
123
- )}
124
- </Spacer>
125
- </InputWrapper>
126
- );
127
- }
72
+ return;
73
+ }
74
+
75
+ const origDate = this.props.value ?? new Date();
76
+ const selectedDate = new Date(date);
77
+
78
+ selectedDate.setHours(origDate.getHours(), origDate.getMinutes());
79
+
80
+ this.props.onChange(selectedDate);
81
+ } else if (this.props.valueType === 'object') {
82
+ this.props.onChange({
83
+ ...this.props.value,
84
+ date: date ? format(date, 'yyyy-MM-dd') : undefined,
85
+ });
86
+ } else {
87
+ assertNever(this.props);
88
+ }
89
+ }
90
+
91
+ prepareFormat(unitOfTime: number) {
92
+ return unitOfTime.toString().padStart(2, "0");
93
+ }
94
+
95
+ getTimeValue(): string {
96
+ if (this.props.valueType === 'date') {
97
+
98
+ return this.props.value != null
99
+ ? `${this.prepareFormat(this.props.value.getHours())}:${this.prepareFormat(this.props.value.getMinutes())}`
100
+ : "";
101
+ } else if (this.props.valueType === 'object') {
102
+ return this.props.value.time ?? '';
103
+ } else {
104
+ assertNever(this.props);
105
+ }
106
+ }
107
+
108
+ getDateValue(): Date | null {
109
+ if (this.props.valueType === 'date') {
110
+ return this.props.value;
111
+ } else if (this.props.valueType === 'object') {
112
+ return this.props.value.date ? new Date(this.props.value.date) : null;
113
+ } else {
114
+ assertNever(this.props);
115
+ }
116
+ }
117
+
118
+ handleClear = () => {
119
+ if (this.props.valueType === 'date') {
120
+ this.props.onChange(null);
121
+ } else if (this.props.valueType === 'object') {
122
+ this.props.onChange({date: undefined, time: undefined});
123
+ } else {
124
+ assertNever(this.props);
125
+ }
126
+ }
127
+
128
+ render() {
129
+ const timeValue = this.getTimeValue();
130
+ const dateValue = this.getDateValue();
131
+
132
+ return (
133
+ <InputWrapper
134
+ label={this.props.label}
135
+ error={this.props.error}
136
+ invalid={this.props.error != null}
137
+ required={this.props.required}
138
+ disabled={this.props.disabled}
139
+ info={this.props.info}
140
+ inlineLabel={this.props.inlineLabel}
141
+ labelHidden={this.props.labelHidden}
142
+ htmlId={this.htmlId}
143
+ tabindex={this.props.tabindex}
144
+ fullWidth={this.props.fullWidth}
145
+ inputWrapper={this.props.inputWrapper}
146
+ data-test-id={this.props["data-test-id"]}
147
+ ref={this.props.ref}
148
+ >
149
+ <Spacer h gap="8" alignItems="end" noWrap>
150
+ <div style={{ flexGrow: 1 }}>
151
+ <DatePicker
152
+ disabled={this.props.disabled}
153
+ preview={this.props.preview}
154
+ required={this.props.required}
155
+ hideClearButton={true}
156
+ value={dateValue}
157
+ onChange={this.handleDateChange}
158
+ dateFormat={this.props.dateFormat}
159
+ inlineLabel
160
+ labelHidden
161
+ fullWidth={this.props.fullWidth}
162
+ />
163
+ </div>
164
+ <div style={{ flexGrow: 1 }}>
165
+ <TimePicker
166
+ disabled={this.props.disabled}
167
+ preview={this.props.preview}
168
+ value={timeValue}
169
+ onChange={this.handleTimeChange}
170
+ inlineLabel
171
+ labelHidden
172
+ allowSeconds={this.props.allowSeconds}
173
+ fullWidth={this.props.fullWidth}
174
+ required={this.props.required}
175
+ />
176
+ </div>
177
+ {this.props.preview !== true && (
178
+ <IconButton
179
+ disabled={this.props.disabled}
180
+ icon="remove-sign"
181
+ onClick={this.handleClear}
182
+ ariaValue="Clear"
183
+ />
184
+ )}
185
+ </Spacer>
186
+ </InputWrapper>
187
+ );
188
+ }
128
189
  }