Skip to content

Commit

Permalink
fix(datetime): can participate in native <form> (#16106)
Browse files Browse the repository at this point in the history
fixes #16075
  • Loading branch information
manucorporat committed Oct 26, 2018
1 parent c982856 commit aad7711
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 6 deletions.
4 changes: 2 additions & 2 deletions angular/src/directives/proxies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ export class Content {
}

export declare interface Datetime extends StencilComponents<'IonDatetime'> {}
@Component({ selector: 'ion-datetime', changeDetection: ChangeDetectionStrategy.OnPush, encapsulation: ViewEncapsulation.None, template: '<ng-content></ng-content>', inputs: ['mode', 'disabled', 'min', 'max', 'displayFormat', 'pickerFormat', 'cancelText', 'doneText', 'yearValues', 'monthValues', 'dayValues', 'hourValues', 'minuteValues', 'monthNames', 'monthShortNames', 'dayNames', 'dayShortNames', 'pickerOptions', 'placeholder', 'value'] })
@Component({ selector: 'ion-datetime', changeDetection: ChangeDetectionStrategy.OnPush, encapsulation: ViewEncapsulation.None, template: '<ng-content></ng-content>', inputs: ['mode', 'name', 'disabled', 'min', 'max', 'displayFormat', 'pickerFormat', 'cancelText', 'doneText', 'yearValues', 'monthValues', 'dayValues', 'hourValues', 'minuteValues', 'monthNames', 'monthShortNames', 'dayNames', 'dayShortNames', 'pickerOptions', 'placeholder', 'value'] })
export class Datetime {
ionCancel: EventEmitter<CustomEvent>;
ionChange: EventEmitter<CustomEvent>;
Expand All @@ -222,7 +222,7 @@ export class Datetime {
constructor(r: ElementRef) {
const el = r.nativeElement;
proxyMethods(this, el, ['open']);
proxyInputs(this, el, ['mode', 'disabled', 'min', 'max', 'displayFormat', 'pickerFormat', 'cancelText', 'doneText', 'yearValues', 'monthValues', 'dayValues', 'hourValues', 'minuteValues', 'monthNames', 'monthShortNames', 'dayNames', 'dayShortNames', 'pickerOptions', 'placeholder', 'value']);
proxyInputs(this, el, ['mode', 'name', 'disabled', 'min', 'max', 'displayFormat', 'pickerFormat', 'cancelText', 'doneText', 'yearValues', 'monthValues', 'dayValues', 'hourValues', 'minuteValues', 'monthNames', 'monthShortNames', 'dayNames', 'dayShortNames', 'pickerOptions', 'placeholder', 'value']);
proxyOutputs(this, el, ['ionCancel', 'ionChange', 'ionStyle']);
}
}
Expand Down
8 changes: 8 additions & 0 deletions core/src/components.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1275,6 +1275,10 @@ export namespace Components {
*/
'monthValues'?: number[] | number | string;
/**
* The name of the control, which is submitted with the form data.
*/
'name': string;
/**
* Opens the datetime overlay.
*/
'open': () => Promise<void>;
Expand Down Expand Up @@ -1361,6 +1365,10 @@ export namespace Components {
*/
'monthValues'?: number[] | number | string;
/**
* The name of the control, which is submitted with the form data.
*/
'name'?: string;
/**
* Emitted when the datetime selection was cancelled.
*/
'onIonCancel'?: (event: CustomEvent<void>) => void;
Expand Down
11 changes: 9 additions & 2 deletions core/src/components/datetime/datetime.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Component, ComponentInterface, Element, Event, EventEmitter, Method, Prop, State, Watch } from '@stencil/core';

import { DatetimeOptions, InputChangeEvent, Mode, PickerColumn, PickerColumnOption, PickerOptions, StyleEvent } from '../../interface';
import { clamp } from '../../utils/helpers';
import { clamp, renderHiddenInput } from '../../utils/helpers';
import { hostContext } from '../../utils/theme';

import { DatetimeData, LocaleData, convertDataToISO, convertFormatToKey, convertToArrayOfNumbers, convertToArrayOfStrings, dateDataSortValue, dateSortValue, dateValueRange, daysInMonth, getValueFromFormat, parseDate, parseTemplate, renderDatetime, renderTextFormat, updateDate } from './datetime-util';
Expand Down Expand Up @@ -35,6 +35,11 @@ export class Datetime implements ComponentInterface {
*/
@Prop() mode!: Mode;

/**
* The name of the control, which is submitted with the form data.
*/
@Prop() name: string = this.inputId;

/**
* If `true`, the user cannot interact with the datetime. Defaults to `false`.
*/
Expand Down Expand Up @@ -523,6 +528,7 @@ export class Datetime implements ComponentInterface {
if (datetimeText == null) {
datetimeText = this.placeholder != null ? this.placeholder : '';
}
renderHiddenInput(this.el, this.name, this.value, this.disabled);

return [
<div class="datetime-text">{datetimeText}</div>,
Expand All @@ -534,7 +540,8 @@ export class Datetime implements ComponentInterface {
onClick={this.open.bind(this)}
class="datetime-cover"
>
</button>
</button>,
<slot></slot>
];
}
}
Expand Down
1 change: 1 addition & 0 deletions core/src/components/datetime/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ dates in JavaScript.
| `monthNames` | `month-names` | Full names for each month name. This can be used to provide locale month names. Defaults to English. | `string \| string[] \| undefined` |
| `monthShortNames` | `month-short-names` | Short abbreviated names for each month name. This can be used to provide locale month names. Defaults to English. | `string \| string[] \| undefined` |
| `monthValues` | -- | Values used to create the list of selectable months. By default the month values range from `1` to `12`. However, to control exactly which months to display, the `monthValues` input can take a number, an array of numbers, or a string of comma separated numbers. For example, if only summer months should be shown, then this input value would be `monthValues="6,7,8"`. Note that month numbers do *not* have a zero-based index, meaning January's value is `1`, and December's is `12`. | `number \| number[] \| string \| undefined` |
| `name` | `name` | The name of the control, which is submitted with the form data. | `string` |
| `pickerFormat` | `picker-format` | The format of the date and time picker columns the user selects. A datetime input can have one or many datetime parts, each getting their own column which allow individual selection of that particular datetime part. For example, year and month columns are two individually selectable columns which help choose an exact date from the datetime picker. Each column follows the string parse format. Defaults to use `displayFormat`. | `string \| undefined` |
| `pickerOptions` | -- | Any additional options that the picker interface can accept. See the [Picker API docs](../../picker/Picker) for the picker options. | `Partial<PickerOptions> \| undefined` |
| `placeholder` | `placeholder` | The text to display when there's no date selected yet. Using lowercase to match the input attribute | `null \| string \| undefined` |
Expand Down
4 changes: 2 additions & 2 deletions core/src/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export function hasShadowDom(el: HTMLElement) {
return !!el.shadowRoot && !!(el as any).attachShadow;
}

export function renderHiddenInput(container: HTMLElement, name: string, value: string, disabled: boolean) {
export function renderHiddenInput(container: HTMLElement, name: string, value: string | undefined | null, disabled: boolean) {
if (hasShadowDom(container)) {
let input = container.querySelector('input.aux-input') as HTMLInputElement | null;
if (!input) {
Expand All @@ -32,7 +32,7 @@ export function renderHiddenInput(container: HTMLElement, name: string, value: s
}
input.disabled = disabled;
input.name = name;
input.value = value;
input.value = value || '';
}
}

Expand Down

0 comments on commit aad7711

Please sign in to comment.