add validation to one input
not sure this is the best approach
This commit is contained in:
@ -7,19 +7,57 @@ import {
|
||||
Well
|
||||
} from 'react-bootstrap';
|
||||
|
||||
const defaults = {
|
||||
'string': {
|
||||
value: '',
|
||||
valid: false,
|
||||
pristine: true
|
||||
}
|
||||
};
|
||||
|
||||
function defaultValue(type) {
|
||||
return defaults[type];
|
||||
}
|
||||
|
||||
function validatePosition(value) {
|
||||
if (!value && typeof value !== 'string') {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
export default contain({
|
||||
actions: 'jobActions',
|
||||
store: 'jobsStore',
|
||||
map({ form = {} }) {
|
||||
return form;
|
||||
const {
|
||||
position = defaultValue('string'),
|
||||
location = defaultValue('string'),
|
||||
description = defaultValue('string')
|
||||
} = form;
|
||||
return {
|
||||
position,
|
||||
location,
|
||||
description
|
||||
};
|
||||
}
|
||||
},
|
||||
React.createClass({
|
||||
displayName: 'NewJob',
|
||||
|
||||
propTypes: {
|
||||
jobActions: PropTypes.object
|
||||
jobActions: PropTypes.object,
|
||||
position: PropTypes.object,
|
||||
location: PropTypes.object,
|
||||
description: PropTypes.object
|
||||
},
|
||||
|
||||
render() {
|
||||
const {
|
||||
jobActions,
|
||||
position
|
||||
} = this.props;
|
||||
|
||||
return (
|
||||
<div>
|
||||
<Row>
|
||||
@ -28,10 +66,23 @@ export default contain({
|
||||
<h1>Create You Job Post</h1>
|
||||
<form className='form-horizontal'>
|
||||
<Input
|
||||
bsStyle={
|
||||
!position.valid && !position.pristine ?
|
||||
'error' :
|
||||
null
|
||||
}
|
||||
label='Position'
|
||||
labelClassName='col-xs-2'
|
||||
onChange={ ({ target: { value } }) => {
|
||||
jobActions.handleForm({
|
||||
name: 'position',
|
||||
value,
|
||||
validator: validatePosition
|
||||
});
|
||||
}}
|
||||
placeholder='Position'
|
||||
type='text'
|
||||
value={ position.value }
|
||||
wrapperClassName='col-xs-10' />
|
||||
<Input
|
||||
label='Location'
|
||||
|
@ -2,6 +2,7 @@ import { Actions } from 'thundercats';
|
||||
import debugFactory from 'debug';
|
||||
|
||||
const debug = debugFactory('freecc:jobs:actions');
|
||||
const assign = Object.assign;
|
||||
|
||||
export default Actions({
|
||||
setJobs: null,
|
||||
@ -23,7 +24,7 @@ export default Actions({
|
||||
|
||||
// if no job found this will be null which is a op noop
|
||||
return foundJob ?
|
||||
Object.assign({}, oldState, { currentJob: foundJob }) :
|
||||
assign({}, oldState, { currentJob: foundJob }) :
|
||||
null;
|
||||
};
|
||||
},
|
||||
@ -37,6 +38,38 @@ export default Actions({
|
||||
},
|
||||
closeModal() {
|
||||
return { showModal: false };
|
||||
},
|
||||
handleForm({ name, value, validator = () => {} }) {
|
||||
if (!name) {
|
||||
// operation noop
|
||||
return { replace: null };
|
||||
}
|
||||
if (!validator(value)) {
|
||||
return {
|
||||
transform(oldState) {
|
||||
const { oldForm } = oldState;
|
||||
const newState = assign({}, oldState);
|
||||
newState.form = assign(
|
||||
{},
|
||||
oldForm,
|
||||
{ [name]: { value, valid: false, pristine: false }}
|
||||
);
|
||||
return newState;
|
||||
}
|
||||
};
|
||||
}
|
||||
return {
|
||||
transform(oldState) {
|
||||
const { oldForm } = oldState;
|
||||
const newState = assign({}, oldState);
|
||||
newState.form = assign(
|
||||
{},
|
||||
oldForm,
|
||||
{ [name]: { value, valid: true, pristine: false }}
|
||||
);
|
||||
return newState;
|
||||
}
|
||||
};
|
||||
}
|
||||
})
|
||||
.refs({ displayName: 'JobActions' })
|
||||
|
@ -14,12 +14,15 @@ export default Store({ showModal: false })
|
||||
findJob,
|
||||
setError,
|
||||
openModal,
|
||||
closeModal
|
||||
closeModal,
|
||||
handleForm
|
||||
} = cat.getActions('JobActions');
|
||||
const register = createRegistrar(jobsStore);
|
||||
register(setter(setJobs));
|
||||
register(transformer(findJob));
|
||||
register(setter(setError));
|
||||
register(setter(openModal));
|
||||
register(setter(closeModal));
|
||||
|
||||
register(transformer(findJob));
|
||||
register(handleForm);
|
||||
});
|
||||
|
Reference in New Issue
Block a user