fix(datetime): can participate in native <form> (#16106)

fixes #16075
This commit is contained in:
Manu MA
2018-10-26 19:01:39 +02:00
committed by GitHub
parent c982856dba
commit aad7711af2
5 changed files with 22 additions and 6 deletions

View File

@ -213,7 +213,7 @@ export class Content {
} }
export declare interface Datetime extends StencilComponents<'IonDatetime'> {} 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 { export class Datetime {
ionCancel: EventEmitter<CustomEvent>; ionCancel: EventEmitter<CustomEvent>;
ionChange: EventEmitter<CustomEvent>; ionChange: EventEmitter<CustomEvent>;
@ -222,7 +222,7 @@ export class Datetime {
constructor(r: ElementRef) { constructor(r: ElementRef) {
const el = r.nativeElement; const el = r.nativeElement;
proxyMethods(this, el, ['open']); 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']); proxyOutputs(this, el, ['ionCancel', 'ionChange', 'ionStyle']);
} }
} }

View File

@ -1275,6 +1275,10 @@ export namespace Components {
*/ */
'monthValues'?: number[] | number | string; 'monthValues'?: number[] | number | string;
/** /**
* The name of the control, which is submitted with the form data.
*/
'name': string;
/**
* Opens the datetime overlay. * Opens the datetime overlay.
*/ */
'open': () => Promise<void>; 'open': () => Promise<void>;
@ -1361,6 +1365,10 @@ export namespace Components {
*/ */
'monthValues'?: number[] | number | string; '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. * Emitted when the datetime selection was cancelled.
*/ */
'onIonCancel'?: (event: CustomEvent<void>) => void; 'onIonCancel'?: (event: CustomEvent<void>) => void;

View File

@ -1,7 +1,7 @@
import { Component, ComponentInterface, Element, Event, EventEmitter, Method, Prop, State, Watch } from '@stencil/core'; import { Component, ComponentInterface, Element, Event, EventEmitter, Method, Prop, State, Watch } from '@stencil/core';
import { DatetimeOptions, InputChangeEvent, Mode, PickerColumn, PickerColumnOption, PickerOptions, StyleEvent } from '../../interface'; 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 { hostContext } from '../../utils/theme';
import { DatetimeData, LocaleData, convertDataToISO, convertFormatToKey, convertToArrayOfNumbers, convertToArrayOfStrings, dateDataSortValue, dateSortValue, dateValueRange, daysInMonth, getValueFromFormat, parseDate, parseTemplate, renderDatetime, renderTextFormat, updateDate } from './datetime-util'; import { DatetimeData, LocaleData, convertDataToISO, convertFormatToKey, convertToArrayOfNumbers, convertToArrayOfStrings, dateDataSortValue, dateSortValue, dateValueRange, daysInMonth, getValueFromFormat, parseDate, parseTemplate, renderDatetime, renderTextFormat, updateDate } from './datetime-util';
@ -35,6 +35,11 @@ export class Datetime implements ComponentInterface {
*/ */
@Prop() mode!: Mode; @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`. * If `true`, the user cannot interact with the datetime. Defaults to `false`.
*/ */
@ -523,6 +528,7 @@ export class Datetime implements ComponentInterface {
if (datetimeText == null) { if (datetimeText == null) {
datetimeText = this.placeholder != null ? this.placeholder : ''; datetimeText = this.placeholder != null ? this.placeholder : '';
} }
renderHiddenInput(this.el, this.name, this.value, this.disabled);
return [ return [
<div class="datetime-text">{datetimeText}</div>, <div class="datetime-text">{datetimeText}</div>,
@ -534,7 +540,8 @@ export class Datetime implements ComponentInterface {
onClick={this.open.bind(this)} onClick={this.open.bind(this)}
class="datetime-cover" class="datetime-cover"
> >
</button> </button>,
<slot></slot>
]; ];
} }
} }

View File

@ -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` | | `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` | | `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` | | `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` | | `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` | | `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` | | `placeholder` | `placeholder` | The text to display when there's no date selected yet. Using lowercase to match the input attribute | `null \| string \| undefined` |

View File

@ -21,7 +21,7 @@ export function hasShadowDom(el: HTMLElement) {
return !!el.shadowRoot && !!(el as any).attachShadow; 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)) { if (hasShadowDom(container)) {
let input = container.querySelector('input.aux-input') as HTMLInputElement | null; let input = container.querySelector('input.aux-input') as HTMLInputElement | null;
if (!input) { if (!input) {
@ -32,7 +32,7 @@ export function renderHiddenInput(container: HTMLElement, name: string, value: s
} }
input.disabled = disabled; input.disabled = disabled;
input.name = name; input.name = name;
input.value = value; input.value = value || '';
} }
} }