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

feat: [DHIS2-15463] Use dhis2 ui calendarInput component in working list #3712

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 17 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
Original file line number Diff line number Diff line change
Expand Up @@ -249,9 +249,9 @@ Then('the list should display data ordered descendingly by report date', () => {
.click();

cy.get('input[placeholder="From"]')
.type(`${lastYear}-01-01`);
.type(`${lastYear}-01-01`).blur();

cy.get('input[placeholder="To"]').click();
cy.get('input[placeholder="To"]').click().blur();

cy.contains('Update')
.click({ force: true });
Expand Down Expand Up @@ -398,10 +398,10 @@ When('you set the date of admission filter', () => {
cy.get('input[type="text"]')
.then(($elements) => {
cy.wrap($elements[0])
.type('2018-01-01');
.type('2018-01-01').blur();

cy.wrap($elements[1])
.type('2018-12-31');
.type('2018-12-31').blur();
});

cy.contains('Update')
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"@dhis2/d2-ui-rich-text": "^7.4.0",
"@dhis2/d2-ui-sharing-dialog": "^7.3.3",
"@dhis2/ui": "^9.10.1",
"@dhis2-ui/calendar": "9.9.0-alpha.6",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alphas releases are useful when developing, but IMHO it is best to wait for the stable @dhis2/ui release before merging this PR. Do you know the timeline/steps to get the stable release to include the alpha changes?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was given the task to update the alpha with latest master. But I am not sure about the timeline as there are still accessibility task for CalendarInput.

"@joakim_sm/react-infinite-calendar": "^2.4.2",
"@material-ui/core": "3.9.4",
"@material-ui/icons": "3",
Expand Down Expand Up @@ -136,6 +137,7 @@
"@dhis2/app-runtime": "^3.10.2",
"@babel/preset-react": "7.16.7",
"@dhis2/ui": "^9.10.1",
"@dhis2-ui/calendar": "9.9.0-alpha.6",
"@js-temporal/polyfill": "0.4.3",
"core-js": "2.5.7",
"i18next": "^20.5.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -302,11 +302,11 @@ class DateFilterPlain extends Component<Props, State> implements UpdatableFilter
}

handleEnterKeyInFrom = () => {
this.toD2DateTextFieldInstance.focus();
this.toD2DateTextFieldInstance && this.toD2DateTextFieldInstance.focus();
};

handleDateSelectedFromCalendarInFrom = () => {
this.toD2DateTextFieldInstance.focus();
this.toD2DateTextFieldInstance && this.toD2DateTextFieldInstance.focus();
};

handleEnterKeyInTo = (value: { [key: string]: string }) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import React, { Component } from 'react';
import i18n from '@dhis2/d2-i18n';
import { D2Date } from '../../FormFields/DateAndTime/D2Date/D2Date.component';
import { modes, absoluteDirections } from '../../FormFields/DateAndTime/D2Date/d2DatePopup.const';
import { withInternalChangeHandler } from '../../FormFields/withInternalChangeHandler';

type Props = {
Expand Down Expand Up @@ -30,7 +29,7 @@ class FromDateFilterPlain extends Component<Props> {
this.props.onBlur(FromDateFilterPlain.getValueObject(value));
}

handleKeyPress = (event: SyntheticKeyboardEvent<HTMLInputElement>) => {
handleKeyDown = (event: SyntheticKeyboardEvent<HTMLInputElement>) => {
if (event.key === 'Enter') {
this.props.onEnterKey();
}
Expand All @@ -42,16 +41,11 @@ class FromDateFilterPlain extends Component<Props> {
<div>
{/* $FlowFixMe[cannot-spread-inexact] automated comment */}
<D2Date
onKeyPress={this.handleKeyPress}
onKeyDown={this.handleKeyDown}
onBlur={this.handleBlur}
placeholder={i18n.t('From')}
popupMode={modes.ABSOLUTE}
popupAbsoluteDirection={absoluteDirections.UP}
width={150}
calendarWidth={330}
calendarHeight={170}
calendarRowHeight={45}
calendarDisplayOptions={this.displayOptions}
{...passOnProps}
/>
<div className={errorClass}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import React, { Component } from 'react';
import i18n from '@dhis2/d2-i18n';
import { D2Date } from '../../FormFields/DateAndTime/D2Date/D2Date.component';
import { anchorPositions, modes, absoluteDirections } from '../../FormFields/DateAndTime/D2Date/d2DatePopup.const';
import { withInternalChangeHandler } from '../../FormFields/withInternalChangeHandler';

type Props = {
Expand Down Expand Up @@ -33,7 +32,7 @@ class ToDateFilterPlain extends Component<Props> {
this.props.onBlur(ToDateFilterPlain.getValueObject(value));
}

handleKeyPress = (event: SyntheticKeyboardEvent<HTMLInputElement>) => {
handleKeyDown = (event: SyntheticKeyboardEvent<HTMLInputElement>) => {
if (event.key === 'Enter') {
this.props.onEnterKey(ToDateFilterPlain.getValueObject(this.props.value || ''));
}
Expand All @@ -49,18 +48,12 @@ class ToDateFilterPlain extends Component<Props> {
<div>
{/* $FlowFixMe[cannot-spread-inexact] automated comment */}
<D2Date
onKeyPress={this.handleKeyPress}
onKeyDown={this.handleKeyDown}
onBlur={this.handleBlur}
onDateSelectedFromCalendar={this.handleDateSelectedFromCalendar}
placeholder={i18n.t('To')}
popupAnchorPosition={anchorPositions.RIGHT}
popupMode={modes.ABSOLUTE}
popupAbsoluteDirection={absoluteDirections.UP}
width={150}
calendarWidth={330}
calendarHeight={170}
calendarRowHeight={45}
calendarDisplayOptions={this.displayOptions}
{...passOnProps}
/>
<div className={errorClass}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,6 @@ export class AgeField extends Component<Props> {
onBlur={this.handleCalendarBlur}
placeholder={i18n.t('mm/dd/yyyy')}
width={350}
calendarMaxMoment={moment()}
/>
</div>
<TextField
Expand Down
Original file line number Diff line number Diff line change
@@ -1,207 +1,87 @@
// @flow
import * as React from 'react';
import { withStyles } from '@material-ui/core/styles';
import { lowerCaseFirstLetter } from 'capture-core-utils/string/lowerCaseFirstLetter';
import { D2TextField } from '../../Generic/D2TextField.component';
import { D2DatePopup } from './D2DatePopup.component';
import { D2DateCalendar } from './D2DateCalendar.component';
import { CalendarInput } from '@dhis2/ui';
import { systemSettingsStore } from '../../../../../capture-core/metaDataMemoryStores';

type Props = {
label: string,
label?: ?string,
value: ?string,
width: number,
calendar?: string,
calendarWidth?: ?number,
calendarHeight?: ?number,
inputWidth?: ?number,
onBlur: (value: string) => void,
onFocus?: ?() => void,
onKeyDown?: ?() => void,
onDateSelectedFromCalendar?: () => void,
classes: Object,
textFieldRef?: (instance: D2TextField) => void,
classes?: Object,
disabled?: boolean,
};

type State = {
popoverOpen: boolean,
date: ?string,
};

const styles = () => ({
textField: {
width: '100%',
},
});

class D2DatePlain extends React.Component<Props, State> {
static splitPassOnProps(passOnProps: ?Object) {
const splittedProps = {
input: {},
popup: {},
calendar: {},
};

if (!passOnProps) {
return splittedProps;
}

return Object
.keys(passOnProps)
.reduce((accSplittedProps, propKey) => {
let propContainer;
if (propKey.startsWith(D2Date.propContainers.CALENDAR)) {
propContainer = D2Date.propContainers.CALENDAR;
} else if (propKey.startsWith(D2Date.propContainers.POPUP)) {
propContainer = D2Date.propContainers.POPUP;
} else {
propContainer = D2Date.propContainers.INPUT;
}

const outputKey = lowerCaseFirstLetter(propKey.replace(propContainer, ''));

accSplittedProps[propContainer][outputKey] = passOnProps[propKey];
return accSplittedProps;
}, splittedProps);
}

containerInstance: ?HTMLElement;
handleTextFieldFocus: () => void;
handleDateSelected: (value: string) => void;
handleTextFieldBlur: (value: string) => void;
hidePopover: () => void;
handleDocumentClick: (event: SyntheticEvent<any>) => void;
export class D2Date extends React.Component<Props, State> {
handleDateSelected: (value: {calendarDateString: string}) => void;

constructor(props: Props) {
super(props);

this.state = {
popoverOpen: false,
date: props.value,
};

this.handleTextFieldFocus = this.handleTextFieldFocus.bind(this);
this.handleDateSelected = this.handleDateSelected.bind(this);
this.handleTextFieldBlur = this.handleTextFieldBlur.bind(this);
this.hidePopover = this.hidePopover.bind(this);
this.handleDocumentClick = this.handleDocumentClick.bind(this);
}

componentWillUnmount() {
// $FlowFixMe[incompatible-call] automated comment
document.removeEventListener('click', this.handleDocumentClick);
}

static propContainers = {
CALENDAR: 'calendar',
POPUP: 'popup',
INPUT: 'input',
};

handleTextFieldFocus() {
// $FlowFixMe[incompatible-call] automated comment
document.removeEventListener('click', this.handleDocumentClick);

this.setState({
popoverOpen: true,
});

this.props.onFocus && this.props.onFocus();
}

handleDateSelected(value: string) {
this.props.onBlur(value);
this.hidePopover();
this.props.onDateSelectedFromCalendar && this.props.onDateSelectedFromCalendar();

// $FlowFixMe[incompatible-call] automated comment
document.removeEventListener('click', this.handleDocumentClick);
}

handleDocumentClick(event) {
if ((event.target && event.target.className &&
event.target.className.startsWith &&
event.target.className.startsWith('Cal__')) ||
(event.target && event.target.className &&
event.target.className.baseVal && event.target.className.baseVal.startsWith('Cal__'))) {
return;
}

this.hidePopover();

// $FlowFixMe[incompatible-call] automated comment
document.removeEventListener('click', this.handleDocumentClick);
}

handleTextFieldBlur(value: string, event) {
this.props.onBlur(value);

if (!event.relatedTarget || event.relatedTarget.className !== 'Cal__Container__root') {
this.hidePopover();
} else {
// $FlowFixMe[incompatible-call] automated comment
document.addEventListener('click', this.handleDocumentClick);
handleDateSelected(value: {calendarDateString: string}) {
const selectedDate = value?.calendarDateString;
this.setState({ date: selectedDate });
if (selectedDate !== undefined) {
this.props.onBlur(selectedDate);
}
}

hidePopover() {
this.setState({
popoverOpen: false,
});
this.props.onDateSelectedFromCalendar && this.props.onDateSelectedFromCalendar();
}

render() {
const {
width,
calendar,
calendarWidth,
calendarHeight,
inputWidth,
classes,
onBlur,
onFocus,
onDateSelectedFromCalendar,
textFieldRef,
value,
disabled,
...passOnProps
} = this.props;
const { popoverOpen } = this.state;

const textFieldRefPropObject = textFieldRef ? { ref: textFieldRef } : null;
const calculatedInputWidth = inputWidth || width;
const calculatedCalendarWidth = calendarWidth || width;
const splittedPassOnProps = D2Date.splitPassOnProps(passOnProps);
const calculatedCalendarHeight = calendarHeight || 350;

const calendarType = calendar || 'gregory';
const format = systemSettingsStore.get().dateFormat;
return (
<div
ref={(containerInstance) => { this.containerInstance = containerInstance; }}
style={{
width,
}}
>
{/* $FlowFixMe[incompatible-type] automated comment */}
<D2TextField
{...textFieldRefPropObject}
onFocus={this.handleTextFieldFocus}
onBlur={this.handleTextFieldBlur}
className={classes.textField}
width={calculatedInputWidth}
{...splittedPassOnProps.input}
<CalendarInput
{...passOnProps}
label={this.props.label}
format={format}
onDateSelect={this.handleDateSelected}
calendar={calendarType}
date={this.state.date}
width={String(calculatedCalendarWidth)}
inputWidth={String(calculatedInputWidth)}
onFocus={onFocus}
disabled={disabled}
/>
<D2DatePopup
open={popoverOpen}
onClose={this.hidePopover}
width={calculatedCalendarWidth}
height={calculatedCalendarHeight}
inputWidth={calculatedInputWidth}
inputUsesFloatingLabel={!!splittedPassOnProps.input.label}
{...splittedPassOnProps.popup}
>
<D2DateCalendar
onDateSelected={this.handleDateSelected}
value={this.props.value}
currentWidth={calculatedCalendarWidth}
height={calculatedCalendarHeight}
{...splittedPassOnProps.calendar}
/>
</D2DatePopup>
</div>
);
}
}

export const D2Date = withStyles(styles)(D2DatePlain);
Loading
Loading