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 1 commit
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
30 changes: 30 additions & 0 deletions src/lib/props/property-sources/createFormPropertySource.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import dedent from 'ts-dedent';
import type { PropTypeInfo } from '../propDefinitions.types';
import { createFormPropertySource } from './createFormPropertySource';
import { getFullPropTypeInfo } from './createFormPropertySource.testutils';
Expand Down Expand Up @@ -166,6 +167,35 @@ describe('createFormPropertySource', () => {
});
});

describe('radio', () => {
const form = document.createElement('form');
form.innerHTML = `
<input id="onBoolean" value="single" name="maritalStatus" type="radio" checked/>
<input id="offBoolean" value="married" name="maritalStatus" type="radio" />
`;

const radioDirectValue = getFullPropTypeInfo('offBoolean', form);
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', () => {
const consoleSpy = jest.spyOn(console, 'warn');

expect(createFormPropertySource()(form).getProp(radioDirectValue.string.asInput)).toBe(
'married',
);

expect(consoleSpy)
.toHaveBeenCalledWith(dedent`The property "offBoolean" is trying to get a radio button value but the target is not the parent form, if you have multiple radio buttons with a shared name use the parent form as target
Returning the input value "married" despite the fact it could be unchecked.`);
});

it('Should return the selected radio value when using the parent form as a target', () => {
expect(createFormPropertySource()(form).getProp(radioFormDataValue.string.asForm)).toBe(
'single',
);
});
});

describe('select', () => {
it('Should return the select value if the target is a select that is not multiple', () => {
const form = document.createElement('form');
Expand Down
11 changes: 11 additions & 0 deletions src/lib/props/property-sources/createFormPropertySource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export function createFormPropertySource(): PropertySource {
const element = propInfo.source.target!;
const isCheckbox =
element.nodeName === 'INPUT' && (element as HTMLInputElement).type === 'checkbox';
const isRadio =
element.nodeName === 'INPUT' && (element as HTMLInputElement).type === 'radio';
const isForm = element.nodeName === 'FORM';
const isInput =
['INPUT', 'TEXTAREA', 'SELECT'].includes(element.nodeName) &&
Expand Down Expand Up @@ -57,6 +59,15 @@ export function createFormPropertySource(): PropertySource {

const textInput = (previousValue: unknown) => {
const input = element as HTMLInputElement;

if (isRadio && !isForm) {
ThaNarie marked this conversation as resolved.
Show resolved Hide resolved
// eslint-disable-next-line no-console
console.warn(
dedent`The property "${propInfo.name}" is trying to get a radio button value but the target is not the parent form, if you have multiple radio buttons with a shared name use the parent form as target
Returning the input value "${input.value}" despite the fact it could be unchecked.`,
);
}

if (isInput && !input.multiple) return convertSourceValue(propInfo, input.value);
return previousValue;
};
Expand Down