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

Add possibility to override Field.jsx #57

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "formol",
"version": "2.5.11",
"version": "2.5.12-alpha.2",
"description": "An opiniated react form framework.",
"main": "lib/formol.js",
"module": "src/index.js",
Expand Down
133 changes: 4 additions & 129 deletions src/Field.jsx
Original file line number Diff line number Diff line change
@@ -1,140 +1,15 @@
import React from 'react'
import { FaQuestionCircle } from 'react-icons/fa'

import { FieldBase } from './FieldBase'
import { block } from './utils'
import fieldPropsAdapter from './utils/fieldPropsAdapter'

@fieldPropsAdapter
@block
export default class Field extends React.PureComponent {
constructor(props) {
super(props)
this.element = React.createRef()
this.state = {
focus: false,
}

this.handleChange = this.handleChange.bind(this)
this.handleFocus = this.handleFocus.bind(this)
this.handleBlur = this.handleBlur.bind(this)
}

componentDidMount() {
const { register, name, validator, validityErrors } = this.props
register(name, this.element, validator, validityErrors)
}

componentWillUnmount() {
const { unregister, name } = this.props
unregister(name)
this.handleChange()
}

handleChange(value, error) {
const { name, unformatter, handleChange } = this.props
handleChange(name, unformatter(value), error)
}

handleFocus() {
this.setState({
focus: true,
})
}

handleBlur() {
const { name, value, normalizer, handleChange, handleEntered } = this.props
// Normalize data
const normalized = normalizer(value)
if (normalized !== value) {
handleChange(name, normalized)
}
this.setState({
focus: false,
})
handleEntered(name)
}

render(b) {
const {
name,
value,
type,
title,
modified,
className,
validator,
readOnly,
disabled,
unit,
extras,
formatter,
normalizer,
unformatter,
children,
classNameModifiers,
TypeField,
i18n,
error,
validityErrors,
handleChange,
handleEntered,
register,
unregister,
...props
} = this.props

const { focus } = this.state

const Label = TypeField.formolFieldLabelElement || 'label'
return (
<div
className={b.mix(className).m({
type,
name,
error: !!error,
disabled,
readOnly,
required: !!props.required,
modified,
focus,
...classNameModifiers.field,
})}
>
<Label
className={b.e('label').m(classNameModifiers.label)}
title={title}
>
{children && (
<span className={b.e('title').m(classNameModifiers.labelText)}>
{children}
{title && <FaQuestionCircle />}
</span>
)}
<TypeField
name={name}
value={value}
type={type}
disabled={disabled}
readOnly={readOnly}
i18n={i18n}
elementRef={this.element}
className={b.e('element')}
onFocus={this.handleFocus}
onBlur={this.handleBlur}
onChange={this.handleChange}
{...props}
/>
{unit && (
<div className={b.e('unit').m(classNameModifiers.unit)}>{unit}</div>
)}
{extras}
</Label>
{error && (
<div className={b.e('error-text').m(classNameModifiers.error)}>
{error}
</div>
)}
</div>
)
const { fieldComponent, ...rest } = this.props
const FieldComp = fieldComponent || FieldBase
return <FieldComp {...rest} b={b} />
}
}
136 changes: 136 additions & 0 deletions src/FieldBase.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
import React from 'react'
import { FaQuestionCircle } from 'react-icons/fa'

export class FieldBase extends React.PureComponent {
constructor(props) {
super(props)
this.element = React.createRef()
this.state = {
focus: false,
}

this.handleChange = this.handleChange.bind(this)
this.handleFocus = this.handleFocus.bind(this)
this.handleBlur = this.handleBlur.bind(this)
}

componentDidMount() {
const { register, name, validator, validityErrors } = this.props
register(name, this.element, validator, validityErrors)
}

componentWillUnmount() {
const { unregister, name } = this.props
unregister(name)
this.handleChange()
}

handleChange(value, error) {
const { name, unformatter, handleChange } = this.props
handleChange(name, unformatter(value), error)
}

handleFocus() {
this.setState({
focus: true,
})
}

handleBlur() {
const { name, value, normalizer, handleChange, handleEntered } = this.props
// Normalize data
const normalized = normalizer(value)
if (normalized !== value) {
handleChange(name, normalized)
}
this.setState({
focus: false,
})
handleEntered(name)
}

render() {
const {
b,
name,
value,
type,
title,
modified,
className,
validator,
readOnly,
disabled,
unit,
extras,
formatter,
normalizer,
unformatter,
children,
classNameModifiers,
TypeField,
i18n,
error,
validityErrors,
handleChange,
handleEntered,
register,
unregister,
...props
} = this.props

const { focus } = this.state

const Label = TypeField.formolFieldLabelElement || 'label'
return (
<div
className={b.mix(className).m({
type,
name,
error: !!error,
disabled,
readOnly,
required: !!props.required,
modified,
focus,
...classNameModifiers.field,
})}
>
<Label
className={b.e('label').m(classNameModifiers.label)}
title={title}
>
{children && (
<span className={b.e('title').m(classNameModifiers.labelText)}>
{children}
{title && <FaQuestionCircle />}
</span>
)}
<TypeField
name={name}
value={value}
type={type}
disabled={disabled}
readOnly={readOnly}
i18n={i18n}
elementRef={this.element}
className={b.e('element')}
onFocus={this.handleFocus}
onBlur={this.handleBlur}
onChange={this.handleChange}
{...props}
/>
{unit && (
<div className={b.e('unit').m(classNameModifiers.unit)}>{unit}</div>
)}
{extras}
</Label>
{error && (
<div className={b.e('error-text').m(classNameModifiers.error)}>
{error}
</div>
)}
</div>
)
}
}
6 changes: 6 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,9 @@ export {
diff,
isModified,
} from './utils/object'

export * from './FieldBase'

export { default as choicesAdapter } from './utils/choicesAdapter'
export { default as memoizedChoices } from './utils/memoizedChoices'
export { default as multipleAdapter } from './utils/multipleAdapter'
24 changes: 24 additions & 0 deletions test/formol/fields.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ describe('Formol field', () => {
.find('Field')
.children()
.first()
.children()
.first()
expect(field).toBeTruthy()
expect(field.hasClass('Formol_Field')).toBeTruthy()
expect(field.hasClass('Formol_Field--name-field-1')).toBeTruthy()
Expand All @@ -38,6 +40,28 @@ describe('Formol field', () => {
expect(input.props().type).toEqual('text')
expect(input.props().value).toEqual('')
})
it('is rendered with overriden FieldBase fieldComponent', () => {
const wrapper = mount(
<Formol>
<Field
fieldComponent={() => {
return <div>TEST</div>
}}
/>
</Formol>
)
expect(wrapper.find('form')).toBeTruthy()

const field = wrapper
.find('Field')
.children()
.first()
.children()
.first()
expect(field).toBeTruthy()
expect(field.type()).toEqual('div')
expect(field.props().children).toEqual('TEST')
})
it('respects the name attribute', () => {
const wrapper = mount(
<Formol>
Expand Down