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

Improve Date/DateTime type parameters #2695

Merged
merged 2 commits into from
Jul 25, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
41 changes: 41 additions & 0 deletions client/app/components/DateInput.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import moment from 'moment';
import React from 'react';
import PropTypes from 'prop-types';
import { react2angular } from 'react2angular';
import { DatePicker } from 'antd';
import 'antd/dist/antd.less';

function DateInput({
value,
placeholder,
onSelect,
clientConfig,
}) {
const format = clientConfig.dateFormat || 'YYYY-MM-DD';
const defaultValue = moment(value, format);
return (
<DatePicker
{...(defaultValue.isValid() ? { defaultValue } : {})}
format={format}
placeholder={placeholder}
onChange={onSelect}
/>
);
}

DateInput.propTypes = {
value: PropTypes.instanceOf(Date),
placeholder: PropTypes.string,
onSelect: PropTypes.func,
};

DateInput.defaultProps = {
value: Date.now(),
placeholder: 'Select date',
Copy link
Member

Choose a reason for hiding this comment

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

Select Date

Copy link
Member

Choose a reason for hiding this comment

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

Also seems that we don't really use it customer placeholders. Maybe just pass a static value?

onSelect: () => {},
};

export default function init(ngModule) {
ngModule.component('dateInput', react2angular(DateInput, null, ['clientConfig']));
}

46 changes: 46 additions & 0 deletions client/app/components/DateTimeInput.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import moment from 'moment';
import React from 'react';
import PropTypes from 'prop-types';
import { react2angular } from 'react2angular';
import { DatePicker } from 'antd';
import 'antd/dist/antd.less';

function DateTimeInput({
value,
withSeconds,
placeholder,
onSelect,
clientConfig,
}) {
const format = (clientConfig.dateFormat || 'YYYY-MM-DD') +
(withSeconds ? ' HH:mm:ss' : ' HH:mm');
const defaultValue = moment(value, format);
return (
<DatePicker
showTime
{...(defaultValue.isValid() ? { defaultValue } : {})}
format={format}
placeholder={placeholder}
onChange={onSelect}
/>
);
}

DateTimeInput.propTypes = {
value: PropTypes.instanceOf(Date),
withSeconds: PropTypes.bool,
placeholder: PropTypes.string,
onSelect: PropTypes.func,
};

DateTimeInput.defaultProps = {
value: Date.now(),
withSeconds: false,
placeholder: 'Select date and time',
onSelect: () => {},
};

export default function init(ngModule) {
ngModule.component('dateTimeInput', react2angular(DateTimeInput, null, ['clientConfig']));
}

9 changes: 6 additions & 3 deletions client/app/components/parameters.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@
<i class="zmdi zmdi-settings"></i>
</button>
<span ng-switch="param.type">
<input ng-switch-when="datetime-with-seconds" type="datetime-local" step="1" class="form-control" ng-model="param.ngModel">
<input ng-switch-when="datetime-local" type="datetime-local" class="form-control" ng-model="param.ngModel">
<input ng-switch-when="date" type="date" class="form-control" ng-model="param.ngModel">
<date-time-input ng-switch-when="datetime-with-seconds" value="param.ngModel" with-seconds="true"
on-select="param.updateValue"></date-time-input>
<date-time-input ng-switch-when="datetime-local" value="param.ngModel"
on-select="param.updateValue"></date-time-input>
<date-input ng-switch-when="date" value="param.ngModel"
on-select="param.updateValue"></date-input>
<span ng-switch-when="enum">
<select ng-model="param.value" class="form-control">
<option ng-repeat="option in extractEnumOptions(param.enumOptions)" value="{{option}}">{{option}}</option>
Expand Down
6 changes: 6 additions & 0 deletions client/app/services/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ class Parameter {
this.global = parameter.global;
this.enumOptions = parameter.enumOptions;
this.queryId = parameter.queryId;

// method to update parameter value from date/time picker component
// (react does not support two-way binding with `ngModel`)
this.updateValue = (function updateValue(value) {
this.ngModel = value;
}).bind(this);
Copy link
Member

Choose a reason for hiding this comment

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

Why not use the following?

this.updateValue = value => this.ngModel = value;

(=> instead of .bind(this))

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

oh, missed it, you're right

}

get ngModel() {
Expand Down
Loading