Skip to content

Commit 7b53431

Browse files
committed
fix(carbon): fix initial value for date timepicker
1 parent a8018e0 commit 7b53431

File tree

8 files changed

+74
-10
lines changed

8 files changed

+74
-10
lines changed

packages/carbon-component-mapper/src/tests/time-picker-string.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ describe('TimePicker<String>', () => {
193193
onSubmit.mockReset();
194194

195195
await act(async () => {
196-
wrapper.find('select#time-picker-12h').simulate('change', { target: { value: 'UTC' } });
196+
wrapper.find('select#time-picker-timezones').simulate('change', { target: { value: 'UTC' } });
197197
});
198198
wrapper.update();
199199

@@ -203,7 +203,7 @@ describe('TimePicker<String>', () => {
203203
wrapper.update();
204204

205205
expect(wrapper.find('input').props().value).toEqual('00:35');
206-
expect(onSubmit).toHaveBeenLastCalledWith({ 'time-picker': '00:35 UTC EST' });
206+
expect(onSubmit).toHaveBeenLastCalledWith({ 'time-picker': '00:35 AM UTC' });
207207
});
208208

209209
it('handles initial value', async () => {

packages/carbon-component-mapper/src/tests/time-picker.test.js

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ describe('TimePicker', () => {
194194
onSubmit.mockReset();
195195

196196
await act(async () => {
197-
wrapper.find('select#time-picker-12h').simulate('change', { target: { value: 'UTC' } });
197+
wrapper.find('select#time-picker-timezones').simulate('change', { target: { value: 'UTC' } });
198198
});
199199
wrapper.update();
200200

@@ -203,8 +203,47 @@ describe('TimePicker', () => {
203203
});
204204
wrapper.update();
205205

206-
expect(wrapper.find('input').props().value).toEqual('10:35');
207-
expect(onSubmit.mock.calls[0][0]['time-picker'].getHours()).toEqual(10);
206+
expect(wrapper.find('input').props().value).toEqual('05:35');
207+
expect(onSubmit.mock.calls[0][0]['time-picker'].getHours()).toEqual(5);
208208
expect(onSubmit.mock.calls[0][0]['time-picker'].getMinutes()).toEqual(35);
209209
});
210+
211+
it('handles initial value', async () => {
212+
schema = {
213+
fields: [
214+
{
215+
component: componentTypes.TIME_PICKER,
216+
name: 'time-picker',
217+
initialValue: new Date('December 17, 1995 16:00:00'),
218+
twelveHoursFormat: true,
219+
},
220+
],
221+
};
222+
223+
await act(async () => {
224+
wrapper = mount(<FormRenderer schema={schema} {...initialProps} />);
225+
});
226+
wrapper.update();
227+
228+
expect(wrapper.find('input').props().value).toEqual('04:00');
229+
expect(wrapper.find('select').props().defaultValue).toEqual('PM');
230+
231+
await act(async () => {
232+
wrapper.find('input').simulate('change', { target: { value: '03:00' } });
233+
});
234+
wrapper.update();
235+
236+
await act(async () => {
237+
wrapper.find('input').simulate('blur');
238+
});
239+
wrapper.update();
240+
241+
await act(async () => {
242+
wrapper.find('form').simulate('submit');
243+
});
244+
wrapper.update();
245+
246+
expect(onSubmit.mock.calls[0][0]['time-picker'].getHours()).toEqual(15);
247+
expect(onSubmit.mock.calls[0][0]['time-picker'].getMinutes()).toEqual(0);
248+
});
210249
});

packages/carbon-component-mapper/src/time-picker-base/time-picker-base.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ interface InternalTimePickerBaseProps extends CarbonTimePickerProps, AnyObject {
2020
warnText?: ReactNode;
2121
selectFormat: (value: 'AM' | 'PM') => void;
2222
selectTimezone: (value: string) => void;
23+
format?: 'AM' | 'PM';
24+
defaultTimezone?: string;
2325
}
2426

2527
export type TimePickerBaseProps = InternalTimePickerBaseProps & FormGroupProps;

packages/carbon-component-mapper/src/time-picker-base/time-picker-base.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ const TimePickerBase = ({
1818
warnText,
1919
selectFormat,
2020
selectTimezone,
21+
format,
22+
timezone,
2123
...rest
2224
}) => (
2325
<div {...WrapperProps}>
@@ -34,13 +36,23 @@ const TimePickerBase = ({
3436
{...rest}
3537
>
3638
{twelveHoursFormat && (
37-
<TimePickerSelect labelText="Period" id={`${rest.id || input.name}-12h`} onChange={({ target: { value } }) => selectFormat(value)}>
39+
<TimePickerSelect
40+
defaultValue={format}
41+
labelText="Period"
42+
id={`${rest.id || input.name}-12h`}
43+
onChange={({ target: { value } }) => selectFormat(value)}
44+
>
3845
<SelectItem value="AM" text="AM" />
3946
<SelectItem value="PM" text="PM" />
4047
</TimePickerSelect>
4148
)}
4249
{timezones && (
43-
<TimePickerSelect labelText="Timezone" id={`${rest.id || input.name}-timezones`} onChange={({ target: { value } }) => selectTimezone(value)}>
50+
<TimePickerSelect
51+
defaultValue={timezone}
52+
labelText="Timezone"
53+
id={`${rest.id || input.name}-timezones`}
54+
onChange={({ target: { value } }) => selectTimezone(value)}
55+
>
4456
{timezones.map(({ showAs, ...tz }) => (
4557
<SelectItem key={tz.value} text={tz.label} {...tz} />
4658
))}
@@ -78,6 +90,8 @@ TimePickerBase.propTypes = {
7890
warnText: PropTypes.node,
7991
selectFormat: PropTypes.func.isRequired,
8092
selectTimezone: PropTypes.func.isRequired,
93+
format: PropTypes.oneOf(['AM', 'PM']),
94+
timezone: PropTypes.string,
8195
};
8296

8397
export default TimePickerBase;

packages/carbon-component-mapper/src/time-picker-date/time-picker-date.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export interface Timezone extends SelectItemProps {
1212
interface InternalTimePickerProps extends CarbonTimePickerProps {
1313
twelveHoursFormat?: boolean;
1414
timezones?: Timezone[];
15+
defaultTimezone?: string;
1516
}
1617

1718
export type TimePickerDateProps = InternalTimePickerProps & FormGroupProps & UseFieldApiComponentConfig;

packages/carbon-component-mapper/src/time-picker-date/time-picker-date.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ import prepareProps from '../prepare-props';
66
import TimePickerBase from '../time-picker-base';
77

88
const TimePickerDate = (props) => {
9-
const { input, meta, twelveHoursFormat, timezones, validateOnMount, helperText, WrapperProps, ...rest } = useFieldApi(prepareProps(props));
9+
const { input, meta, twelveHoursFormat, timezones, validateOnMount, helperText, WrapperProps, defaultTimezone, ...rest } = useFieldApi(
10+
prepareProps(props)
11+
);
1012

11-
const [timezone, selectTimezone] = useState(timezones ? timezones[0]?.value : '');
12-
const [format, selectFormat] = useState('AM');
13+
const [timezone, selectTimezone] = useState(defaultTimezone || timezones ? timezones[0]?.value : '');
14+
const [format, selectFormat] = useState(() => (input.value?.getHours?.() >= 12 ? 'PM' : 'AM'));
1315
const isMounted = useRef(false);
1416

1517
const invalid = (meta.touched || validateOnMount) && (meta.error || meta.submitError);
@@ -75,6 +77,8 @@ const TimePickerDate = (props) => {
7577
warnText={warnText}
7678
selectFormat={selectFormat}
7779
selectTimezone={selectTimezone}
80+
format={format}
81+
timezone={timezone}
7882
{...rest}
7983
/>
8084
);
@@ -96,6 +100,7 @@ TimePickerDate.propTypes = {
96100
})
97101
),
98102
WrapperProps: PropTypes.object,
103+
defaultTimezone: PropTypes.string,
99104
};
100105

101106
export default TimePickerDate;

packages/carbon-component-mapper/src/time-picker-string/time-picker-string.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ const TimePickerString = (props) => {
4242
warnText={warnText}
4343
selectFormat={selectFormat}
4444
selectTimezone={selectTimezone}
45+
format={format}
46+
timezone={timezone}
4547
{...rest}
4648
/>
4749
);

packages/react-renderer-demo/src/doc-components/examples-texts/carbon/time-picker.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ This component also accepts all other original props, please see [here](https://
44

55
|Props|Description|default|
66
|-----|-----------|-------|
7+
|defaultTimezone|string - a value of default timezone, use only in Date varian|undefined|
78
|useStringFormat|boolean - save value as string|false|
89
|twelveHoursFormat|boolean - if true an AM/PM selector is shown|false|
910
|timezones|array of timezones - if not empty, an timezone selector is shown|undefined|

0 commit comments

Comments
 (0)