Skip to content
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

chore(DateTimePicker): convert demo to typescript #7268

Merged
Merged
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
104 changes: 1 addition & 103 deletions packages/react-core/src/demos/DateTimePicker.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,107 +13,5 @@ In this demo, learn how to use a [CalendarMonth](/components/calendar-month) com

### Date and time picker

```js
import React from 'react';
import {
Dropdown,
DropdownToggle,
DropdownItem,
CalendarMonth,
InputGroup,
TextInput,
Button,
Popover
} from '@patternfly/react-core';
import OutlinedCalendarAltIcon from '@patternfly/react-icons/dist/esm/icons/outlined-calendar-alt-icon';
import OutlinedClockIcon from '@patternfly/react-icons/dist/esm/icons/outlined-clock-icon';

DateTimePicker = () => {
const [isCalendarOpen, setIsCalendarOpen] = React.useState(false);
const [isTimeOpen, setIsTimeOpen] = React.useState(false);
const [valueDate, setValueDate] = React.useState('MM-DD-YYYY');
const [valueTime, setValueTime] = React.useState('HH:MM');
const times = Array.from(new Array(10), (_, i) => i + 8);
const defaultTime = '0:00';
const dateFormat = date =>
date.toLocaleDateString('en-US', { year: 'numeric', month: '2-digit', day: '2-digit' }).replace(/\//g, '-');

const onToggleCalendar = () => {
setIsCalendarOpen(!isCalendarOpen);
setIsTimeOpen(false);
};

const onToggleTime = ev => {
setIsTimeOpen(!isTimeOpen);
setIsCalendarOpen(false);
};

const onSelectCalendar = newValueDate => {
const newValue = dateFormat(newValueDate);
setValueDate(newValue);
setIsCalendarOpen(!isCalendarOpen);
// setting default time when it is not picked
if (valueTime === 'HH:MM') {
setValueTime(defaultTime);
}
};

const onSelectTime = ev => {
setValueTime(ev.target.value);
setIsTimeOpen(!isTimeOpen);
};

const timeOptions = times.map(time => (
<DropdownItem key={time} component="button" value={`${time}:00`}>
{`${time}:00`}
</DropdownItem>
));
const calendar = <CalendarMonth date={valueDate} onChange={onSelectCalendar} />;
const time = (
<Dropdown
onSelect={onSelectTime}
toggle={
<DropdownToggle
aria-label="Toggle the time picker menu"
toggleIndicator={null}
onToggle={onToggleTime}
style={{ padding: '6px 16px' }}
>
<OutlinedClockIcon />
</DropdownToggle>
}
isOpen={isTimeOpen}
dropdownItems={timeOptions}
/>
);
const calendarButton = (
<Button variant="control" aria-label="Toggle the calendar" onClick={onToggleCalendar}>
<OutlinedCalendarAltIcon />
</Button>
);
return (
<div style={{ width: '300px' }}>
<Popover
position="bottom"
bodyContent={calendar}
showClose={false}
isVisible={isCalendarOpen}
hasNoPadding
hasAutoWidth
>
<InputGroup>
<TextInput
type="'datetime-local"
id="date-time"
aria-label="date and time picker demo"
value={valueDate + ' ' + valueTime}
isReadOnly
/>
{calendarButton}
{time}
</InputGroup>
</Popover>
</div>
);
};
```ts file="./examples/DateTimePicker/DateTimePicker.tsx"
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import React from 'react';
import {
Dropdown,
DropdownToggle,
DropdownItem,
CalendarMonth,
InputGroup,
TextInput,
Button,
Popover
} from '@patternfly/react-core';
import OutlinedCalendarAltIcon from '@patternfly/react-icons/dist/esm/icons/outlined-calendar-alt-icon';
import OutlinedClockIcon from '@patternfly/react-icons/dist/esm/icons/outlined-clock-icon';

export const DateTimePicker: React.FunctionComponent = () => {
const [isCalendarOpen, setIsCalendarOpen] = React.useState(false);
const [isTimeOpen, setIsTimeOpen] = React.useState(false);
const [valueDate, setValueDate] = React.useState('MM-DD-YYYY');
const [valueTime, setValueTime] = React.useState('HH:MM');
const times = Array.from(new Array(10), (_, i) => i + 8);
const defaultTime = '0:00';
const dateFormat = (date: Date) =>
date.toLocaleDateString('en-US', { year: 'numeric', month: '2-digit', day: '2-digit' }).replace(/\//g, '-');

const onToggleCalendar = () => {
setIsCalendarOpen(!isCalendarOpen);
setIsTimeOpen(false);
};

const onToggleTime = (_value: boolean, _event: any) => {
setIsTimeOpen(!isTimeOpen);
setIsCalendarOpen(false);
};

const onSelectCalendar = (newValueDate: Date) => {
const newValue = dateFormat(newValueDate);
setValueDate(newValue);
setIsCalendarOpen(!isCalendarOpen);
// setting default time when it is not picked
if (valueTime === 'HH:MM') {
setValueTime(defaultTime);
}
};

const onSelectTime = (ev: React.SyntheticEvent<HTMLDivElement>) => {
setValueTime(ev.currentTarget.textContent);
setIsTimeOpen(!isTimeOpen);
};

const timeOptions = times.map(time => (
<DropdownItem key={time} component="button" value={`${time}:00`}>
{`${time}:00`}
</DropdownItem>
));

const calendar = <CalendarMonth date={new Date(valueDate)} onChange={onSelectCalendar} />;

const time = (
<Dropdown
onSelect={onSelectTime}
toggle={
<DropdownToggle
aria-label="Toggle the time picker menu"
toggleIndicator={null}
onToggle={onToggleTime}
style={{ padding: '6px 16px' }}
>
<OutlinedClockIcon />
</DropdownToggle>
}
isOpen={isTimeOpen}
dropdownItems={timeOptions}
/>
);

const calendarButton = (
<Button variant="control" aria-label="Toggle the calendar" onClick={onToggleCalendar}>
<OutlinedCalendarAltIcon />
</Button>
);

return (
<div style={{ width: '300px' }}>
<Popover
position="bottom"
bodyContent={calendar}
showClose={false}
isVisible={isCalendarOpen}
hasNoPadding
hasAutoWidth
>
<InputGroup>
<TextInput
type="text"
id="date-time"
aria-label="date and time picker demo"
value={valueDate + ' ' + valueTime}
isReadOnly
/>
{calendarButton}
{time}
</InputGroup>
</Popover>
</div>
);
};