Skip to content

Commit d7733cd

Browse files
committed
Also added props tagName and tagProps for components FormattedDate, FormattedTime, FormattedRelative, FormattedPlural and FormattedNumber.
Example of FormattedNumber component working: ``` <FormattedNumber tagName={Text} value={4.344554} /> ```
1 parent 894ef55 commit d7733cd

File tree

6 files changed

+89
-20
lines changed

6 files changed

+89
-20
lines changed

src/components/date.js

+20-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* See the accompanying LICENSE file for terms.
55
*/
66

7-
import React, {Component, PropTypes} from 'react';
7+
import {Component, PropTypes, createElement} from 'react';
88
import {intlShape, dateTimeFormatPropTypes} from '../types';
99
import {invariantIntlContext, shouldIntlComponentUpdate} from '../utils';
1010

@@ -20,15 +20,20 @@ export default class FormattedDate extends Component {
2020

2121
render() {
2222
const {formatDate} = this.context.intl;
23-
const {value, children} = this.props;
23+
const {value,
24+
children,
25+
tagName,
26+
tagProps,
27+
} = this.props;
2428

2529
let formattedDate = formatDate(value, this.props);
2630

2731
if (typeof children === 'function') {
2832
return children(formattedDate);
2933
}
3034

31-
return <span>{formattedDate}</span>;
35+
return createElement(tagName, tagProps, formattedDate);
36+
3237
}
3338
}
3439

@@ -40,7 +45,17 @@ FormattedDate.contextTypes = {
4045

4146
FormattedDate.propTypes = {
4247
...dateTimeFormatPropTypes,
43-
value : PropTypes.any.isRequired,
44-
format : PropTypes.string,
4548
children: PropTypes.func,
49+
format : PropTypes.string,
50+
tagName: PropTypes.oneOfType([
51+
PropTypes.string,
52+
PropTypes.func,
53+
]),
54+
tagProps: PropTypes.object,
55+
value : PropTypes.any.isRequired,
56+
};
57+
58+
FormattedDate.defaultProps = {
59+
tagName: 'span',
60+
tagProps: null,
4661
};

src/components/message.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,9 @@ FormattedMessage.propTypes = {
110110
...messageDescriptorPropTypes,
111111
values : PropTypes.object,
112112
tagName: PropTypes.oneOfType([
113-
PropTypes.string,
114-
PropTypes.func
115-
]),
113+
PropTypes.string,
114+
PropTypes.func,
115+
]),
116116
tagProps: PropTypes.object,
117117
children: PropTypes.func,
118118
};

src/components/number.js

+18-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* See the accompanying LICENSE file for terms.
55
*/
66

7-
import React, {Component, PropTypes} from 'react';
7+
import {Component, PropTypes, createElement} from 'react';
88
import {intlShape, numberFormatPropTypes} from '../types';
99
import {invariantIntlContext, shouldIntlComponentUpdate} from '../utils';
1010

@@ -20,15 +20,20 @@ export default class FormattedNumber extends Component {
2020

2121
render() {
2222
const {formatNumber} = this.context.intl;
23-
const {value, children} = this.props;
23+
const {value,
24+
children,
25+
tagName,
26+
tagProps,
27+
} = this.props;
2428

2529
let formattedNumber = formatNumber(value, this.props);
2630

2731
if (typeof children === 'function') {
2832
return children(formattedNumber);
2933
}
3034

31-
return <span>{formattedNumber}</span>;
35+
return createElement(tagName, tagProps, formattedNumber);
36+
3237
}
3338
}
3439

@@ -42,5 +47,15 @@ FormattedNumber.propTypes = {
4247
...numberFormatPropTypes,
4348
value : PropTypes.any.isRequired,
4449
format : PropTypes.string,
50+
tagName: PropTypes.oneOfType([
51+
PropTypes.string,
52+
PropTypes.func,
53+
]),
54+
tagProps: PropTypes.object,
4555
children: PropTypes.func,
4656
};
57+
58+
FormattedNumber.defaultProps = {
59+
tagName: 'span',
60+
tagProps: null,
61+
};

src/components/plural.js

+17-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* See the accompanying LICENSE file for terms.
55
*/
66

7-
import React, {Component, PropTypes} from 'react';
7+
import {Component, PropTypes, createElement} from 'react';
88
import {intlShape, pluralFormatPropTypes} from '../types';
99
import {invariantIntlContext, shouldIntlComponentUpdate} from '../utils';
1010

