accounts-frontend/packages/app/components/ui/form/FormComponent.tsx

40 lines
1.1 KiB
TypeScript
Raw Normal View History

2021-03-26 08:49:04 +05:30
import React, { ReactNode } from 'react';
2019-12-07 16:58:52 +05:30
import { MessageDescriptor } from 'react-intl';
2021-03-26 08:49:04 +05:30
import i18n from 'app/services/i18n';
2019-12-07 16:58:52 +05:30
2021-03-26 08:49:04 +05:30
function isMessageDescriptor(value: any): value is MessageDescriptor {
return typeof value === 'object' && value.id;
}
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
*
2021-03-26 08:49:04 +05:30
* @deprecated
2020-05-24 04:38:24 +05:30
* @returns {string}
*/
2021-03-26 08:49:04 +05:30
formatMessage<T extends ReactNode | MessageDescriptor>(message: T): T extends MessageDescriptor ? string : T {
if (isMessageDescriptor(message)) {
// @ts-ignore
return i18n.getIntl().formatMessage(message);
2020-05-24 04:38:24 +05:30
}
2019-12-07 16:58:52 +05:30
2021-03-26 08:49:04 +05:30
// @ts-ignore
return message;
2020-05-24 04:38:24 +05:30
}
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
}