import { Story, Meta, html } from '@open-wc/demoing-storybook';
import { loadDefaultFeedbackMessages, MinDate, MinMaxDate, MaxDate } from '@lion/validate';
import { formatDate } from '@lion/localize';

import '../lion-input-date.js';

<Meta title="Forms/Input Date" parameters={{ component: 'lion-input-date' }} />

# Input Date

`lion-input-date` component is based on the generic text input field. Its purpose is to provide a way for users to fill in a date.

<Story name="Default">
  {() => {
    loadDefaultFeedbackMessages();
    return html`
      <lion-input-date label="Date"></lion-input-date>
    `;
  }}
</Story>

```html
<lion-input-date label="Date"></lion-input-date>
```

## Features

- Based on [lion-input](?path=/docs/forms-input--default-story)
- Makes use of [formatDate](?path=/docs/localize-dates--formatting)) for formatting and parsing.
- Option to override locale to change the formatting and parsing
- Default label in different languages
- Can make use of date specific [validators](?path=/docs/forms-validation-overview--page) with corresponding error messages in different languages
  - IsDate (default)
  - MinDate
  - MaxDate
  - MinMaxDate

## How to use

### Installation

```sh
npm i --save @lion/input-date
```

```js
import '@lion/input-date/lion-input-date.js';
```

## Examples

### Validation

<Story name="Validation">
  {html`
    <style>
      lion-input-date {
        margin-bottom: 30px;
      }
    </style>
    <lion-input-date label="IsDate" .modelValue=${new Date('foo')}> </lion-input-date>
    <lion-input-date
      label="MinDate"
      help-text="Enter a date greater than or equal to today."
      .modelValue=${new Date('2018/05/30')}
      .validators=${[new MinDate(new Date())]}
    >
    </lion-input-date>
    <lion-input-date
      label="MaxDate"
      help-text="Enter a date smaller than or equal to today."
      .modelValue=${new Date('2100/05/30')}
      .validators=${[new MaxDate(new Date())]}
    >
    </lion-input-date>
    <lion-input-date
      label="MinMaxDate"
      .modelValue=${new Date('2018/05/30')}
      .validators=${[
        new MinMaxDate({ min: new Date('2018/05/24'), max: new Date('2018/06/24') }),
      ]}
    >
      <div slot="help-text">
        Enter a date between ${formatDate(new Date('2018/05/24'))} and
        ${formatDate(new Date('2018/06/24'))}.
      </div>
    </lion-input-date>
  `}
</Story>

```js
import { loadDefaultFeedbackMessages, MinDate, MinMaxDate, MaxDate } from '@lion/validate';
import { formatDate } from '@lion/localize';
```

IsDate
```html
<lion-input-date label="IsDate" .modelValue=${new Date('foo')}> </lion-input-date>
```

MinDate
```html
<lion-input-date
  label="MinDate"
  help-text="Enter a date greater than or equal to today."
  .modelValue=${new Date('2018/05/30')}
  .validators=${[new MinDate(new Date())]}
></lion-input-date>
```

MaxDate
```html
<lion-input-date
  label="MaxDate"
  help-text="Enter a date smaller than or equal to today."
  .modelValue=${new Date('2100/05/30')}
  .validators=${[new MaxDate(new Date())]}
></lion-input-date>
```

MinMaxDate
```html
<lion-input-date
  label="MinMaxDate"
  .modelValue=${new Date('2018/05/30')}
  .validators=${[
    new MinMaxDate({ min: new Date('2018/05/24'), max: new Date('2018/06/24') }),
  ]}
>
  <div slot="help-text">
    Enter a date between ${formatDate(new Date('2018/05/24'))} and
    ${formatDate(new Date('2018/06/24'))}.
  </div>
</lion-input-date>
```

### Faulty prefilled

<Story name="Faulty prefilled">
  {html`
    <lion-input-date
      label="Date"
      help-text="Faulty prefilled input will be cleared"
      .modelValue=${'foo'}
    ></lion-input-date>
  `}
</Story>

```html
<lion-input-date
  label="Date"
  help-text="Faulty prefilled input will be cleared"
  .modelValue=${'foo'}
></lion-input-date>
```
