Skip to content

Commit

Permalink
TimeInput: Expose as subcomponent of TimePicker (WordPress#63145)
Browse files Browse the repository at this point in the history
* TimeInput: Make subcomponent of TimePicker

* Add changelog

* Hide Storybook controls for TimeInput story

* Add explicit font-size to colon

Co-authored-by: mirka <0mirka00@git.wordpress.org>
Co-authored-by: tyxla <tyxla@git.wordpress.org>
Co-authored-by: ciampo <mciampini@git.wordpress.org>
Co-authored-by: t-hamano <wildworks@git.wordpress.org>
Co-authored-by: bogiii <bogdannikolic@git.wordpress.org>
Co-authored-by: Tropicalista <tropicalista@git.wordpress.org>
  • Loading branch information
7 people authored Jul 31, 2024
1 parent 635c134 commit f0874ac
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 48 deletions.
1 change: 1 addition & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

### Enhancements

- `TimeInput`: Expose as subcomponent of `TimePicker` ([#63145](https://github.com/WordPress/gutenberg/pull/63145)).
- `RadioControl`: add support for option help text ([#63751](https://github.com/WordPress/gutenberg/pull/63751)).

### Internal
Expand Down
3 changes: 1 addition & 2 deletions packages/components/src/date-time/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
*/
import { default as DatePicker } from './date';
import { default as TimePicker } from './time';
import { default as TimeInput } from './time-input';
import { default as DateTimePicker } from './date-time';

export { DatePicker, TimePicker, TimeInput };
export { DatePicker, TimePicker };
export default DateTimePicker;
36 changes: 0 additions & 36 deletions packages/components/src/date-time/stories/time-input.story.tsx

This file was deleted.

17 changes: 17 additions & 0 deletions packages/components/src/date-time/stories/time.story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import TimePicker from '../time';
const meta: Meta< typeof TimePicker > = {
title: 'Components/TimePicker',
component: TimePicker,
// @ts-expect-error - See https://github.com/storybookjs/storybook/issues/23170
subcomponents: { 'TimePicker.TimeInput': TimePicker.TimeInput },
argTypes: {
currentTime: { control: 'date' },
onChange: { action: 'onChange', control: { type: null } },
Expand Down Expand Up @@ -49,3 +51,18 @@ const Template: StoryFn< typeof TimePicker > = ( {
};

export const Default: StoryFn< typeof TimePicker > = Template.bind( {} );

const TimeInputTemplate: StoryFn< typeof TimePicker.TimeInput > = ( args ) => {
return <TimePicker.TimeInput { ...args } />;
};

/**
* The time input can be used in isolation as `<TimePicker.TimeInput />`. In this case, the `value` will be passed
* as an object in 24-hour format (`{ hours: number, minutes: number }`).
*/
export const TimeInput = TimeInputTemplate.bind( {} );
TimeInput.args = {
label: 'Time',
};
// Hide TimePicker controls because they don't apply to TimeInput
TimeInput.parameters = { controls: { include: [] } };
27 changes: 26 additions & 1 deletion packages/components/src/date-time/time/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import {
validateInputElementTarget,
} from '../utils';
import { TIMEZONELESS_FORMAT } from '../constants';
import { TimeInput } from '../time-input';
import { TimeInput } from './time-input';

const VALID_DATE_ORDERS = [ 'dmy', 'mdy', 'ymd' ];

Expand Down Expand Up @@ -252,4 +252,29 @@ export function TimePicker( {
);
}

/**
* A component to input a time.
*
* Values are passed as an object in 24-hour format (`{ hours: number, minutes: number }`).
*
* ```jsx
* import { TimePicker } from '@wordpress/components';
* import { useState } from '@wordpress/element';
*
* const MyTimeInput = () => {
* const [ time, setTime ] = useState( { hours: 13, minutes: 30 } );
*
* return (
* <TimePicker.TimeInput
* value={ time }
* onChange={ setTime }
* label="Time"
* />
* );
* };
* ```
*/
TimePicker.TimeInput = TimeInput;
Object.assign( TimePicker.TimeInput, { displayName: 'TimePicker.TimeInput' } );

export default TimePicker;
1 change: 1 addition & 0 deletions packages/components/src/date-time/time/styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export const HoursInput = styled( NumberControl )`
export const TimeSeparator = styled.span`
border-top: ${ CONFIG.borderWidth } solid ${ COLORS.gray[ 700 ] };
border-bottom: ${ CONFIG.borderWidth } solid ${ COLORS.gray[ 700 ] };
font-size: ${ CONFIG.fontSize };
line-height: calc(
${ CONFIG.controlHeight } - ${ CONFIG.borderWidth } * 2
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,20 @@ import {
HoursInput,
MinutesInput,
Fieldset,
} from '../time/styles';
import { HStack } from '../../h-stack';
import Button from '../../button';
import ButtonGroup from '../../button-group';
} from '../styles';
import { HStack } from '../../../h-stack';
import Button from '../../../button';
import ButtonGroup from '../../../button-group';
import {
from12hTo24h,
from24hTo12h,
buildPadInputStateReducer,
validateInputElementTarget,
} from '../utils';
import type { TimeInputProps } from '../types';
import type { InputChangeCallback } from '../../input-control/types';
import { useControlledValue } from '../../utils';
import BaseControl from '../../base-control';
} from '../../utils';
import type { TimeInputProps } from '../../types';
import type { InputChangeCallback } from '../../../input-control/types';
import { useControlledValue } from '../../../utils';
import BaseControl from '../../../base-control';

export function TimeInput( {
value: valueProp,
Expand Down

0 comments on commit f0874ac

Please sign in to comment.