@@ -20,7 +20,12 @@ export default class FormattedPlural extends Component {
2020

2121
render() {
2222
const {formatPlural} = this.context.intl;
23-
const {value, other, children} = this.props;
23+
const {value,
24+
other,
25+
children,
26+
tagName,
27+
tagProps,
28+
} = this.props;
2429

2530
let pluralCategory = formatPlural(value, this.props);
2631
let formattedPlural = this.props[pluralCategory] || other;
@@ -29,7 +34,8 @@ export default class FormattedPlural extends Component {
2934
return children(formattedPlural);
3035
}
3136

32-
return <span>{formattedPlural}</span>;
37+
return createElement(tagName, tagProps, formattedPlural);
38+
3339
}
3440
}
3541

@@ -50,9 +56,17 @@ FormattedPlural.propTypes = {
5056
few : PropTypes.node,
5157
many : PropTypes.node,
5258

59+
tagName: PropTypes.oneOfType([
60+
PropTypes.string,
61+
PropTypes.func,
62+
]),
63+
tagProps: PropTypes.object,
64+
5365
children: PropTypes.func,
5466
};
5567

5668
FormattedPlural.defaultProps = {
5769
style: 'cardinal',
70+
tagName: 'span',
71+
tagProps: null,
5872
};

src/components/relative.js

+14-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* See the accompanying LICENSE file for terms.
55
*/
66

7-
import React, {Component, PropTypes} from 'react';
7+
import {Component, PropTypes, createElement} from 'react';
88
import {intlShape, relativeFormatPropTypes} from '../types';
99
import {invariantIntlContext, shouldIntlComponentUpdate} from '../utils';
1010

@@ -109,7 +109,11 @@ export default class FormattedRelative extends Component {
109109

110110
render() {
111111
const {formatRelative} = this.context.intl;
112-
const {value, children} = this.props;
112+
const {value,
113+
children,
114+
tagName,
115+
tagProps,
116+
} = this.props;
113117

114118
let formattedRelative = formatRelative(value, {
115119
...this.props,
@@ -120,7 +124,7 @@ export default class FormattedRelative extends Component {
120124
return children(formattedRelative);
121125
}
122126

123-
return <span>{formattedRelative}</span>;
127+
return createElement(tagName, tagProps, formattedRelative);
124128
}
125129
}
126130

@@ -136,9 +140,16 @@ FormattedRelative.propTypes = {
136140
format : PropTypes.string,
137141
updateInterval: PropTypes.number,
138142
initialNow : PropTypes.any,
143+
tagName: PropTypes.oneOfType([
144+
PropTypes.string,
145+
PropTypes.func,
146+
]),
147+
tagProps: PropTypes.object,
139148
children : PropTypes.func,
140149
};
141150

142151
FormattedRelative.defaultProps = {
152+
tagName: 'span',
153+
tagProps: null,
143154
updateInterval: 1000 * 10,
144155
};

src/components/time.js

+17-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* See the accompanying LICENSE file for terms.
55
*/
66

7-
import React, {Component, PropTypes} from 'react';
7+
import {Component, PropTypes, createElement} from 'react';
88
import {intlShape, dateTimeFormatPropTypes} from '../types';
99
import {invariantIntlContext, shouldIntlComponentUpdate} from '../utils';
1010

@@ -20,15 +20,19 @@ export default class FormattedTime extends Component {
2020

2121
render() {
2222
const {formatTime} = this.context.intl;
23-
const {value, children} = this.props;
23+
const {value,
24+
children,
25+
tagName,
26+
tagProps,
27+
} = this.props;
2428

2529
let formattedTime = formatTime(value, this.props);
2630

2731
if (typeof children === 'function') {
2832
return children(formattedTime);
2933
}
3034

31-
return <span>{formattedTime}</span>;
35+
return createElement(tagName, tagProps, formattedTime);
3236
}
3337
}
3438

@@ -41,6 +45,16 @@ FormattedTime.contextTypes = {
4145
FormattedTime.propTypes = {
4246
...dateTimeFormatPropTypes,
4347
value : PropTypes.any.isRequired,
48+
tagName: PropTypes.oneOfType([
49+
PropTypes.string,
50+
PropTypes.func,
51+
]),
52+
tagProps: PropTypes.object,
4453
format : PropTypes.string,
4554
children: PropTypes.func,
4655
};
56+
57+
FormattedTime.defaultProps = {
58+
tagName: 'span',
59+
tagProps: null,
60+
};

0 commit comments

Comments
 (0)