2019-12-07 16:58:52 +05:30
|
|
|
import React from 'react';
|
|
|
|
import { MessageDescriptor } from 'react-intl';
|
2019-12-08 00:32:00 +05:30
|
|
|
import i18n from 'app/services/i18n';
|
2019-12-07 16:58:52 +05:30
|
|
|
|
2020-01-18 02:07:52 +05:30
|
|
|
export default class FormComponent<P, S = {}> extends React.Component<P, S> {
|
2020-05-24 04:38:24 +05:30
|
|
|
/**
|
|
|
|
* Formats message resolving intl translations
|
|
|
|
*
|
|
|
|
* @param {string|object} message - message string, or intl message descriptor with an `id` field
|
|
|
|
*
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
formatMessage(message: string | MessageDescriptor): string {
|
|
|
|
if (!message) {
|
|
|
|
throw new Error('A message is required');
|
|
|
|
}
|
2019-12-07 16:58:52 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
if (typeof message === 'string') {
|
|
|
|
return message;
|
|
|
|
}
|
2019-12-07 16:58:52 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
if (!message.id) {
|
|
|
|
throw new Error(`Invalid message format: ${JSON.stringify(message)}`);
|
|
|
|
}
|
2019-12-07 16:58:52 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
return i18n.getIntl().formatMessage(message);
|
|
|
|
}
|
2019-12-07 16:58:52 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
/**
|
|
|
|
* Focuses this field
|
|
|
|
*/
|
|
|
|
focus() {}
|
2019-12-07 16:58:52 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
/**
|
|
|
|
* A hook, that called, when the form was submitted with invalid data
|
|
|
|
* This is useful for the cases, when some field needs to be refreshed e.g. captcha
|
|
|
|
*/
|
|
|
|
onFormInvalid() {}
|
2019-12-07 16:58:52 +05:30
|
|
|
}
|