Skip to content

Commit

Permalink
feat: introduced inputMode field prop and autoTrimValue form option
Browse files Browse the repository at this point in the history
introduced inputMode field prop and autoTrimValue form option

fix #283
  • Loading branch information
foxhound87 committed Apr 2, 2023
1 parent 8e417a3 commit 6ed5db6
Show file tree
Hide file tree
Showing 11 changed files with 47 additions and 11 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# 5.9.0 (next)

- Introduced `inputMode` Field property.
- Introduced `autoTrimValue` form option (can be also enabled on single field).
Fix: #283

# 5.8.0 (next)

- `set()` method will increase `changed` prop and trigger `onChange` Event Hook
Expand Down
1 change: 1 addition & 0 deletions src/Bindings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export default class Bindings implements BindingsInterface {
onBlur: FieldPropsEnum.onBlur,
onFocus: FieldPropsEnum.onFocus,
autoFocus: FieldPropsEnum.autoFocus,
inputMode: FieldPropsEnum.inputMode,
onKeyUp: FieldPropsEnum.onKeyUp,
onKeyDown: FieldPropsEnum.onKeyDown,
},
Expand Down
13 changes: 11 additions & 2 deletions src/Field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ const setupFieldProps = (instance: FieldInterface, props: any, data: any) =>
$hooks: props.$hooks || (data && data.hooks) || {},
$handlers: props.$handlers || (data && data.handlers) || {},
$autoFocus: props.$autoFocus || (data && data.autoFocus) || false,
$inputMode: props.$inputMode || (data && data.inputMode) || undefined,
$ref: props.$ref || (data && data.ref) || undefined,
});

