Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Input specific propType source #75

Merged
merged 53 commits into from
Sep 26, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
501eebd
create the createFormPropertySource and add it to the global property…
jspolancor Mar 9, 2022
edd65fb
Create test cases for the createFormPropertySource function
jspolancor Mar 9, 2022
c0c9adc
Update createFormPropertySource() adding 'checkbox' and 'multiple' cases
jspolancor Mar 9, 2022
70ef62f
Update createFormPropertySource() tests cases
jspolancor Mar 9, 2022
ca69777
Updates usage of the propInfo sourceName in the property sources
jspolancor Mar 10, 2022
39d16e3
Add stories for the 'form' propType.source
jspolancor Mar 10, 2022
acf4d8e
Update checkbox behavior in createFormPropertySource()
jspolancor Mar 10, 2022
76c976c
Update stories for createFormPropertySource()
jspolancor Mar 10, 2022
63f797e
Remove the file function from createFormPropertySource
jspolancor Mar 23, 2022
1f8d419
Remove lodash dependency from createFormPropertySource
jspolancor Mar 23, 2022
b8e1c50
Fix HtmlElement casting in the tests for createFormPropertySource
jspolancor Mar 23, 2022
1d2179f
Update createFormPropertySource tests to have an isolated testing for…
jspolancor Mar 23, 2022
cca2c88
Add docs for the 'form' propType source
jspolancor Mar 23, 2022
a6ede3f
Improve form styling
ThaNarie Mar 24, 2022
33ab7b4
Update checkbox test for createFormPropertySource()
jspolancor Mar 29, 2022
2a7c1ba
Update array of checkbox values test for createFormPropertySource()
jspolancor Mar 29, 2022
c2f93d9
Add play function to the FormProps story
jspolancor Mar 29, 2022
8140770
Remove the formData ref from the FormProps tests
jspolancor Mar 29, 2022
faecd70
Add a radio button to the FormProps story
jspolancor Mar 29, 2022
767a51c
Make the 'name' property for a propType source optional
jspolancor Apr 18, 2022
ecfad28
Add json extraction tests to the FormProps story
jspolancor Apr 20, 2022
ca98f43
Add an unchecked radio to the formProp stories
jspolancor Apr 20, 2022
86ed598
Fix linting issues for createFormPropertySource()
jspolancor Apr 20, 2022
3ff8250
Refactor the formdata extraction for createFormPropertySource()
jspolancor Apr 20, 2022
11b50e7
Refactor createFormPropertySource() removing the formData boolean
jspolancor Apr 20, 2022
39ca24b
update docs for the 'form' prop type
jspolancor Apr 21, 2022
bc98a1e
Refactor the createFormPropertysource removing the validations for a …
jspolancor Apr 26, 2022
4def895
Add the formData boolean to the SourceOptionForm type
jspolancor Apr 26, 2022
ddf1665
Update text inputs tests for createFormPropertySource()
jspolancor Apr 26, 2022
09b2ef8
Add test for data transformations for createFormPropertySource()
jspolancor Apr 26, 2022
8baf1c2
Update checkboxes and select tests for createFormPropertySource()
jspolancor Apr 26, 2022
2e04b21
Update multiselect tests in createFormPropertySource()
jspolancor Apr 26, 2022
bb01e49
Update form target tests for createFormPropertySource()
jspolancor Apr 26, 2022
341bcf5
Add jsdocs to the createFormPropertySource test utils
jspolancor Apr 26, 2022
76bac6c
Update docs for the 'form' source type
jspolancor Apr 26, 2022
f46deea
Add a validation to createFormPropertySource() to check if the source…
jspolancor May 4, 2022
ba2fb0f
Update types for the createFormPropertySource() test utils
jspolancor May 4, 2022
18b9c54
Update docs for form propType source
jspolancor May 4, 2022
af15b23
Reorder createFormPropertySource() code
jspolancor May 4, 2022
34a1d96
Reorder the FormData extraction code in createFormPropertySource()
jspolancor May 5, 2022
2eafaf9
Adds radio buttons tests for createFormPropertySource()
jspolancor May 17, 2022
587407f
Adds unreleased section to the changelog
jspolancor May 17, 2022
9ae0269
Creates the getPropTypeInfo() test util function
jspolancor May 18, 2022
e554f74
Merge branch 'next' into feature/input-prop-type-source
jspolancor May 18, 2022
81a35d4
Remove branch name from the unreleased section in changelog
jspolancor May 24, 2022
7f0fea7
Remove an unnecesary condition from createFormPropertySource()
jspolancor May 24, 2022
5477836
Merge branch 'feature/input-prop-type-source' of github.com:mubanjs/m…
jspolancor May 24, 2022
363a133
Remove id's from createFormPopertySource() test utils
jspolancor May 25, 2022
f8dc07b
Update array value checking for createFormPropertySource()
jspolancor May 25, 2022
a684c1f
Removes an unnecessary Array check in createFormPropertySource()
jspolancor May 31, 2022
d939976
Fix return value in createFormPropertySource() for empty values
jspolancor May 31, 2022
be934fc
Update CHANGELOG.md
ThaNarie Sep 20, 2022
a3d7bd2
Merge remote-tracking branch 'origin/next' into feature/input-prop-ty…
ThaNarie Sep 26, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 24 additions & 22 deletions src/lib/props/property-sources/createFormPropertySource.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ describe('createFormPropertySource', () => {
describe('Text inputs', () => {
const form = document.createElement('form');
form.innerHTML = `
<input id="email" type="email" name="email" value="[email protected]"/>
<input id="password" type="password" name="password" value="123456"/>
<textarea id="description" name="description">lorem ipsum</textarea>
<input type="email" name="email" value="[email protected]"/>
<input type="password" name="password" value="123456"/>
<textarea name="description">lorem ipsum</textarea>
`;
const email = getFullPropTypeInfo('email', form);
const password = getFullPropTypeInfo('password', form);
Expand All @@ -78,13 +78,13 @@ describe('createFormPropertySource', () => {
describe('Non String text inputs', () => {
const form = document.createElement('form');
form.innerHTML = `
<input type="text" name="age" id="age" value="32"/>
<input type="text" name="height" id="height" value="1.70"/>
<input type="text" name="terms" id="terms" value="true"/>
<input type="text" name="kids" id="kids" value="false"/>
<input type="text" name="birthday" id="birthday" value="2022/06/06"/>
<input type="text" name="pets" id="pets" value='["Armin", "Trico"]'/>
<input type="text" name="area" id="area" value='{ "zip": "110010", "latlong": 1293847}'/>
<input type="text" name="age" value="32"/>
<input type="text" name="height" value="1.70"/>
<input type="text" name="terms" value="true"/>
<input type="text" name="kids" value="false"/>
<input type="text" name="birthday" value="2022/06/06"/>
<input type="text" name="pets" value='["Armin", "Trico"]'/>
<input type="text" name="area" value='{ "zip": "110010", "latlong": 1293847}'/>
`;

const age = getFullPropTypeInfo('age', form);
Expand Down Expand Up @@ -121,12 +121,12 @@ describe('createFormPropertySource', () => {
describe('checkbox', () => {
const form = document.createElement('form');
form.innerHTML = `
<input id="onBoolean" name="onBoolean" type="checkbox" checked/>
<input id="offBoolean" name="offBoolean" type="checkbox" />
<input id="onString" name="onString" type="checkbox" checked/>
<input id="offString" name="offString" type="checkbox" />
<input id="onStringValue" name="onStringValue" type="checkbox" value="foo" checked />
<input id="offStringValue" name="offStringValue" type="checkbox" value="foo" />
<input name="onBoolean" type="checkbox" checked/>
<input name="offBoolean" type="checkbox" />
<input name="onString" type="checkbox" checked/>
<input name="offString" type="checkbox" />
<input name="onStringValue" type="checkbox" value="foo" checked />
<input name="offStringValue" type="checkbox" value="foo" />
`;

const onBoolean = getFullPropTypeInfo('onBoolean', form);
Expand Down Expand Up @@ -170,11 +170,13 @@ describe('createFormPropertySource', () => {
describe('radio', () => {
const form = document.createElement('form');
form.innerHTML = `
<input id="onBoolean" value="single" name="maritalStatus" type="radio" checked/>
<input value="single" name="maritalStatus" type="radio" checked/>
<input id="offBoolean" value="married" name="maritalStatus" type="radio" />
`;

const radioDirectValue = getFullPropTypeInfo('offBoolean', form);
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const offBooleanElement = form.querySelector('#offBoolean')!;
const radioDirectValue = getFullPropTypeInfo('offBoolean', offBooleanElement);
const radioFormDataValue = getFullPropTypeInfo('onBoolean', form, 'maritalStatus');
ThaNarie marked this conversation as resolved.
Show resolved Hide resolved

it('Should show a warning message and return the input value when trying to get a radio button value directly', () => {
Expand All @@ -200,11 +202,11 @@ describe('createFormPropertySource', () => {
it('Should return the select value if the target is a select that is not multiple', () => {
const form = document.createElement('form');
form.innerHTML = `
<select id="preference" name="preference">
<select name="preference">
<option value="foo" selected>foo</option>
<option value="bar">bar</option>
</select>
<select id="preferenceBoolean" name="preferenceBoolean">
<select name="preferenceBoolean">
<option value="true" selected>foo</option>
<option value="false">bar</option>
</select>
Expand All @@ -221,7 +223,7 @@ describe('createFormPropertySource', () => {
it('Should return an array of strings if the target is a multiselect', () => {
const form = document.createElement('form');
form.innerHTML = `
<select id="candidates" name="candidates" multiple>
<select name="candidates" multiple>
<option value="foo" selected>foo</option>
<option value="bar" selected>bar</option>
</select>
Expand Down Expand Up @@ -259,7 +261,7 @@ describe('createFormPropertySource', () => {
it('Should return FormData object when using type "Object" and formData: true', () => {
ThaNarie marked this conversation as resolved.
Show resolved Hide resolved
jspolancor marked this conversation as resolved.
Show resolved Hide resolved
const form = document.createElement('form');
form.innerHTML = `
<input id="email" type="email" name="email" value="[email protected]"/>
<input type="email" name="email" value="[email protected]"/>
`;
const validForm = getFullPropTypeInfo('validForm', form, undefined, true);
const formData = createFormPropertySource()(form).getProp(validForm.object.asForm);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type FullPropTypeInfo = {

function getMixedPropTypeInfo(
ThaNarie marked this conversation as resolved.
Show resolved Hide resolved
name: string,
form: HTMLFormElement,
targetElement: HTMLElement,
type: PropTypeDefinition['type'],
formData?: boolean,
sourceName: string = '',
Expand All @@ -22,15 +22,24 @@ function getMixedPropTypeInfo(
type,
source: {
name: sourceName,
target: form,
target: targetElement,
type: 'form',
formData,
},
});

const inputTarget: PropTypeInfo = { ...formTarget };
const inputField = form.querySelector<HTMLElement>(`#${formTarget.source.name}`);
if (inputField) inputTarget.source.target = inputField;
const inputTarget: PropTypeInfo = getPropTypeInfo({
name,
type,
source: {
name: sourceName,
target:
targetElement.querySelector<HTMLElement>(`[name='${formTarget.source.name}']`) ||
targetElement,
type: 'form',
formData,
},
});

return {
asForm: formTarget,
Expand All @@ -44,23 +53,25 @@ function getMixedPropTypeInfo(
*
* Useful for testing value extraction for a form and it's child inputs
* @param {string} name prop info name
* @param {HTMLFormElement} html target element
* @param {Element} targetElement target element
* @param {string} sourceName to be used as source.name
* @param {boolean } formData to be used as source.formData
* @returns {FullPropTypeInfo}
*/
export function getFullPropTypeInfo(
name: string,
form: HTMLFormElement,
targetElement: Element,
sourceName?: string,
formData?: boolean,
): FullPropTypeInfo {
const target = targetElement as HTMLElement;

return {
number: getMixedPropTypeInfo(name, form, Number, formData, sourceName),
string: getMixedPropTypeInfo(name, form, String, formData, sourceName),
boolean: getMixedPropTypeInfo(name, form, Boolean, formData, sourceName),
date: getMixedPropTypeInfo(name, form, Date, formData, sourceName),
array: getMixedPropTypeInfo(name, form, Array, formData, sourceName),
object: getMixedPropTypeInfo(name, form, Object, formData, sourceName),
number: getMixedPropTypeInfo(name, target, Number, formData, sourceName),
string: getMixedPropTypeInfo(name, target, String, formData, sourceName),
boolean: getMixedPropTypeInfo(name, target, Boolean, formData, sourceName),
date: getMixedPropTypeInfo(name, target, Date, formData, sourceName),
array: getMixedPropTypeInfo(name, target, Array, formData, sourceName),
object: getMixedPropTypeInfo(name, target, Object, formData, sourceName),
};
}
15 changes: 10 additions & 5 deletions src/lib/props/property-sources/createFormPropertySource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,19 @@ export function createFormPropertySource(): PropertySource {
}

const childInputValues = formData.getAll(propInfo.source.name || '');
let valueIsStringifiedArray = false;

if (propInfo.type !== Object) {
if (propInfo.type === Array) return childInputValues;
if (childInputValues.length === 0) return undefined;
return convertSourceValue(propInfo, (childInputValues[0] as string) || '');
if (childInputValues.length > 0 && propInfo.type === Array) {
ThaNarie marked this conversation as resolved.
Show resolved Hide resolved
try {
const parsedValue = JSON.parse(childInputValues[0] as string);
if (Array.isArray(parsedValue)) valueIsStringifiedArray = true;
// eslint-disable-next-line no-empty
} catch {}
}

return previousValue;
if (propInfo.type === Array && !valueIsStringifiedArray) return childInputValues;
ThaNarie marked this conversation as resolved.
Show resolved Hide resolved
if (childInputValues.length === 0) return previousValue;
ThaNarie marked this conversation as resolved.
Show resolved Hide resolved
return convertSourceValue(propInfo, (childInputValues[0] as string) || '');
};

const textInput = (previousValue: unknown) => {
Expand Down