accounts-frontend/src/components/ui/form/FormComponent.tsx

42 lines
1009 B
TypeScript
Raw Normal View History

2019-12-07 16:58:52 +05:30
import React from 'react';
import { MessageDescriptor } from 'react-intl';
import i18n from 'services/i18n';
class FormComponent<P, S = {}> extends React.Component<P, S> {
/**
* 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');
}
if (typeof message === 'string') {
return message;
}
if (!message.id) {
throw new Error(`Invalid message format: ${JSON.stringify(message)}`);
}
return i18n.getIntl().formatMessage(message);
}
/**
* Focuses this field
*/
focus() {}
/**
* 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() {}
}
export default FormComponent;