@conform-to/dom
Advanced tools
Comparing version 0.1.1 to 0.2.0
@@ -5,4 +5,4 @@ export declare type Constraint = { | ||
maxLength?: number; | ||
min?: string; | ||
max?: string; | ||
min?: string | number; | ||
max?: string | number; | ||
step?: string; | ||
@@ -12,8 +12,7 @@ multiple?: boolean; | ||
}; | ||
export interface FieldConfig<Type = any> { | ||
export interface FieldProps<Type = any> extends Constraint { | ||
name: string; | ||
initialValue?: FieldsetData<Type, string>; | ||
defaultValue?: FieldsetData<Type, string>; | ||
error?: FieldsetData<Type, string>; | ||
form?: string; | ||
constraint?: Constraint; | ||
} | ||
@@ -29,3 +28,3 @@ export declare type Schema<Type extends Record<string, any>> = { | ||
*/ | ||
export declare type FieldsetData<Type, Value> = Type extends string | number | Date | undefined ? Value : Type extends Array<infer InnerType> ? Array<FieldsetData<InnerType, Value>> : Type extends Object ? { | ||
export declare type FieldsetData<Type, Value> = Type extends string | number | Date | boolean | undefined ? Value : Type extends Array<infer InnerType> ? Array<FieldsetData<InnerType, Value>> : Type extends Object ? { | ||
[Key in keyof Type]?: FieldsetData<Type[Key], Value>; | ||
@@ -42,14 +41,42 @@ } : unknown; | ||
export declare type FieldElement = HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement | HTMLButtonElement; | ||
export declare type FormResult<T> = { | ||
state: 'processed'; | ||
value: FieldsetData<T, string> | null; | ||
error: FieldsetData<T, string> | null; | ||
export interface FormState<T> { | ||
value: FieldsetData<T, string>; | ||
error: FieldsetData<T, string>; | ||
} | ||
export declare type Submission<T extends Record<string, unknown>> = { | ||
state: 'modified'; | ||
form: FormState<T>; | ||
} | { | ||
state: 'rejected'; | ||
value: FieldsetData<T, string> | null; | ||
error: FieldsetData<T, string>; | ||
form: FormState<T>; | ||
} | { | ||
state: 'accepted'; | ||
value: T; | ||
data: T; | ||
form: FormState<T>; | ||
}; | ||
export interface ControlButtonProps { | ||
type: 'submit'; | ||
name: string; | ||
value: string; | ||
formNoValidate: boolean; | ||
} | ||
export interface ControlAction<T = unknown> { | ||
prepend: { | ||
defaultValue: T; | ||
}; | ||
append: { | ||
defaultValue: T; | ||
}; | ||
replace: { | ||
defaultValue: T; | ||
index: number; | ||
}; | ||
remove: { | ||
index: number; | ||
}; | ||
reorder: { | ||
from: number; | ||
to: number; | ||
}; | ||
} | ||
export declare function isFieldsetElement(element: unknown): element is FieldsetElement; | ||
@@ -61,9 +88,9 @@ export declare function isFieldElement(element: unknown): element is FieldElement; | ||
export declare function reportValidity(fieldset: FieldsetElement): boolean; | ||
export declare function createFieldConfig<Type extends Record<string, any>>(schema: Schema<Type>, options: { | ||
export declare function getFieldProps<Type extends Record<string, any>>(schema: Schema<Type>, options: { | ||
name?: string; | ||
form?: string; | ||
initialValue?: FieldsetData<Type, string>; | ||
defaultValue?: FieldsetData<Type, string>; | ||
error?: FieldsetData<Type, string>; | ||
}): { | ||
[Key in keyof Type]-?: FieldConfig<Type[Key]>; | ||
[Key in keyof Type]-?: FieldProps<Type[Key]>; | ||
}; | ||
@@ -73,10 +100,6 @@ export declare function shouldSkipValidate(event: SubmitEvent): boolean; | ||
export declare function getName(paths: Array<string | number>): string; | ||
export declare function transform(entries: Array<[string, FormDataEntryValue]> | Iterable<[string, FormDataEntryValue]>): unknown; | ||
export declare function createControlButton(name: string, action: 'prepend' | 'append' | 'remove', data: any): { | ||
type: 'submit'; | ||
name: string; | ||
value: string; | ||
formNoValidate: boolean; | ||
}; | ||
export declare function parse(payload: FormData | URLSearchParams): FormResult<unknown>; | ||
export declare function transform(entries: Array<[string, FormDataEntryValue]> | Iterable<[string, FormDataEntryValue]>): Record<string, unknown>; | ||
export declare function getControlButtonProps<Action extends keyof ControlAction, Payload extends ControlAction[Action]>(name: string, action: Action, payload: Payload): ControlButtonProps; | ||
export declare function applyControlCommand<Type, Action extends keyof ControlAction<Type>, Payload extends ControlAction<Type>[Action]>(list: Array<Type>, action: Action | string, payload: Payload): Array<Type>; | ||
export declare function parse(payload: FormData | URLSearchParams): Submission<Record<string, unknown>>; | ||
export declare function getFieldElements(fieldset: FieldsetElement, key: string): FieldElement[]; |
128
index.js
@@ -5,2 +5,4 @@ 'use strict'; | ||
var _rollupPluginBabelHelpers = require('./_virtual/_rollupPluginBabelHelpers.js'); | ||
/** | ||
@@ -55,17 +57,18 @@ * Data structure of the form value | ||
} | ||
function createFieldConfig(schema, options) { | ||
function getFieldProps(schema, options) { | ||
var result = {}; | ||
for (var key of Object.keys(schema.fields)) { | ||
var _options$initialValue, _options$error; | ||
var _options$defaultValue, _options$error; | ||
var constraint = schema.fields[key]; | ||
var config = { | ||
var props = _rollupPluginBabelHelpers.objectSpread2({ | ||
name: options.name ? "".concat(options.name, ".").concat(key) : key, | ||
form: options.form, | ||
initialValue: (_options$initialValue = options.initialValue) === null || _options$initialValue === void 0 ? void 0 : _options$initialValue[key], | ||
error: (_options$error = options.error) === null || _options$error === void 0 ? void 0 : _options$error[key], | ||
constraint | ||
}; | ||
result[key] = config; | ||
defaultValue: (_options$defaultValue = options.defaultValue) === null || _options$defaultValue === void 0 ? void 0 : _options$defaultValue[key], | ||
error: (_options$error = options.error) === null || _options$error === void 0 ? void 0 : _options$error[key] | ||
}, constraint); | ||
result[key] = props; | ||
} | ||
@@ -148,10 +151,61 @@ | ||
} | ||
function createControlButton(name, action, data) { | ||
function getControlButtonProps(name, action, payload) { | ||
return { | ||
type: 'submit', | ||
name: '__conform__', | ||
value: [name, action, JSON.stringify(data)].join('::'), | ||
value: [name, action, JSON.stringify(payload)].join('::'), | ||
formNoValidate: true | ||
}; | ||
} | ||
function applyControlCommand(list, action, payload) { | ||
switch (action) { | ||
case 'prepend': | ||
{ | ||
var { | ||
defaultValue | ||
} = payload; | ||
list.unshift(defaultValue); | ||
break; | ||
} | ||
case 'append': | ||
{ | ||
var { | ||
defaultValue: _defaultValue | ||
} = payload; | ||
list.push(_defaultValue); | ||
break; | ||
} | ||
case 'replace': | ||
{ | ||
var { | ||
defaultValue: _defaultValue2, | ||
index: _index | ||
} = payload; | ||
list.splice(_index, 1, _defaultValue2); | ||
break; | ||
} | ||
case 'remove': | ||
var { | ||
index | ||
} = payload; | ||
list.splice(index, 1); | ||
break; | ||
case 'reorder': | ||
var { | ||
from, | ||
to | ||
} = payload; | ||
list.splice(to, 0, ...list.splice(from, 1)); | ||
break; | ||
default: | ||
throw new Error('Invalid action found'); | ||
} | ||
return list; | ||
} | ||
function parse(payload) { | ||
@@ -187,34 +241,11 @@ var command = payload.get('__conform__'); | ||
switch (action) { | ||
case 'prepend': | ||
{ | ||
var initialValue = JSON.parse(json); | ||
list.unshift(initialValue); | ||
break; | ||
} | ||
case 'append': | ||
{ | ||
var _initialValue = JSON.parse(json); | ||
list.push(_initialValue); | ||
break; | ||
} | ||
case 'remove': | ||
var { | ||
index | ||
} = JSON.parse(json); | ||
list.splice(index, 1); | ||
break; | ||
default: | ||
throw new Error('Invalid action found; Only `prepend`, `append` and `remove` is accepted'); | ||
} | ||
applyControlCommand(list, action, JSON.parse(json)); | ||
} catch (e) { | ||
return { | ||
state: 'rejected', | ||
value, | ||
error: { | ||
__conform__: e instanceof Error ? e.message : 'Something went wrong' | ||
form: { | ||
value, | ||
error: { | ||
__conform__: e instanceof Error ? e.message : 'Something went wrong' | ||
} | ||
} | ||
@@ -225,5 +256,7 @@ }; | ||
return { | ||
state: 'processed', | ||
value, | ||
error: null | ||
state: 'modified', | ||
form: { | ||
value, | ||
error: {} | ||
} | ||
}; | ||
@@ -234,3 +267,7 @@ } | ||
state: 'accepted', | ||
value | ||
data: value, | ||
form: { | ||
value, | ||
error: {} | ||
} | ||
}; | ||
@@ -247,5 +284,6 @@ } | ||
exports.createControlButton = createControlButton; | ||
exports.createFieldConfig = createFieldConfig; | ||
exports.applyControlCommand = applyControlCommand; | ||
exports.getControlButtonProps = getControlButtonProps; | ||
exports.getFieldElements = getFieldElements; | ||
exports.getFieldProps = getFieldProps; | ||
exports.getName = getName; | ||
@@ -252,0 +290,0 @@ exports.getPaths = getPaths; |
@@ -0,1 +1,3 @@ | ||
import { objectSpread2 as _objectSpread2 } from './_virtual/_rollupPluginBabelHelpers.js'; | ||
/** | ||
@@ -50,17 +52,18 @@ * Data structure of the form value | ||
} | ||
function createFieldConfig(schema, options) { | ||
function getFieldProps(schema, options) { | ||
var result = {}; | ||
for (var key of Object.keys(schema.fields)) { | ||
var _options$initialValue, _options$error; | ||
var _options$defaultValue, _options$error; | ||
var constraint = schema.fields[key]; | ||
var config = { | ||
var props = _objectSpread2({ | ||
name: options.name ? "".concat(options.name, ".").concat(key) : key, | ||
form: options.form, | ||
initialValue: (_options$initialValue = options.initialValue) === null || _options$initialValue === void 0 ? void 0 : _options$initialValue[key], | ||
error: (_options$error = options.error) === null || _options$error === void 0 ? void 0 : _options$error[key], | ||
constraint | ||
}; | ||
result[key] = config; | ||
defaultValue: (_options$defaultValue = options.defaultValue) === null || _options$defaultValue === void 0 ? void 0 : _options$defaultValue[key], | ||
error: (_options$error = options.error) === null || _options$error === void 0 ? void 0 : _options$error[key] | ||
}, constraint); | ||
result[key] = props; | ||
} | ||
@@ -143,10 +146,61 @@ | ||
} | ||
function createControlButton(name, action, data) { | ||
function getControlButtonProps(name, action, payload) { | ||
return { | ||
type: 'submit', | ||
name: '__conform__', | ||
value: [name, action, JSON.stringify(data)].join('::'), | ||
value: [name, action, JSON.stringify(payload)].join('::'), | ||
formNoValidate: true | ||
}; | ||
} | ||
function applyControlCommand(list, action, payload) { | ||
switch (action) { | ||
case 'prepend': | ||
{ | ||
var { | ||
defaultValue | ||
} = payload; | ||
list.unshift(defaultValue); | ||
break; | ||
} | ||
case 'append': | ||
{ | ||
var { | ||
defaultValue: _defaultValue | ||
} = payload; | ||
list.push(_defaultValue); | ||
break; | ||
} | ||
case 'replace': | ||
{ | ||
var { | ||
defaultValue: _defaultValue2, | ||
index: _index | ||
} = payload; | ||
list.splice(_index, 1, _defaultValue2); | ||
break; | ||
} | ||
case 'remove': | ||
var { | ||
index | ||
} = payload; | ||
list.splice(index, 1); | ||
break; | ||
case 'reorder': | ||
var { | ||
from, | ||
to | ||
} = payload; | ||
list.splice(to, 0, ...list.splice(from, 1)); | ||
break; | ||
default: | ||
throw new Error('Invalid action found'); | ||
} | ||
return list; | ||
} | ||
function parse(payload) { | ||
@@ -182,34 +236,11 @@ var command = payload.get('__conform__'); | ||
switch (action) { | ||
case 'prepend': | ||
{ | ||
var initialValue = JSON.parse(json); | ||
list.unshift(initialValue); | ||
break; | ||
} | ||
case 'append': | ||
{ | ||
var _initialValue = JSON.parse(json); | ||
list.push(_initialValue); | ||
break; | ||
} | ||
case 'remove': | ||
var { | ||
index | ||
} = JSON.parse(json); | ||
list.splice(index, 1); | ||
break; | ||
default: | ||
throw new Error('Invalid action found; Only `prepend`, `append` and `remove` is accepted'); | ||
} | ||
applyControlCommand(list, action, JSON.parse(json)); | ||
} catch (e) { | ||
return { | ||
state: 'rejected', | ||
value, | ||
error: { | ||
__conform__: e instanceof Error ? e.message : 'Something went wrong' | ||
form: { | ||
value, | ||
error: { | ||
__conform__: e instanceof Error ? e.message : 'Something went wrong' | ||
} | ||
} | ||
@@ -220,5 +251,7 @@ }; | ||
return { | ||
state: 'processed', | ||
value, | ||
error: null | ||
state: 'modified', | ||
form: { | ||
value, | ||
error: {} | ||
} | ||
}; | ||
@@ -229,3 +262,7 @@ } | ||
state: 'accepted', | ||
value | ||
data: value, | ||
form: { | ||
value, | ||
error: {} | ||
} | ||
}; | ||
@@ -242,2 +279,2 @@ } | ||
export { createControlButton, createFieldConfig, getFieldElements, getName, getPaths, isFieldElement, isFieldsetElement, parse, reportValidity, setFieldState, shouldSkipValidate, transform }; | ||
export { applyControlCommand, getControlButtonProps, getFieldElements, getFieldProps, getName, getPaths, isFieldElement, isFieldsetElement, parse, reportValidity, setFieldState, shouldSkipValidate, transform }; |
@@ -5,3 +5,3 @@ { | ||
"license": "MIT", | ||
"version": "0.1.1", | ||
"version": "0.2.0", | ||
"main": "index.js", | ||
@@ -8,0 +8,0 @@ "module": "module/index.js", |
@@ -7,4 +7,4 @@ # @conform-to/dom | ||
- [createControlButton](#createControlButton) | ||
- [createFieldConfig](#createFieldConfig) | ||
- [getControlButtonProps](#getControlButtonProps) | ||
- [getFieldProps](#getFieldProps) | ||
- [getFieldElements](#getFieldElements) | ||
@@ -21,5 +21,5 @@ - [getName](#getName) | ||
### createControlButton | ||
### getControlButtonProps | ||
### createFieldConfig | ||
### getFieldProps | ||
@@ -26,0 +26,0 @@ ### getFieldElements |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
22460
8
640
2
27