-
-
Notifications
You must be signed in to change notification settings - Fork 316
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #926 from centerofci/type_config
Type configuration for Text, Number, Boolean, and all mathesar types with single associated db type
- Loading branch information
Showing
42 changed files
with
1,065 additions
and
246 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
mathesar_ui/src/component-library/common/utils/__tests__/ImmutableSet.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
mathesar_ui/src/component-library/common/utils/__tests__/ruleExecutor.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import { executeRule } from '../ruleExecuter'; | ||
import type { Rule } from '../ruleExecuter'; | ||
|
||
describe('Rule executer', () => { | ||
test('Single term rule', () => { | ||
const rule: Rule = { | ||
id: 'restrictFieldSize', | ||
op: 'eq', | ||
value: false, | ||
}; | ||
|
||
expect(executeRule(rule, { restrictFieldSize: false })).toBe(true); | ||
expect(executeRule(rule, { restrictFieldSize: true })).toBe(false); | ||
expect(executeRule(rule, {})).toBe(false); | ||
}); | ||
|
||
test('Multi term rule with and', () => { | ||
const rule: Rule = { | ||
combination: 'and', | ||
terms: [ | ||
{ | ||
id: 'restrictFieldSize', | ||
op: 'eq', | ||
value: true, | ||
}, | ||
{ | ||
id: 'fieldSizeLimit', | ||
op: 'lte', | ||
value: 255, | ||
}, | ||
], | ||
}; | ||
|
||
expect( | ||
executeRule(rule, { | ||
restrictFieldSize: true, | ||
fieldSizeLimit: 200, | ||
}), | ||
).toBe(true); | ||
expect( | ||
executeRule(rule, { | ||
restrictFieldSize: false, | ||
fieldSizeLimit: 200, | ||
}), | ||
).toBe(false); | ||
expect( | ||
executeRule(rule, { | ||
restrictFieldSize: true, | ||
fieldSizeLimit: 300, | ||
}), | ||
).toBe(false); | ||
expect( | ||
executeRule(rule, { | ||
restrictFieldSize: false, | ||
fieldSizeLimit: 300, | ||
}), | ||
).toBe(false); | ||
expect(executeRule(rule, {})).toBe(false); | ||
}); | ||
|
||
test('Multi term rule with or', () => { | ||
const rule: Rule = { | ||
combination: 'or', | ||
terms: [ | ||
{ | ||
id: 'restrictFieldSize', | ||
op: 'eq', | ||
value: true, | ||
}, | ||
{ | ||
id: 'fieldSizeLimit', | ||
op: 'lte', | ||
value: 255, | ||
}, | ||
], | ||
}; | ||
|
||
expect( | ||
executeRule(rule, { | ||
restrictFieldSize: true, | ||
fieldSizeLimit: 200, | ||
}), | ||
).toBe(true); | ||
expect( | ||
executeRule(rule, { | ||
restrictFieldSize: false, | ||
fieldSizeLimit: 200, | ||
}), | ||
).toBe(true); | ||
expect( | ||
executeRule(rule, { | ||
restrictFieldSize: true, | ||
fieldSizeLimit: 300, | ||
}), | ||
).toBe(true); | ||
expect( | ||
executeRule(rule, { | ||
restrictFieldSize: false, | ||
fieldSizeLimit: 300, | ||
}), | ||
).toBe(false); | ||
expect(executeRule(rule, {})).toBe(false); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,5 +43,3 @@ export function filterTree( | |
.indexOf(searchTerm.toLowerCase()) > -1, | ||
); | ||
} | ||
|
||
export default {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// Utility Classes | ||
export { default as CancellablePromise } from './CancellablePromise'; | ||
export { default as EventHandler } from './EventHandler'; | ||
export { default as ImmutableSet } from './ImmutableSet'; | ||
|
||
// Utility Functions | ||
export * from './domUtils'; | ||
export * from './filterUtils'; | ||
export * from './formatUtils'; | ||
export * from './pauseableTweened'; | ||
export * from './ruleExecuter'; | ||
export * from './storeUtils'; |
66 changes: 66 additions & 0 deletions
66
mathesar_ui/src/component-library/common/utils/ruleExecuter.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
export type RuleTermOperator = 'eq' | 'neq' | 'lt' | 'gt' | 'lte' | 'gte'; | ||
|
||
export interface RuleTerm { | ||
id: string; | ||
op: RuleTermOperator; | ||
value: unknown; | ||
} | ||
|
||
export interface RuleTermCombination { | ||
combination: 'and' | 'or'; | ||
terms: Rule[]; | ||
} | ||
|
||
export type Rule = RuleTerm | RuleTermCombination; | ||
|
||
function combine( | ||
combination: RuleTermCombination['combination'], | ||
term1: boolean, | ||
term2: boolean, | ||
): boolean { | ||
if (combination === 'and') { | ||
return term1 && term2; | ||
} | ||
return term1 || term2; | ||
} | ||
|
||
function executureTerm( | ||
term: RuleTerm, | ||
values: Record<RuleTerm['id'], unknown>, | ||
): boolean { | ||
const termValue = values[term.id]; | ||
switch (term.op) { | ||
case 'eq': | ||
return termValue === term.value; | ||
case 'neq': | ||
return termValue !== term.value; | ||
case 'gt': | ||
return termValue > term.value; | ||
case 'gte': | ||
return termValue >= term.value; | ||
case 'lt': | ||
return termValue < term.value; | ||
case 'lte': | ||
return termValue <= term.value; | ||
default: | ||
return false; | ||
} | ||
} | ||
|
||
export function executeRule( | ||
rule: Rule, | ||
values: Record<RuleTerm['id'], unknown>, | ||
): boolean { | ||
if ('combination' in rule) { | ||
let isSuccess = rule.combination === 'and'; | ||
rule.terms.forEach((term) => { | ||
isSuccess = combine( | ||
rule.combination, | ||
isSuccess, | ||
executeRule(term, values), | ||
); | ||
}); | ||
return isSuccess; | ||
} | ||
return executureTerm(rule, values); | ||
} |
2 changes: 1 addition & 1 deletion
2
.../component-library/form-builder/Form.scss → ...ent-library/form-builder/FormBuilder.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
.form { | ||
.form-builder { | ||
.form-element + .form-element { | ||
margin-top: 0.6rem; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
...ry/form-builder/__meta__/FormStory.svelte → ...-builder/__meta__/FormBuilderStory.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 33 additions & 4 deletions
37
mathesar_ui/src/component-library/form-builder/formFactory.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,44 @@ | ||
import { writable } from 'svelte/store'; | ||
import type { FormConfiguration, FormBuildConfiguration } from './types'; | ||
import { derived, get, writable } from 'svelte/store'; | ||
import type { Readable } from 'svelte/store'; | ||
import type { | ||
FormInputDataType, | ||
FormConfiguration, | ||
FormBuildConfiguration, | ||
FormValues, | ||
} from './types'; | ||
|
||
export function makeForm( | ||
formConfig: FormConfiguration, | ||
formValues?: FormValues, | ||
): FormBuildConfiguration { | ||
const stores = {}; | ||
const stores: FormBuildConfiguration['stores'] = new Map(); | ||
Object.keys(formConfig.variables)?.forEach((key) => { | ||
stores[key] = writable(formConfig.variables[key].default); | ||
const value = | ||
typeof formValues?.[key] !== 'undefined' | ||
? formValues[key] | ||
: formConfig.variables[key].default; | ||
stores.set(key, writable(value)); | ||
}); | ||
|
||
const values: Readable<Record<string, FormInputDataType>> = derived( | ||
[...stores.values()], | ||
(storeValues) => { | ||
const valueObj = {}; | ||
[...stores.keys()].forEach((key, index) => { | ||
valueObj[key] = storeValues[index]; | ||
}); | ||
return valueObj; | ||
}, | ||
); | ||
|
||
function getValues(): ReturnType<FormBuildConfiguration['getValues']> { | ||
return get(values); | ||
} | ||
|
||
return { | ||
...formConfig, | ||
stores, | ||
values, | ||
getValues, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
export { makeForm } from './formFactory'; | ||
export { default as Form } from './Form.svelte'; | ||
export { default as FormBuilder } from './FormBuilder.svelte'; |
Oops, something went wrong.