-
Notifications
You must be signed in to change notification settings - Fork 405
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Align OL measure number format of tooltip to measure panel's one (#3673)
- Loading branch information
1 parent
afacd43
commit fac0390
Showing
4 changed files
with
112 additions
and
3 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
web/client/components/I18N/enhancers/__tests__/addI18NProps-test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* Copyright 2019, GeoSolutions Sas. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
const React = require('react'); | ||
const ReactDOM = require('react-dom'); | ||
const {createSink} = require('recompose'); | ||
const expect = require('expect'); | ||
const addI18NProps = require('../addI18NProps'); | ||
var Localized = require('../../Localized'); | ||
|
||
|
||
describe('addI18NProps enhancer', () => { | ||
beforeEach((done) => { | ||
document.body.innerHTML = '<div id="container"></div>'; | ||
setTimeout(done); | ||
}); | ||
afterEach((done) => { | ||
ReactDOM.unmountComponentAtNode(document.getElementById("container")); | ||
document.body.innerHTML = ''; | ||
setTimeout(done); | ||
}); | ||
it('addI18NProps format with no context', () => { | ||
const Sink = addI18NProps(['formatNumber'])(createSink(props => { | ||
expect(props).toExist(); | ||
expect(props.formatNumber).toExist(); | ||
// this is the default implementation. | ||
expect(props.formatNumber(1.1)).toBe(1.1); | ||
expect(props.formatNumber(1000)).toBe(1000); | ||
})); | ||
ReactDOM.render(<Sink />, document.getElementById("container")); | ||
}); | ||
it('addI18NProps format numbers', () => { | ||
const Sink = addI18NProps(['formatNumber'])(createSink( props => { | ||
expect(props).toExist(); | ||
expect(props.formatNumber).toExist(); | ||
expect(typeof props.formatNumber(1)).toBe('string'); | ||
expect(props.formatNumber(1.1)).toBe("1.1"); | ||
expect(props.formatNumber(1000)).toBe("1,000"); | ||
})); | ||
ReactDOM.render(<Localized locale="en-EN" messages={{}}> | ||
<Sink /> | ||
</Localized>, document.getElementById("container")); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* Copyright 2019, GeoSolutions Sas. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
const {injectIntl} = require('react-intl'); | ||
const PropTypes = require('prop-types'); | ||
const {omit} = require('lodash'); | ||
const { compose, branch, getContext, withProps, withPropsOnChange, mapProps } = require('recompose'); | ||
|
||
const omitProps = keys => mapProps(props => omit(props, keys)); | ||
|
||
// TODO: provide better defaults | ||
const defaults = { | ||
locale: navigator && navigator.language, | ||
formats: { }, | ||
messages: { }, | ||
defaultLocale: 'en', | ||
defaultFormats: {}, | ||
formatDate: v => v, | ||
formatTime: v => v, | ||
formatRelative: v => v, | ||
formatNumber: v => v, | ||
formatPlural: v => v, | ||
formatMessage: v => v, | ||
formatHTMLMessage: v => v, | ||
now: () => new Date() | ||
}; | ||
|
||
/** | ||
* Add i18n functionalities and properties as props. Useful to get format functions when the react-intl components can not be used (i.e. with wrapped libs) | ||
* @name addI18NFormat | ||
* @param {string[]} props add the props to format as props. Should be keys of of this interface https://github.com/yahoo/react-intl/wiki/API#intlshape | ||
* @example | ||
* addI18NProps(['formatNumber'])(MyCmp); // MyCmp will receive `formatNumber` from current locale Intl object as a property | ||
*/ | ||
module.exports = (propsToAdd = []) => compose( | ||
// check intl and inject (or add default dummy object) | ||
getContext({intl: PropTypes.object}), | ||
branch( | ||
({intl}) => !!intl, | ||
injectIntl, | ||
withProps({intl: defaults}) | ||
), | ||
// add propsToAdd properties from intl object | ||
withPropsOnChange(['intl'], ({ intl = {} }) => propsToAdd.reduce((acc = {}, k) => ({ | ||
...acc, | ||
[k]: intl[k] | ||
}), {})), | ||
// clean up intl property | ||
omitProps(['intl']) | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters