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
Form.onSubmitFactory = fn => form => ev => { | |
ev.preventDefault(); | |
fn(Form.getData(form)); | |
}; |
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
Form.onChangeFactory = fn => (form, fieldKey) => updatedProps => fn({ | |
...form, | |
fields: { | |
...form.fields, | |
[fieldKey]: { | |
...form.fields[fieldKey], | |
...updatedProps, | |
}, | |
}, | |
}); |
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
Form.getProps = form => Object.assign({}, | |
...Object.entries(form) | |
.filter(([formPropKey, formProp]) => formPropKey !== 'fields') | |
.map(([formPropKey, formProp]) => ({ | |
[formPropKey]: formProp instanceof Function ? formProp(form) : formProp, | |
})), | |
{ | |
fields: Object.assign({}, ...Object.entries(form.fields).map(([fieldKey, field]) => ({ | |
[fieldKey]: Object.assign({}, ...Object.entries(field).map(([fieldPropKey, fieldProp]) => ({ | |
[fieldPropKey]: fieldProp instanceof Function ? fieldProp(form, fieldKey) : fieldProp, |
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
Form.getData = form => Object.assign({}, | |
...Object.entries(Form.getProps(form).fields) | |
.filter(([fieldKey, field]) => !field.disabled) | |
.filter(([fieldKey, field]) => | |
!['checkbox', 'radio'].includes(field.type) || field.checked | |
) | |
.map(([fieldKey, field]) => ({ [fieldKey]: field.value })), | |
); |
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
Form.fields = (globalProps = {}, fields) => Object.assign({}, | |
...Object.entries(fields).map(([fieldKey, field]) => ({ | |
[fieldKey]: { | |
...Form.Field.defaultProps, | |
...{ name: fieldKey }, | |
...globalProps, | |
...field, | |
}, | |
})), | |
); |
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
Form.fields = (globalProps = {}, fields) => Object.assign({}, | |
...Object.entries(fields).map(([fieldKey, field]) => ({ | |
[fieldKey]: { | |
...Form.Field.defaultProps, | |
...{ name: fieldKey }, | |
...globalProps, | |
...field, | |
}, | |
})), | |
); |
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
Form.customValidityFactory = (constraint, validationMessage = 'Invalid') => (...args) => ( | |
constraint(...args) ? '' : validationMessage | |
); |
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
Form.Component = ({ id, name, onSubmit, children }) => ( | |
<form | |
id={id} | |
name={name} | |
onSubmit={onSubmit} | |
> | |
{children} | |
</form> | |
); |
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
import React from 'react'; | |
import FormFieldPropTypes from './FormFieldPropTypes'; | |
import FormDefaultComponentLibrary from './FormDefaultComponentLibrary'; | |
const FormField = ({componentLibrary, ...props}) => { | |
const Component = componentLibrary[props.type]; | |
return <Component {...props} />; | |
} | |
FormField.defaultProps = { |
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
// Specs | |
// emits newListener when new listeners are added | |
// event contains event name and reference to new listener | |
// emits removeListener when existing listeners are removed | |
// | |
const DEFAULT_MAX_LISTENERS = 10; | |
const PREPEND = 'prepend'; | |
const APPEND = 'append'; |