Add definition for React Intl 2.0.0-pr3.

Kept backwards compatibility with 1.2.0 by having two files.

Test files is fairly complete!
This commit is contained in:
cdroulers
2015-11-03 08:16:57 -05:00
parent 62eedc3121
commit 7884038616
6 changed files with 620 additions and 212 deletions
+99
View File
@@ -0,0 +1,99 @@
// Type definitions for react-intl 1.2.0
// Project: http://formatjs.io/react/
// Definitions by: Bruno Grieder <https://github.com/bgrieder>
// Definitions: https://github.com/borisyankov/DefinitelyTyped
///<reference path='../react/react.d.ts' />
declare module "react-intl" {
import * as React from 'react'
module ReactIntl {
interface IIntlMixin extends React.Mixin<any,any> {
getIntlMessage(key: string): string
}
var IntlMixin: IIntlMixin
module IntlComponent {
interface Props {
locales?: string[]
messages?: {[key: string]: any}
formats?: string[]
}
}
interface IntlComponent {
getIntlMessage(key: string): string;
}
module FormattedDate {
export interface Props extends IntlComponent.Props {
value: Date
day?: string
month?: string
year?: string
}
}
class FormattedDate extends React.Component<FormattedDate.Props,any> {}
module FormattedTime {
export interface Props extends IntlComponent.Props {
value: Date
day?: string
month?: string
year?: string
format?: string
}
}
class FormattedTime extends React.Component<FormattedTime.Props,any> {}
module FormattedRelative {
export interface Props extends IntlComponent.Props {
value: number
units?: string //"second", "minute", "hour", "day", "month" or "year"
style?: string //"best fit" (default) or "numeric"
format?: string
}
}
class FormattedRelative extends React.Component<FormattedRelative.Props,any> {}
module FormattedMessage {
export interface Props extends IntlComponent.Props {
message: string;
[prop: string]: any
}
}
class FormattedMessage extends React.Component<FormattedMessage.Props,any> {}
module FormattedHTMLMessage {
export interface Props extends IntlComponent.Props {
message: string;
[prop: string]: any
}
}
class FormattedHTMLMessage extends React.Component<FormattedHTMLMessage.Props,any> {}
module FormattedNumber {
export interface Props extends IntlComponent.Props {
value: number
style?: string
currency?: string
format?: string
}
}
class FormattedNumber extends React.Component<FormattedNumber.Props,any> {}
}
export = ReactIntl
}
+138
View File
@@ -0,0 +1,138 @@
/**
* Created by Bruno Grieder
*/
///<reference path='../react/react.d.ts' />
///<reference path='../react-mixin/react-mixin.d.ts' />
///<reference path='../react-intl/react-intl-1.2.0.d.ts' />
import * as React from 'react'
import * as reactMixin from 'react-mixin'
import {IntlMixin, IntlComponent, FormattedNumber, FormattedMessage, FormattedDate} from 'react-intl'
///////////////////////////////////////////////////////////////////////////
//
// This class does not use the mixin and react-mixin is not required
// The MESSAGES are maintained in the file
// To use it call <I18nDirect locales={['en-US']}/>
//
////////////////////////////////////////////////////////////////////////////
const MESSAGES: {[key: string] : { [lang: string]: string }} = {
Sorry: {
'en-US': 'Sorry {name}',
'fr-FR': 'Désolé {name}'
}
}
module I18nDirect {
export interface Props extends IntlComponent.Props {}
}
class I18nDirect extends React.Component<I18nDirect.Props, any> {
private _currentLocale: string
private _messages: {[key: string]: string}
constructor( props: I18nDirect.Props ) {
super( props )
}
render() {
return (
<ul>
<li>FormattedNumber:&nbsp;
<FormattedNumber value={99.95} style="currency" currency="USD"/>
</li>
<li>FormattedMessage:&nbsp;
<FormattedMessage message={this._messages['Sorry']} name='Dave'/>
</li>
<li>FormattedDate:&nbsp;
<FormattedDate value={new Date()}/>
</li>
</ul>
)
}
componentWillReceiveProps( nextProps: I18nDirect.Props ) {
this.compileMessages(nextProps)
}
componentWillMount() {
this.compileMessages(this.props)
}
private compileMessages = (props: I18nDirect.Props): void => {
let locale: string = ( props.locales && props.locales[ 0 ] ) || 'en-US'
if (this._currentLocale !== locale) {
this._messages = Object.keys( MESSAGES ).reduce(
( dic , key ) => {
dic[ key ] = MESSAGES[ key ][ locale ]
return dic
},
{} as { [key: string]: string; }
)
}
}
}
///////////////////////////////////////////////////////////////////////////
//
// This class uses the mixin and react-mixin is
// The MESSAGES are passed from messages property of the props
// To use it call <I18nMixin {...props}/>
//
////////////////////////////////////////////////////////////////////////////
module I18nMixin {
export interface Props extends IntlComponent.Props {}
}
@reactMixin.decorate( IntlMixin )
class I18nMixin extends React.Component<I18nMixin.Props, any> implements IntlComponent {
private _currentLocale: string
constructor( props: I18nMixin.Props ) {
super( props )
}
//Expose the method provided by the Mixin
getIntlMessage: (key: string) => string = this['getIntlMessage']
render() {
return (
<ul>
<li>FormattedNumber:
<FormattedNumber value={99.95} style="currency" currency="USD"/>
</li>
<li>FormattedMessage:
<FormattedMessage message={this.getIntlMessage('Sorry')} name='Dave'/> {/* this uses the mixin */}
</li>
</ul>
)
}
}
export { I18nDirect, I18nMixin }
@@ -0,0 +1 @@
--target es5 --noImplicitAny --experimentalDecorators --jsx react --module commonjs
+152 -122
View File
@@ -1,138 +1,168 @@
/**
* Created by Bruno Grieder
* Created by Bruno Grieder and Christian Droulers
*/
///<reference path='../react/react.d.ts' />
///<reference path='../react-mixin/react-mixin.d.ts' />
///<reference path='../react-intl/react-intl.d.ts' />
import * as React from 'react'
import * as reactMixin from 'react-mixin'
import {IntlMixin, IntlComponent, FormattedNumber, FormattedMessage, FormattedDate} from 'react-intl'
///<reference path="../react/react.d.ts" />
///<reference path="../react-mixin/react-mixin.d.ts" />
///<reference path="./react-intl.d.ts" />
///////////////////////////////////////////////////////////////////////////
//
// This class does not use the mixin and react-mixin is not required
// The MESSAGES are maintained in the file
// To use it call <I18nDirect locales={['en-US']}/>
//
////////////////////////////////////////////////////////////////////////////
import * as React from "react"
import * as reactMixin from "react-mixin"
import {
IntlProvider,
InjectedIntlProps,
addLocaleData,
hasLocaleData,
injectIntl,
intlShape,
defineMessages,
FormattedRelative,
FormattedMessage,
FormattedHTMLMessage,
FormattedNumber,
FormattedPlural,
FormattedDate,
FormattedTime
} from "react-intl"
import reactIntlEn = require("react-intl/lib/locale-data/en");
const MESSAGES: {[key: string] : { [lang: string]: string }} = {
addLocaleData(reactIntlEn);
console.log(hasLocaleData("en"));
Sorry: {
'en-US': 'Sorry {name}',
'fr-FR': 'Désolé {name}'
interface SomeComponentProps extends InjectedIntlProps {
}
class SomeComponent extends React.Component<SomeComponentProps, {}> {
static propTypes: React.ValidationMap<any> = {
intl: intlShape.isRequired
};
public render(): React.ReactElement<{}> {
const formattedDate = this.props.formatDate(new Date(), { format: "short" });
const formattedTime = this.props.formatTime(new Date(), { format: "short" });
const formattedRelative = this.props.formatRelative(new Date().getTime(), { format: "short" });
const formattedNumber = this.props.formatNumber(123, { format: "short" });
const formattedPlural = this.props.formatPlural(1, { one: "hai!" });
const formattedMessage = this.props.formatMessage({ id: "hello", defaultMessage: "Hello {name}!" }, { name: "Roger" });
const formattedHTMLMessage = this.props.formatHTMLMessage({ id: "hello", defaultMessage: "Hello <strong>{name}</strong>!" }, { name: "Roger" });
return <div>
<FormattedRelative
value={new Date().getTime() }
units="hour"
style="numeric"
format="yyyy-MM-dd"
updateInterval={123}
initialNow={new Date() } />
<FormattedMessage
id="test"
description="Test"
defaultMessage="Hi, {name}!"
values={{ name: "bob" }}
tagName="div" />
<FormattedHTMLMessage
id="test"
description="Test"
defaultMessage="Hi, {name}!"
values={{ name: "bob" }}
tagName="div" />
<FormattedHTMLMessage
id="test"
description="Test"
defaultMessage="Hi, {name}!"
values={{ name: "bob" }}
tagName="div" />
<FormattedNumber
value={123456.78}
format="N"
localeMatcher="lookup"
style="currency"
currency="USD"
currencyDisplay="name"
useGrouping={false}
minimumIntegerDigits={1}
minimumFractionDigits={1}
minimumSignificantDigits={2}
maximumFractionDigits={3}
maximumSignificantDigits={3} />
<FormattedPlural
style="cardinal"
value={3}
other="hai?"
zero="no hai"
one="hai"
two="hai2"
few="haifew"
many="haiku" />
<FormattedDate
value={new Date() }
format="short"
localeMatcher="best fit"
formatMatcher="basic"
timeZone="EDT"
hour12={false}
weekday="short"
era="short"
year="2-digit"
month="2-digit"
day="2-digit"
hour="2-digit"
minute="2-digit"
second="2-digit"
timeZoneName="short" />
<FormattedTime
value={new Date() }
format="short"
localeMatcher="best fit"
formatMatcher="basic"
timeZone="EDT"
hour12={false}
weekday="short"
era="short"
year="2-digit"
month="2-digit"
day="2-digit"
hour="2-digit"
minute="2-digit"
second="2-digit"
timeZoneName="short" />
<FormattedNumber value={123}>
{(formattedNum: string) => (
<span className="number">{formattedNum}</span>
) }
</FormattedNumber>
</div>
}
}
class TestApp extends React.Component<{}, {}> {
public render(): React.ReactElement<{}> {
const definedMessages = defineMessages({
"sup": {
id: "sup",
defaultMessage: "Hai mom"
}
});
module I18nDirect {
export interface Props extends IntlComponent.Props {}
}
class I18nDirect extends React.Component<I18nDirect.Props, any> {
private _currentLocale: string
private _messages: {[key: string]: string}
constructor( props: I18nDirect.Props ) {
super( props )
}
render() {
return (
<ul>
<li>FormattedNumber:&nbsp;
<FormattedNumber value={99.95} style="currency" currency="USD"/>
</li>
<li>FormattedMessage:&nbsp;
<FormattedMessage message={this._messages['Sorry']} name='Dave'/>
</li>
<li>FormattedDate:&nbsp;
<FormattedDate value={new Date()}/>
</li>
</ul>
)
}
componentWillReceiveProps( nextProps: I18nDirect.Props ) {
this.compileMessages(nextProps)
}
componentWillMount() {
this.compileMessages(this.props)
}
private compileMessages = (props: I18nDirect.Props): void => {
let locale: string = ( props.locales && props.locales[ 0 ] ) || 'en-US'
if (this._currentLocale !== locale) {
this._messages = Object.keys( MESSAGES ).reduce(
( dic , key ) => {
dic[ key ] = MESSAGES[ key ][ locale ]
return dic
},
{} as { [key: string]: string; }
)
const messages = {
"hello": "Hello, {name}!"
}
}
}
///////////////////////////////////////////////////////////////////////////
//
// This class uses the mixin and react-mixin is
// The MESSAGES are passed from messages property of the props
// To use it call <I18nMixin {...props}/>
//
////////////////////////////////////////////////////////////////////////////
module I18nMixin {
export interface Props extends IntlComponent.Props {}
}
@reactMixin.decorate( IntlMixin )
class I18nMixin extends React.Component<I18nMixin.Props, any> implements IntlComponent {
private _currentLocale: string
constructor( props: I18nMixin.Props ) {
super( props )
}
//Expose the method provided by the Mixin
getIntlMessage: (key: string) => string = this['getIntlMessage']
render() {
return (
<ul>
<li>FormattedNumber:
<FormattedNumber value={99.95} style="currency" currency="USD"/>
</li>
<li>FormattedMessage:
<FormattedMessage message={this.getIntlMessage('Sorry')} name='Dave'/> {/* this uses the mixin */}
</li>
</ul>
)
return (<IntlProvider locale="en" formats={{}} messages={messages} defaultLocale="en" defaultFormats={messages}>
<SomeComponent />
</IntlProvider>)
}
}
export { I18nDirect, I18nMixin }
export default {
TestApp,
SomeComponent: injectIntl(SomeComponent)
}
+1 -1
View File
@@ -1 +1 @@
--target es5 --noImplicitAny --experimentalDecorators --jsx react
--target es5 --noImplicitAny --experimentalDecorators --jsx react --module commonjs
+229 -89
View File
@@ -1,99 +1,239 @@
// Type definitions for react-intl 1.2.0
// Type definitions for react-intl 2.0.0-beta1
// Project: http://formatjs.io/react/
// Definitions by: Bruno Grieder <https://github.com/bgrieder>
// Definitions by: Bruno Grieder <https://github.com/bgrieder>, Christian Droulers <https://github.com/cdroulers>
// Definitions: https://github.com/borisyankov/DefinitelyTyped
///<reference path='../react/react.d.ts' />
declare module "react-intl" {
import * as React from 'react'
module ReactIntl {
interface IIntlMixin extends React.Mixin<any,any> {
getIntlMessage(key: string): string
}
var IntlMixin: IIntlMixin
module IntlComponent {
interface Props {
locales?: string[]
messages?: {[key: string]: any}
formats?: string[]
}
}
interface IntlComponent {
getIntlMessage(key: string): string;
}
module FormattedDate {
export interface Props extends IntlComponent.Props {
value: Date
day?: string
month?: string
year?: string
}
}
class FormattedDate extends React.Component<FormattedDate.Props,any> {}
module FormattedTime {
export interface Props extends IntlComponent.Props {
value: Date
day?: string
month?: string
year?: string
format?: string
}
}
class FormattedTime extends React.Component<FormattedTime.Props,any> {}
module FormattedRelative {
export interface Props extends IntlComponent.Props {
value: number
units?: string //"second", "minute", "hour", "day", "month" or "year"
style?: string //"best fit" (default) or "numeric"
format?: string
}
}
class FormattedRelative extends React.Component<FormattedRelative.Props,any> {}
module FormattedMessage {
export interface Props extends IntlComponent.Props {
message: string;
[prop: string]: any
}
}
class FormattedMessage extends React.Component<FormattedMessage.Props,any> {}
module FormattedHTMLMessage {
export interface Props extends IntlComponent.Props {
message: string;
[prop: string]: any
}
}
class FormattedHTMLMessage extends React.Component<FormattedHTMLMessage.Props,any> {}
module FormattedNumber {
export interface Props extends IntlComponent.Props {
value: number
style?: string
currency?: string
format?: string
}
}
class FormattedNumber extends React.Component<FormattedNumber.Props,any> {}
declare module ReactIntl {
import React = __React;
interface Locale {
locale: string;
fields?: { [key: string]: string },
pluralRuleFunction?: (n: number, ord: boolean) => string;
}
export = ReactIntl
function injectIntl<T>(clazz: T): T;
function addLocaleData(data: Locale[] | Locale): void;
function hasLocaleData(localeName: string): boolean;
interface Messages {
[key: string]: FormattedMessage.MessageDescriptor
}
function defineMessages<T extends Messages>(messages: Messages): T;
interface IntlShape extends React.Requireable<any> {
}
var intlShape: IntlShape;
interface FormatConfig {
locale?: string;
formats?: any;
}
interface InjectedIntlProps {
formatDate?: (date: Date, options?: FormattedDate.PropsBase) => string;
formatTime?: (date: Date, options?: FormattedTime.PropsBase) => string;
formatRelative?: (value: number, options?: FormattedRelative.PropsBase) => string;
formatNumber?: (value: number, options?: FormattedNumber.PropsBase) => string;
formatPlural?: (value: number, options?: FormattedPlural.PropsBase) => string;
formatMessage?: (messageDescriptor: FormattedMessage.MessageDescriptor, values?: Object) => string;
formatHTMLMessage?: (messageDescriptor: FormattedMessage.MessageDescriptor, values?: Object) => string;
}
module IntlComponent {
interface DateTimeFormatProps {
/*
* one of "best fit" (default) | "lookup"
*/
localeMatcher?: string;
/*
* one of "basic" (default) | "best fit"
*/
formatMatcher?: string;
timeZone?: string,
hour12?: boolean;
/*
* one of "narrow" (default) | "short" | "long"
*/
weekday?: string;
/*
* one of "narrow" (default) | "short" | "long"
*/
era?: string;
/*
* one of "numeric" (default) | "2-digit"
*/
year?: string;
/*
* one of "numeric" (default) | "2-digit" | "narrow" | "short" | "long"
*/
month?: string;
/*
* one of "numeric" (default) | "2-digit"
*/
day?: string;
/*
* one of "numeric" (default) | "2-digit"
*/
hour?: string;
/*
* one of "numeric" (default) | "2-digit"
*/
minute?: string;
/*
* one of "numeric" (default) | "2-digit"
*/
second?: string;
/*
* one of "short" (default) | "long"
*/
timeZoneName?: string;
}
}
module FormattedDate {
export interface PropsBase extends IntlComponent.DateTimeFormatProps {
format?: string;
}
export interface Props extends PropsBase {
value: Date;
}
}
class FormattedDate extends React.Component<FormattedDate.Props, any> { }
module FormattedTime {
export interface PropsBase extends IntlComponent.DateTimeFormatProps {
format?: string;
}
export interface Props extends PropsBase {
value: Date;
}
}
class FormattedTime extends React.Component<FormattedTime.Props, any> { }
module FormattedRelative {
export interface PropsBase {
/*
* one of "second", "minute", "hour", "day", "month" or "year"
*/
units?: string;
/*
* one of "best fit" (default) | "numeric"
*/
style?: string;
format?: string;
updateInterval?: number;
initialNow?: any;
}
export interface Props extends PropsBase {
value: number;
}
}
class FormattedRelative extends React.Component<FormattedRelative.Props, any> { }
module FormattedMessage {
export interface MessageDescriptor {
id: string;
description?: string;
defaultMessage?: string;
}
export interface Props extends MessageDescriptor {
values?: Object;
tagName?: string;
}
}
class FormattedMessage extends React.Component<FormattedMessage.Props, any> { }
class FormattedHTMLMessage extends React.Component<FormattedMessage.Props, any> { }
module FormattedNumber {
export interface PropsBase {
format?: string;
/*
* one of "best fit" (default) | "lookup"
*/
localeMatcher?: string;
/*
* one of "decimal" (default) | "currency" | "percent"
*/
style?: string;
currency?: string,
/*
* one of "symbol" (default) | "code" | "name"
*/
currencyDisplay?: string;
useGrouping?: boolean;
minimumIntegerDigits?: number;
minimumFractionDigits?: number;
maximumFractionDigits?: number;
minimumSignificantDigits?: number;
maximumSignificantDigits?: number;
}
export interface Props extends PropsBase {
value: number;
}
}
class FormattedNumber extends React.Component<FormattedNumber.Props, any> { }
module FormattedPlural {
export interface PropsBase {
/*
* one of "cardinal" (default) | "ordinal"
*/
style?: string;
other?: any;
zero?: any;
one?: any;
two?: any;
few?: any;
many?: any;
}
export interface Props extends PropsBase {
value: number;
}
}
class FormattedPlural extends React.Component<FormattedPlural.Props, any> { }
module IntlProvider {
export interface Props {
locale?: string;
formats?: Object;
messages?: Object;
defaultLocale?: string;
defaultFormats?: Object;
}
}
class IntlProvider extends React.Component<IntlProvider.Props, any> { }
class LocaleData extends Array<Locale> {
}
}
declare module "react-intl" {
export = ReactIntl
}
declare module "react-intl/lib/locale-data/en" {
var data: ReactIntl.LocaleData;
export = data;
}