Skip to content

Commit

Permalink
feat(useIntlDateTimeFormat): add useIntlDateTimeFormat composable (#329)
Browse files Browse the repository at this point in the history
* feat(useIntlDateTimeFormat): add useIntlDateTimeFormat

* improve dateTimeformat types and refactor types

* add more types

* docs: add docs

* test: add string test

* docs: fix datetimeformat example code
  • Loading branch information
pikax committed Jul 18, 2020
1 parent c2d3549 commit 8ab953f
Show file tree
Hide file tree
Showing 10 changed files with 809 additions and 193 deletions.
78 changes: 78 additions & 0 deletions docs/.vuepress/components/IntlDateTimeFormatExample.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<template>
<div>
<ul>
<li>
<label for="value">value</label>
<input name="value" v-model="value" type="date" />
</li>

<li>
<label for="locale">Locale</label>
<select name="locale" v-model="selectedLocale">
<option v-for="l in locales" :value="l">{{ l }}</option>
</select>
</li>

<li>
<label for="style">options.year</label>
<select name="style" v-model="options.year">
<option value="numeric">numeric</option>
<option value="2-digit">2-digit</option>
</select>
</li>
</ul>

<p>
value:
<b>{{ formatString(value) }}</b>
</p>

<p>
formatted:
<b>{{ formattedValue }}</b>
</p>
</div>
</template>

<script>
import { defineComponent, ref } from "@vue/composition-api";
import { useIntlDateTimeFormat, useLanguage } from "vue-composable";
export default defineComponent({
name: "intl-date-time-format-example",
setup() {
const { languages, language } = useLanguage();
const locales = languages.value.concat(...["pt-PT", "pt-BR"]);
const selectedLocale = ref(language.value || "en");
const options = ref({
year: "numeric",
month: "2-digit",
day: "2-digit"
});
const { formatString, format } = useIntlDateTimeFormat(
selectedLocale,
options
);
const value = ref("2020-01-01");
const formattedValue = format(value);
return {
locales,
selectedLocale,
formatString,
options,
value,
formattedValue
};
}
});
</script>
1 change: 1 addition & 0 deletions docs/.vuepress/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ module.exports = {
sidebarDepth: 1,
collapsable: false,
children: [
["composable/Intl/dateTimeFormat", "dateTimeFormat"],
["composable/Intl/numberFormat", "numberFormat"],
["composable/Intl/currencyFormat", "currencyFormat"]
]
Expand Down
117 changes: 117 additions & 0 deletions docs/api/vue-composable.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,31 @@ export interface CurrencyFormatReturn {
) => string;
}

// @public (undocumented)
export type DateTimeFormatLocales =
| RefTyped<string>
| RefTyped<string[]>
| undefined;

// @public (undocumented)
export interface DateTimeFormatReturn {
// (undocumented)
format: DateTimeFormatterFormat<Ref<Readonly<string>>>;
// (undocumented)
formatString: DateTimeFormatterFormat<string>;
// (undocumented)
formatter: Ref<Readonly<Intl.DateTimeFormat>>;
}

// @public (undocumented)
export type DateTimeFormatterFormat<T> = (
value: Readonly<RefTyped<Readonly<number | Date | string>>>,
overrideOpts?: RefTyped<
Intl.DateTimeFormatOptions | IntlDateTimeFormatOptions
>,
overrideLocale?: RefTyped<DateTimeFormatLocales>
) => T;

// @public (undocumented)
export function debounce<F extends Procedure>(
func: F,
Expand Down Expand Up @@ -448,6 +473,68 @@ export interface IntersectionObserverResult {
unobserve: (el: RefTyped<Element>) => void;
}

// @public (undocumented)
export type IntlDateTimeCalendarType =
| "buddhist"
| "chinese"
| "coptic"
| "ethiopia"
| "ethiopic"
| "gregory"
| "hebrew"
| "indian"
| "islamic"
| "iso8601"
| "japanese"
| "persian"
| "roc";

// @public (undocumented)
export type IntlDateTimeFormatOptions = Intl.DateTimeFormatOptions & {
dateStyle: "full" | "long" | "medium" | "short";
timeStyle: "full" | "long" | "medium" | "short";
calendar: IntlDateTimeCalendarType;
dayPeriod: "narrow" | "short" | "long";
numberingSystem: IntlDateTimeNumberingSystem;
weekday: "narrow" | "short" | "long";
era: "narrow" | "short" | "long";
year: "numeric" | "2-digit";
month: "numeric" | "2-digit" | "narrow" | "short" | "long";
day: "numeric" | "2-digit";
hour: "numeric" | "2-digit";
minute: "numeric" | "2-digit";
second: "numeric" | "2-digit";
timeZoneName: "short" | "long";
hour12: true | false;
hourCycle: "h11" | "h12" | "h23" | "h24";
formatMatcher: "basic" | "best fit";
};

// @public (undocumented)
export type IntlDateTimeNumberingSystem =
| "arab"
| "arabext"
| "bali"
| "beng"
| "deva"
| "fullwide"
| "gujr"
| "guru"
| "hanidec"
| "khmr"
| "knda"
| "laoo"
| "latn"
| "limb"
| "mlym"
| "mong"
| "mymr"
| "orya"
| "tamldec"
| "telu"
| "thai"
| "tibt";

// @public (undocumented)
export type IntlNumberFormatLocales =
| RefTyped<string>
Expand Down Expand Up @@ -1279,6 +1366,36 @@ export function useIntersectionObserver(
options: RefTyped<IntersectionObserverOptions>
): IntersectionObserverResult;

// @public (undocumented)
export function useIntlDateTimeFormat(): DateTimeFormatReturn;

// @public (undocumented)
export function useIntlDateTimeFormat(
locales: DateTimeFormatLocales
): DateTimeFormatReturn;

// @public (undocumented)
export function useIntlDateTimeFormat(
options: RefTyped<IntlDateTimeFormatOptions>
): DateTimeFormatReturn;

// @public (undocumented)
export function useIntlDateTimeFormat(
options: RefTyped<Intl.DateTimeFormatOptions>
): DateTimeFormatReturn;

// @public (undocumented)
export function useIntlDateTimeFormat(
locales: DateTimeFormatLocales,
options?: RefTyped<IntlDateTimeFormatOptions | undefined>
): DateTimeFormatReturn;

// @public (undocumented)
export function useIntlDateTimeFormat(
locales: DateTimeFormatLocales,
options: RefTyped<Intl.DateTimeFormatOptions | undefined>
): DateTimeFormatReturn;

// @public (undocumented)
export function useIntlNumberFormat(): NumberFormatReturn;

Expand Down
133 changes: 133 additions & 0 deletions docs/composable/Intl/dateTimeFormat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
# Intl.DateTimeFormat

> The [Intl.DateTimeFormat](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat).
## Parameters

```js
import { useIntlDateTimeFormat } from "vue-composable";

useIntlDateTimeFormat(locales?,options?);

```
| Parameters | Type | Required | Default | Description |
| ---------- | --------------------------------- | -------- | ----------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| locales | `Ref<string | string[]>` | `false` | `undefined` | Default locale passed to [Intl.DateTimeFormat](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/DateTimeFormat) |
| options | `Ref<Intl.DateTimeFormatOptions>` | `false` | `undefined` | Default options passed to [Intl.DateTimeFormat](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/DateTimeFormat) |
## State
The `useIntlDateTimeFormat` function exposes the following reactive state:
```js
import { useIntlDateTimeFormat } from "vue-composable";

const { formatter } = useIntlDateTimeFormat();
```
| State | Type | Description |
| --------- | -------------------------- | ---------------------- |
| formatter | `Ref<Intl.DateTimeFormat>` | Current formatter used |
## Methods
The `useIntlDateTimeFormat` function exposes the following methods:
```js
import { useIntlDateTimeFormat } from "vue-composable";

const { format, formatString } = useIntlDateTimeFormat();
```
| Signature | Description |
| -------------- | -------------------------------------------------------------------------------------- |
| `format` | returns reactive `ref<string>`, if value is `ref<number>` it will update automatically |
| `formatString` | returns `formatted` string |
## Example
<intl-date-time-format-example/>
### Code
```vue
<template>
<div>
<ul>
<li>
<label for="value">value</label>
<input name="value" v-model="value" type="date" />
</li>

<li>
<label for="locale">Locale</label>
<select name="locale" v-model="selectedLocale">
<option v-for="l in locales" :value="l">{{ l }}</option>
</select>
</li>

<li>
<label for="style">options.year</label>
<select name="style" v-model="options.year">
<option value="numeric">numeric</option>
<option value="2-digit">2-digit</option>
</select>
</li>
</ul>

<p>
value:
<b>{{ formatString(value) }}</b>
</p>

<p>
formatted:
<b>{{ formattedValue }}</b>
</p>
</div>
</template>

<script>
import { defineComponent, ref } from "@vue/composition-api";
import { useIntlDateTimeFormat, useLanguage } from "vue-composable";

export default defineComponent({
name: "intl-date-time-format-example",

setup() {
const { languages, language } = useLanguage();
const locales = languages.value.concat(...["pt-PT", "pt-BR"]);

const selectedLocale = ref(language.value || "en");

const options = ref({
year: "numeric",
month: "2-digit",
day: "2-digit"
});

const { formatString, format } = useIntlDateTimeFormat(
selectedLocale,
options
);

const value = ref("2020-01-01");

const formattedValue = format(value);

return {
locales,

selectedLocale,
formatString,

options,

value,
formattedValue
};
}
});
</script>
```

0 comments on commit 8ab953f

Please sign in to comment.