Skip to content

fix(carbon): fix time picker initial value #1157

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/carbon-component-mapper/demo/demo-schemas/sandbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -453,12 +453,14 @@ const output = {
name: 'date_time_control_1',
label: 'Timepicker',
title: 'Timepicker',
twelveHoursFormat: true,
component: components.TIME_PICKER
},
{
name: 'date_time_control_2',
label: 'Timepicker with past days',
title: 'Timepicker with past days',
twelveHoursFormat: true,
component: components.TIME_PICKER
}
],
Expand Down
6 changes: 5 additions & 1 deletion packages/carbon-component-mapper/demo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ class App extends React.Component {
}

render() {
const date = new Date();
date.setHours(18);
const date2 = new Date();
date2.setHours(0,0,0,0);
return (
<div style={{ widht: '100%' }}>
<div style={{ maxWidth: 800, marginLeft: 'auto', marginRight: 'auto' }}>
Expand All @@ -37,7 +41,7 @@ class App extends React.Component {
Wizard
</Button>
<Button onClick={() => this.setState((state) => fieldArrayState)}>arraySchema</Button>
<Button onClick={() => this.setState((state) => ({ schema: sandboxSchema, additionalOptions: {} }))}>Sandbox</Button>
<Button onClick={() => this.setState((state) => ({ schema: sandboxSchema, additionalOptions: { initialValues: { date_time_control_1: date, date_time_control_2: date2} } }))}>Sandbox</Button>
<Button onClick={() => this.setState((state) => ({ schema: demoSchema, additionalOptions: {} }))}>Super schema</Button>
</div>
<FormRenderer
Expand Down
34 changes: 34 additions & 0 deletions packages/carbon-component-mapper/src/tests/time-picker.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,4 +207,38 @@ describe('TimePicker', () => {
expect(onSubmit.mock.calls[0][0]['time-picker'].getHours()).toEqual(10);
expect(onSubmit.mock.calls[0][0]['time-picker'].getMinutes()).toEqual(35);
});

it('handles initial value', async () => {
const date = new Date().setHours(18, 57, 0, 0);
schema = {
fields: [
{
component: componentTypes.TIME_PICKER,
name: 'time-picker',
initialValue: date,
twelveHoursFormat: true,
timezones: [
{ label: 'UTC', value: 'UTC' },
{ label: 'EST', value: 'EAST' },
],
},
],
};

wrapper = mount(<FormRenderer schema={schema} {...initialProps} />);

expect(wrapper.find('input').props().value).toEqual(new Date().setHours(18, 57, 0, 0));

await act(async () => {
wrapper.find('input').simulate('change', { target: { value: '00:35' } });
});
wrapper.update();

await act(async () => {
wrapper.find('form').simulate('submit');
});
wrapper.update();

expect(onSubmit).toHaveBeenLastCalledWith({ 'time-picker': '00:35' });
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import HelperTextBlock from '../helper-text-block/helper-text-block';
const TimePickerBase = ({
WrapperProps,
input,
format,
enhnancedOnBlur,
enhancedOnChange,
finalValue,
Expand All @@ -20,7 +21,7 @@ const TimePickerBase = ({
selectTimezone,
...rest
}) => (
<div {...WrapperProps}>
<div {...WrapperProps}>
<CarbonTimePicker
{...input}
{...(enhnancedOnBlur && { onBlur: enhnancedOnBlur })}
Expand All @@ -34,7 +35,7 @@ const TimePickerBase = ({
{...rest}
>
{twelveHoursFormat && (
<TimePickerSelect labelText="Period" id={`${rest.id || input.name}-12h`} onChange={({ target: { value } }) => selectFormat(value)}>
<TimePickerSelect defaultValue={format} labelText="Period" id={`${rest.id || input.name}-12h`} onChange={({ target: { value } }) => selectFormat(value)}>
<SelectItem value="AM" text="AM" />
<SelectItem value="PM" text="PM" />
</TimePickerSelect>
Expand All @@ -49,7 +50,8 @@ const TimePickerBase = ({
</CarbonTimePicker>
<HelperTextBlock helperText={!invalid && helperText} warnText={warnText} />
</div>
);
);


TimePickerBase.propTypes = {
isDisabled: PropTypes.bool,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ import TimePickerBase from '../time-picker-base';

const TimePickerDate = (props) => {
const { input, meta, twelveHoursFormat, timezones, validateOnMount, helperText, WrapperProps, ...rest } = useFieldApi(prepareProps(props));

const [timezone, selectTimezone] = useState(timezones ? timezones[0]?.value : '');
const [format, selectFormat] = useState('AM');
const [format, selectFormat] = useState(() => input.value?.getHours?.() >= 12 ? "PM" : "AM");
const isMounted = useRef(false);

const invalid = (meta.touched || validateOnMount) && (meta.error || meta.submitError);
Expand Down Expand Up @@ -66,6 +65,7 @@ const TimePickerDate = (props) => {
<TimePickerBase
WrapperProps={WrapperProps}
input={input}
format={format}
enhnancedOnBlur={enhnancedOnBlur}
finalValue={finalValue}
invalid={invalid}
Expand Down