2018-05-07 17:41:22 +09:00
|
|
|
import React, { PureComponent } from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2018-11-14 11:38:41 +01:00
|
|
|
import { Components, registerComponent, mergeWithComponents, Locales } from 'meteor/vulcan:core';
|
2018-06-28 21:35:44 +02:00
|
|
|
import omit from 'lodash/omit';
|
2018-07-02 16:59:29 +02:00
|
|
|
import getContext from 'recompose/getContext';
|
2018-05-07 17:41:22 +09:00
|
|
|
|
2018-10-25 12:07:50 +02:00
|
|
|
// replaceable layout
|
|
|
|
const FormIntlLayout = ({ children }) => (
|
|
|
|
<div className="form-intl">{children}</div>
|
|
|
|
);
|
|
|
|
registerComponent({ name: 'FormIntlLayout', component: FormIntlLayout });
|
|
|
|
const FormIntlItemLayout = ({ locale, children }) => (
|
2018-11-22 16:09:31 +09:00
|
|
|
<div className={`form-intl-${locale.id}`}>
|
2018-10-25 12:07:50 +02:00
|
|
|
{children}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
registerComponent({
|
|
|
|
name: 'FormIntlItemLayout',
|
|
|
|
component: FormIntlItemLayout
|
|
|
|
});
|
2018-05-21 09:42:08 +09:00
|
|
|
|
2018-10-25 12:07:50 +02:00
|
|
|
class FormIntl extends PureComponent {
|
2018-05-21 09:42:08 +09:00
|
|
|
/*
|
|
|
|
|
2018-05-21 10:33:36 +09:00
|
|
|
Note: ideally we'd try to make sure to return the right path no matter
|
|
|
|
the order translations are stored in, but in practice we can't guarantee it
|
|
|
|
so we just use the order of the Locales array.
|
|
|
|
|
2018-05-21 09:42:08 +09:00
|
|
|
*/
|
2018-10-25 12:07:50 +02:00
|
|
|
getLocalePath = defaultIndex => {
|
2018-05-21 10:33:36 +09:00
|
|
|
return `${this.props.path}_intl.${defaultIndex}`;
|
2018-10-25 12:07:50 +02:00
|
|
|
};
|
2018-05-21 09:42:08 +09:00
|
|
|
|
2018-10-25 12:07:50 +02:00
|
|
|
render() {
|
|
|
|
const { name, formComponents } = this.props;
|
|
|
|
const FormComponents = mergeWithComponents(formComponents);
|
2018-09-23 08:13:09 +09:00
|
|
|
|
2018-05-09 10:38:21 +09:00
|
|
|
// do not pass FormIntl's own value, inputProperties, and intlInput props down
|
2018-10-25 12:07:50 +02:00
|
|
|
const properties = omit(
|
|
|
|
this.props,
|
|
|
|
'value',
|
|
|
|
'inputProperties',
|
|
|
|
'intlInput',
|
|
|
|
'nestedInput'
|
|
|
|
);
|
2018-05-07 17:41:22 +09:00
|
|
|
return (
|
2018-10-25 12:07:50 +02:00
|
|
|
<FormComponents.FormIntlLayout>
|
2018-05-21 09:42:08 +09:00
|
|
|
{Locales.map((locale, i) => (
|
2018-11-22 16:09:31 +09:00
|
|
|
<FormComponents.FormIntlItemLayout key={locale.id} locale={locale}>
|
2018-10-25 12:07:50 +02:00
|
|
|
<FormComponents.FormComponent
|
|
|
|
{...properties}
|
|
|
|
label={this.props.getLabel(name, locale.id)}
|
|
|
|
path={this.getLocalePath(i)}
|
|
|
|
locale={locale.id}
|
|
|
|
/>
|
|
|
|
</FormComponents.FormIntlItemLayout>
|
2018-05-07 17:41:22 +09:00
|
|
|
))}
|
2018-10-25 12:07:50 +02:00
|
|
|
</FormComponents.FormIntlLayout>
|
2018-05-07 17:41:22 +09:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-25 12:07:50 +02:00
|
|
|
FormIntl.propTypes = {
|
|
|
|
name: PropTypes.string.isRequired,
|
|
|
|
path: PropTypes.string.isRequired,
|
|
|
|
formComponents: PropTypes.object
|
|
|
|
};
|
|
|
|
|
|
|
|
registerComponent(
|
|
|
|
'FormIntl',
|
|
|
|
FormIntl,
|
|
|
|
getContext({
|
|
|
|
getLabel: PropTypes.func
|
|
|
|
})
|
|
|
|
);
|