Expand Down Expand Up @@ -102,6 +103,7 @@ export default class Field extends Base implements FieldInterface {
$blurred: boolean = false;
$deleted: boolean = false;
$autoFocus: boolean = false;
$inputMode: string = undefined;
$ref: any = undefined

$clearing: boolean = false;
Expand Down Expand Up @@ -159,6 +161,7 @@ export default class Field extends Base implements FieldInterface {
validationAsyncData: observable,
files: observable,
autoFocus: computed,
inputMode: computed,
ref: computed,
checkValidationErrors: computed,
checked: computed,
Expand Down Expand Up @@ -254,7 +257,7 @@ export default class Field extends Base implements FieldInterface {
set value(newVal) {
if (this.$value === newVal) return;
// handle numbers
if (this.state.options.get(OptionsEnum.autoParseNumbers, this) === true) {
if (this.state.options.get(OptionsEnum.autoParseNumbers, this)) {
if (_.isNumber(this.$initial)) {
if (
new RegExp("^-?\\d+(,\\d+)*(\\.\\d+([eE]\\d+)?)?$", "g").exec(newVal)
Expand All @@ -268,7 +271,9 @@ export default class Field extends Base implements FieldInterface {
}
}
}
// handle parse value
if (_.isString(newVal) && this.state.options.get(OptionsEnum.autoTrimValue, this)) {
newVal = newVal.trim();
}
this.$value = newVal;
this.$changed ++;
if (!this.resetting && !this.clearing) {
Expand Down Expand Up @@ -312,6 +317,10 @@ export default class Field extends Base implements FieldInterface {
return this.$autoFocus;
}

get inputMode() {
return this.$inputMode;
}

get type() {
return toJS(this.$type);
}
Expand Down
1 change: 1 addition & 0 deletions src/Options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export default class Options implements OptionsInterface {
retrieveOnlyDirtyFieldsValues: false,
retrieveOnlyEnabledFieldsValues: false,
retrieveOnlyEnabledFieldsErrors: false,
autoTrimValue: false,
autoParseNumbers: false,
removeNullishValuesInArrays: false,
preserveDeletedFieldsValues: false,
Expand Down
1 change: 1 addition & 0 deletions src/models/FieldInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export default interface FieldInterface extends BaseInterface {
validationFunctionsData: any[];
debouncedValidation: any;
autoFocus: boolean;
inputMode: string;
ref: any;
showError: boolean;
checkValidationErrors: boolean;
Expand Down
2 changes: 2 additions & 0 deletions src/models/FieldProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export enum FieldPropsEnum {
onAdd = "onAdd",
onDel = "onDel",
autoFocus = "autoFocus",
inputMode = "inputMode",
onKeyDown = "onKeyDown",
onKeyUp = 'onKeyUp',
}
Expand Down Expand Up @@ -98,5 +99,6 @@ export enum SeparatedPropsMode {
input = 'input',
output = 'output',
autoFocus = 'autoFocus',
inputMode = 'inputMode',
refs = 'refs',
}
22 changes: 13 additions & 9 deletions src/models/FormInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,27 @@ export interface FieldsDefinitions {
values?: any;
labels?: any;
placeholders?: any;
initials?: any;
defaults?: any;
initials?: any;
disabled?: any;
deleted?: any;
types?: any;
related?: any;
rules?: any;
options?: any;
extra?: any;
types?: any;
input?: any;
output?: any;
bindings?: any;
observers?: any;
interceptors?: any;
validatedWith?: any;
extra?: any;
hooks?: any;
handlers?: any;
validatedWith?: any;
validators?: any;
rules?: any;
observers?: any;
interceptors?: any;
input?: any;
output?: any;
autoFocus?: any;
inputMode?: any;
refs?: any;
}

export interface FormConfig {
Expand Down
2 changes: 2 additions & 0 deletions src/models/OptionsModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export enum OptionsEnum {
retrieveOnlyEnabledFieldsErrors = 'retrieveOnlyEnabledFieldsErrors',
removeNullishValuesInArrays = 'removeNullishValuesInArrays',
preserveDeletedFieldsValues = 'preserveDeletedFieldsValues',
autoTrimValue = 'autoTrimValue',
autoParseNumbers = 'autoParseNumbers',
validationDebounceWait = 'validationDebounceWait',
validationDebounceOptions = 'validationDebounceOptions',
Expand Down Expand Up @@ -66,6 +67,7 @@ export default interface OptionsModel {
[OptionsEnum.retrieveOnlyEnabledFieldsErrors]?: boolean;
[OptionsEnum.removeNullishValuesInArrays]?: boolean;
[OptionsEnum.preserveDeletedFieldsValues]?: boolean;
[OptionsEnum.autoTrimValue]?: boolean;
[OptionsEnum.autoParseNumbers]?: boolean;
[OptionsEnum.validationDebounceWait]?: number;
[OptionsEnum.validationDebounceOptions]?: {
Expand Down
2 changes: 2 additions & 0 deletions src/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export const props: PropsGroupsInterface = {
FieldPropsEnum.deleted,
FieldPropsEnum.disabled,
FieldPropsEnum.autoFocus,
FieldPropsEnum.inputMode,
FieldPropsEnum.ref,
],
handlers: [
Expand Down Expand Up @@ -84,6 +85,7 @@ export const props: PropsGroupsInterface = {
SeparatedPropsMode.input,
SeparatedPropsMode.output,
SeparatedPropsMode.autoFocus,
SeparatedPropsMode.inputMode,
SeparatedPropsMode.refs,
],
functions: [
Expand Down
2 changes: 2 additions & 0 deletions tests/data/forms/nested/form.v.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ const hooks = {
form.$('user.id').set('value', 'user-id');
form.$('user.name').set('value', 'user-init');
form.$('user.name').set('disabled', true);
form.$('user.name').set('autoFocus', true);
form.$('user.name').set('inputMode', "text");
form.$('user.name').set('ref', "ref");

},
Expand Down
6 changes: 6 additions & 0 deletions tests/nested.hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ describe('Check form onChange hook', () => {
it('$V user.name disabled should be true', () =>
expect($.$V.$('user.name').disabled).to.be.true);

it('$V user.name autoFocus should be true', () =>
expect($.$V.$('user.name').autoFocus).to.be.true);

it('$V user.name inputMode should be equal "text"', () =>
expect($.$V.$('user.name').inputMode).to.be.equal('text'));

it('$V user.name ref should be equal "ref"', () =>
expect($.$V.$('user.name').ref).to.be.equal("ref"));
});
Expand Down

0 comments on commit 6ed5db6

Please sign in to comment.