native-document 1.0.211 → 1.0.213

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "native-document",
3
- "version": "1.0.211",
3
+ "version": "1.0.213",
4
4
  "description": "A reactive JavaScript framework that preserves native DOM simplicity without sacrificing modern features",
5
5
  "author": "AfroCodeur <https://github.com/afrocodeur>",
6
6
  "license": "MIT",
@@ -52,6 +52,7 @@ export default function FormControl(props) {
52
52
 
53
53
  FormControl.defaultTemplate = null;
54
54
  FormControl.defaultErrorsMapper = null;
55
+ FormControl.keyResolver = null;
55
56
 
56
57
  /**
57
58
  * Registers the render template for FormControl.
@@ -78,6 +79,10 @@ FormControl.setDefaultErrorsMapper = function(mapper) {
78
79
  FormControl.defaultErrorsMapper = mapper;
79
80
  };
80
81
 
82
+ FormControl.setKeyResolver = function(resolver) {
83
+ FormControl.keyResolver = resolver;
84
+ };
85
+
81
86
  /**
82
87
  * @param {GlobalAttributes} [props]
83
88
  * @returns {FormControl}
@@ -128,7 +133,19 @@ FormControl.prototype.fields = function(fieldBuilder) {
128
133
  return this;
129
134
  };
130
135
 
131
- FormControl.prototype.$registerField = function(name, field) {
136
+ const resolveFieldName = (_name, field) => {
137
+ let name = _name;
138
+ if(FormControl.keyResolver) {
139
+ name = FormControl.keyResolver(_name);
140
+ }
141
+ if(field?.$description.as) {
142
+ name = field.$description.as;
143
+ }
144
+ return name;
145
+ };
146
+
147
+ FormControl.prototype.$registerField = function(_name, field) {
148
+ const name = resolveFieldName(_name);
132
149
  this.$fields[name] = field;
133
150
 
134
151
  const dataSource = this.$description.data?.[name];
@@ -149,7 +166,9 @@ FormControl.prototype.$registerField = function(name, field) {
149
166
  * @returns {Field|null}
150
167
  */
151
168
  FormControl.prototype.get = function(fieldName) {
152
- const field = this.$fields[fieldName];
169
+ const name = resolveFieldName(fieldName, field);
170
+ const field = this.$fields[name];
171
+
153
172
  if(!field) {
154
173
  throw new NativeDocumentError(`Field "${fieldName}" not found in form`);
155
174
  }
@@ -30,6 +30,7 @@ export default function Field(name, type, props) {
30
30
  BaseComponent.call(this, props);
31
31
 
32
32
  this.$description = {
33
+ as: null,
33
34
  name: name,
34
35
  type: type,
35
36
  key: name,
@@ -120,6 +121,16 @@ Field.prototype.forceShowErrors = function(forceValue) {
120
121
  this.$description.showErrors.intercept(() => forceValue);
121
122
  };
122
123
 
124
+ /**
125
+ * the alias for form control mapping
126
+ * @param as
127
+ * @return {Field}
128
+ */
129
+ Field.prototype.as = function(as) {
130
+ this.$description.as = as;
131
+ return this;
132
+ };
133
+
123
134
  /**
124
135
  * @param {string} key
125
136
  * @returns {this}
@@ -30,6 +30,7 @@ export type FieldDescription = {
30
30
  export interface FieldInterface extends BaseComponent, HasValidation {
31
31
  forceShowErrors(forceValue: boolean): void;
32
32
  key(key: string): this;
33
+ as(key: string): this;
33
34
  suffix(suffix: string): this;
34
35
  field(): HTMLElement | DocumentFragment;
35
36
  input(callback: (input: HTMLElement) => void): this;
@@ -19,7 +19,7 @@ export type FormControlDescription = {
19
19
  props: GlobalAttributes;
20
20
  };
21
21
 
22
- export interface FormControlInterface extends BaseComponent {
22
+ export interface FormControlInterface extends Omit<BaseComponent, 'onChange'|'onReset'|'onSubmit'|'onPreventSubmit'|'onError'> {
23
23
  fields(fieldBuilder: (form: FormControlInterface) => void): this;
24
24
  get(fieldName: string): FieldInterface | null;
25
25
  layout(layoutCallback: (fields: Record<string, FieldInterface>) => ValidChild): this;
@@ -57,6 +57,7 @@ export declare function FormControl(props?: Record<string, unknown>): FormContro
57
57
  export declare namespace FormControl {
58
58
 
59
59
  function setDefaultErrorsMapper(mapper: (error: Error) => Record<string, string|[]>): void;
60
+ function setKeyResolver(resolver: (name: string) => void): void;
60
61
  function use(template: (description: FormControlDescription, instance: FormControlInterface) => ValidChild): void;
61
62
  function create(props?: Record<string, unknown>): FormControlInterface;
62
63
 
@@ -644,4 +644,12 @@ ObservableItem.prototype.clone = function() {
644
644
  }
645
645
 
646
646
  return new ObservableItem(clonedValue);
647
+ };
648
+
649
+ ObservableItem.prototype.asSet = function(value) {
650
+ return () => this.set(value);
651
+ };
652
+
653
+ ObservableItem.prototype.asToggle = function() {
654
+ return () => this.toggle();
647
655
  };
@@ -130,6 +130,10 @@ export interface ObservableItem<T = any> {
130
130
  toClamped(min: number | ObservableItem<number>, max: number | ObservableItem<number>): ObservableChecker<number>;
131
131
  toPercent(total: number | ObservableItem<number>): ObservableChecker<number>;
132
132
 
133
+ // -- callback builder (as*) -------------------------------------------------
134
+ asSet(value: T): ObservableChecker<T>;
135
+ asToggle(): ObservableChecker<boolean>;
136
+
133
137
  toString(): string;
134
138
  valueOf(): T;
135
139
  equals(value: any): boolean;