This repository has been archived by the owner on Sep 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
submit-button.control.ts
92 lines (83 loc) · 2.88 KB
/
submit-button.control.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import { noop } from '@proc7ts/primitives';
import { InControl } from '../../control';
import { InConverter, intoConvertedAspects } from '../../converter';
import { InMode } from '../../data';
import { inModeByForm } from '../../data/modes';
import { InElement } from '../../element.control';
import { AbstractInElement } from '../abstract-element.control';
/**
* Form submit button control.
*
* It is connected to submittable control (e.g. a form) and makes a button status reflect a form submit status.
* E.g. by disabling the button when submit is {@link InSubmit.Flags.ready not ready}, or while
* {@link InSubmit.Flags.busy submitting} the form.
*
* Submit button control can be created by {@link inSubmitButton} function.
*
* The value of the submit button is always `undefined`, so it is never submitted.
*
* @category Control
* @typeParam TElt - A type of submit button element.
*/
export type InSubmitButton<TElt extends HTMLElement = HTMLElement> = InElement<void, TElt>;
/**
* @category Control
*/
export namespace InSubmitButton {
/**
* Submit button control options.
*/
export interface Options {
/**
* Submitted control. Typically a {@link InContainer container}.
*/
readonly form: InControl<any>;
/**
* Additional input aspects to apply. These are aspect converters to constructed control from the
* {@link inValueOf same-valued one}.
*/
readonly aspects?: InConverter.Aspect<void> | readonly InConverter.Aspect<void>[] | undefined;
/**
* Input modes to derive from submitted control.
*
* Applied to submit button control by {@link inModeByForm}.
*/
modes?: {
/**
* Input mode to set when submit is not ready. E.g. when input is invalid. `on` (enabled) by default.
*/
readonly notReady?: InMode.Value | undefined;
/**
* Input mode to set when submit is not ready _and_ the form is submitted. `off` (disable) by default.
*/
readonly invalid?: InMode.Value | undefined;
/**
* Input mode to set while submitting. `off` (disabled) by default.
*/
readonly busy?: InMode.Value | undefined;
};
}
}
/**
* Creates submit button control.
*
* @category Control
* @param element - Submit button element to create control for.
* @param options - Submit button control options.
*
* @returns New submit button control.
*/
export function inSubmitButton<TElt extends HTMLElement>(
element: TElt,
options: InSubmitButton.Options,
): InSubmitButton<TElt> {
const { form, aspects, modes: { notReady = 'on', invalid = 'off', busy = 'off' } = {} } = options;
const control = new AbstractInElement<void, TElt>(element, {
aspects: [intoConvertedAspects(aspects)],
get: noop,
set: noop,
});
control.supply.needs(form);
control.setup(InMode, mode => mode.derive(inModeByForm(form, { notReady, invalid, busy })));
return control;
}