|
@@ -6,31 +6,31 @@ import { SubmittableResult } from '@polkadot/api';
|
|
|
import { TxFailedCallback, TxCallback } from '@polkadot/react-components/Status/types';
|
|
|
import { MediaDropdownOptions } from './MediaDropdownOptions';
|
|
|
import { OnTxButtonClick } from '@polkadot/joy-utils/TxButton';
|
|
|
-import isEqual from 'lodash/isEqual'
|
|
|
+import isEqual from 'lodash/isEqual';
|
|
|
|
|
|
export const datePlaceholder = 'Date in format yyyy-mm-dd';
|
|
|
|
|
|
export type FormCallbacks = {
|
|
|
- onSubmit: OnTxButtonClick,
|
|
|
- onTxSuccess: TxCallback,
|
|
|
- onTxFailed: TxFailedCallback
|
|
|
+ onSubmit: OnTxButtonClick;
|
|
|
+ onTxSuccess: TxCallback;
|
|
|
+ onTxFailed: TxFailedCallback;
|
|
|
};
|
|
|
|
|
|
export type GenericMediaProp<FormValues> = {
|
|
|
- id: keyof FormValues,
|
|
|
- type: string,
|
|
|
- name: string,
|
|
|
- description?: string,
|
|
|
- required?: boolean,
|
|
|
- minItems?: number,
|
|
|
- maxItems?: number,
|
|
|
- minTextLength?: number,
|
|
|
- maxTextLength?: number,
|
|
|
- classId?: any
|
|
|
+ id: keyof FormValues;
|
|
|
+ type: string;
|
|
|
+ name: string;
|
|
|
+ description?: string;
|
|
|
+ required?: boolean;
|
|
|
+ minItems?: number;
|
|
|
+ maxItems?: number;
|
|
|
+ minTextLength?: number;
|
|
|
+ maxTextLength?: number;
|
|
|
+ classId?: any;
|
|
|
};
|
|
|
|
|
|
type BaseFieldProps<OuterProps, FormValues> = OuterProps & FormikProps<FormValues> & {
|
|
|
- field: GenericMediaProp<FormValues>
|
|
|
+ field: GenericMediaProp<FormValues>;
|
|
|
};
|
|
|
|
|
|
type MediaTextProps<OuterProps, FormValues> =
|
|
@@ -39,21 +39,21 @@ type MediaTextProps<OuterProps, FormValues> =
|
|
|
type MediaFieldProps<OuterProps, FormValues> =
|
|
|
BaseFieldProps<OuterProps, FormValues> &
|
|
|
JoyForms.LabelledProps<FormValues> & {
|
|
|
- fieldProps: any
|
|
|
+ fieldProps: any;
|
|
|
}
|
|
|
|
|
|
type MediaDropdownProps<OuterProps, FormValues> =
|
|
|
BaseFieldProps<OuterProps, FormValues> &
|
|
|
-{
|
|
|
- options: DropdownItemProps[]
|
|
|
-};
|
|
|
+ {
|
|
|
+ options: DropdownItemProps[];
|
|
|
+ };
|
|
|
|
|
|
type FormFields<OuterProps, FormValues> = {
|
|
|
- LabelledText: React.FunctionComponent<JoyForms.LabelledProps<FormValues>>
|
|
|
- LabelledField: React.FunctionComponent<JoyForms.LabelledProps<FormValues>>
|
|
|
- MediaText: React.FunctionComponent<MediaTextProps<OuterProps, FormValues>>
|
|
|
- MediaField: React.FunctionComponent<MediaFieldProps<OuterProps, FormValues>>
|
|
|
- MediaDropdown: React.FunctionComponent<MediaDropdownProps<OuterProps, FormValues>>
|
|
|
+ LabelledText: React.FunctionComponent<JoyForms.LabelledProps<FormValues>>;
|
|
|
+ LabelledField: React.FunctionComponent<JoyForms.LabelledProps<FormValues>>;
|
|
|
+ MediaText: React.FunctionComponent<MediaTextProps<OuterProps, FormValues>>;
|
|
|
+ MediaField: React.FunctionComponent<MediaFieldProps<OuterProps, FormValues>>;
|
|
|
+ MediaDropdown: React.FunctionComponent<MediaDropdownProps<OuterProps, FormValues>>;
|
|
|
};
|
|
|
|
|
|
export type MediaFormProps<OuterProps, FormValues> =
|
|
@@ -61,21 +61,20 @@ export type MediaFormProps<OuterProps, FormValues> =
|
|
|
FormikProps<FormValues> &
|
|
|
FormFields<OuterProps, FormValues> &
|
|
|
FormCallbacks & {
|
|
|
- opts: MediaDropdownOptions
|
|
|
- isFieldChanged: (field: keyof FormValues | GenericMediaProp<FormValues>) => boolean
|
|
|
+ opts: MediaDropdownOptions;
|
|
|
+ isFieldChanged: (field: keyof FormValues | GenericMediaProp<FormValues>) => boolean;
|
|
|
};
|
|
|
|
|
|
export function withMediaForm<OuterProps, FormValues>
|
|
|
- (Component: React.ComponentType<MediaFormProps<OuterProps, FormValues>>)
|
|
|
-{
|
|
|
+(Component: React.ComponentType<MediaFormProps<OuterProps, FormValues>>) {
|
|
|
type FieldName = keyof FormValues
|
|
|
|
|
|
type FieldObject = GenericMediaProp<FormValues>
|
|
|
|
|
|
const LabelledText = JoyForms.LabelledText<FormValues>();
|
|
|
-
|
|
|
+
|
|
|
const LabelledField = JoyForms.LabelledField<FormValues>();
|
|
|
-
|
|
|
+
|
|
|
function MediaText (props: MediaTextProps<OuterProps, FormValues>) {
|
|
|
const { field: f } = props;
|
|
|
return !f ? null : <LabelledText name={f.id} label={f.name} tooltip={f.description} required={f.required} {...props} />;
|
|
@@ -87,7 +86,11 @@ export function withMediaForm<OuterProps, FormValues>
|
|
|
const { id } = f;
|
|
|
|
|
|
const allFieldProps = {
|
|
|
- name: id, id, placeholder, className, style,
|
|
|
+ name: id,
|
|
|
+ id,
|
|
|
+ placeholder,
|
|
|
+ className,
|
|
|
+ style,
|
|
|
disabled: otherProps.isSubmitting,
|
|
|
...fieldProps
|
|
|
};
|
|
@@ -97,7 +100,7 @@ export function withMediaForm<OuterProps, FormValues>
|
|
|
<Field {...allFieldProps} />
|
|
|
</LabelledField>
|
|
|
);
|
|
|
- }
|
|
|
+ };
|
|
|
|
|
|
const MediaDropdown = (props: MediaDropdownProps<OuterProps, FormValues>) => {
|
|
|
const { field: f, options = [] } = props;
|
|
@@ -116,8 +119,8 @@ export function withMediaForm<OuterProps, FormValues>
|
|
|
onChange: (_event: any, data: DropdownProps) => {
|
|
|
props.setFieldValue(id, data.value);
|
|
|
}
|
|
|
- }} />
|
|
|
- }
|
|
|
+ }} />;
|
|
|
+ };
|
|
|
|
|
|
return function (props: MediaFormProps<OuterProps, FormValues>) {
|
|
|
const {
|
|
@@ -128,11 +131,11 @@ export function withMediaForm<OuterProps, FormValues>
|
|
|
errors,
|
|
|
isValid,
|
|
|
setSubmitting,
|
|
|
- opts = MediaDropdownOptions.Empty,
|
|
|
+ opts = MediaDropdownOptions.Empty
|
|
|
} = props;
|
|
|
|
|
|
const isFieldChanged = (field: FieldName | FieldObject): boolean => {
|
|
|
- const fieldName = typeof field === 'string' ? field : (field as FieldObject).id
|
|
|
+ const fieldName = typeof field === 'string' ? field : (field as FieldObject).id;
|
|
|
return (
|
|
|
dirty &&
|
|
|
touched[fieldName] === true &&
|
|
@@ -144,10 +147,10 @@ export function withMediaForm<OuterProps, FormValues>
|
|
|
if (isValid) {
|
|
|
sendTx();
|
|
|
} else {
|
|
|
- console.log('Form is invalid. Errors:', errors)
|
|
|
+ console.log('Form is invalid. Errors:', errors);
|
|
|
}
|
|
|
};
|
|
|
-
|
|
|
+
|
|
|
const onTxSuccess: TxCallback = (_txResult: SubmittableResult) => {
|
|
|
setSubmitting(false);
|
|
|
};
|
|
@@ -156,7 +159,7 @@ export function withMediaForm<OuterProps, FormValues>
|
|
|
setSubmitting(false);
|
|
|
if (txResult === null) {
|
|
|
// Tx cancelled
|
|
|
- return;
|
|
|
+
|
|
|
}
|
|
|
};
|
|
|
|
|
@@ -178,7 +181,7 @@ export function withMediaForm<OuterProps, FormValues>
|
|
|
// Other
|
|
|
opts,
|
|
|
isFieldChanged
|
|
|
- }
|
|
|
+ };
|
|
|
|
|
|
return <Component {...allProps} />;
|
|
|
};
|