Skip to content
This repository was archived by the owner on Aug 23, 2022. It is now read-only.

Commit d784058

Browse files
committed
Adding formatter property to Field + unit tests
1 parent ca1a252 commit d784058

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

src/components/field-component.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ const controlPropsMap = {
5959
value: props.updateOn === 'change'
6060
&& !props.defaultValue
6161
&& !props.hasOwnProperty('value')
62-
? props.modelValue
62+
? props.formatter(props.modelValue)
6363
: props.value,
6464
name: props.model,
6565
}),
@@ -195,6 +195,7 @@ function createFieldClass(customControlPropsMap = {}, defaultProps = {}) {
195195
PropTypes.string,
196196
]),
197197
parser: PropTypes.func,
198+
formatter: PropTypes.func,
198199
updateOn: PropTypes.oneOf([
199200
'change',
200201
'blur',
@@ -221,6 +222,7 @@ function createFieldClass(customControlPropsMap = {}, defaultProps = {}) {
221222
validateOn: 'change',
222223
asyncValidateOn: 'blur',
223224
parser: identity,
225+
formatter: identity,
224226
changeAction: change,
225227
...defaultProps,
226228
};

src/utils/arrays-equal.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export default function arraysEqual(firstArray, secondArray) {
2+
return firstArray && secondArray
3+
&& firstArray.length === secondArray.length
4+
&& firstArray.every((item, index) => item === secondArray[index]);
5+
}

test/field-component-spec.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1524,4 +1524,35 @@ describe('<Field /> component', () => {
15241524

15251525
assert.equal(input.value, 'changed');
15261526
});
1527+
1528+
describe('formatter property', () => {
1529+
it('should format a raw value for text inputs', () => {
1530+
const store = applyMiddleware(thunk)(createStore)(combineReducers({
1531+
test: modelReducer('test', { foo: '' }),
1532+
}));
1533+
1534+
const field = TestUtils.renderIntoDocument(
1535+
<Provider store={store}>
1536+
<Field
1537+
model="test.foo"
1538+
formatter={(val) => val + '!!!'}
1539+
>
1540+
<input />
1541+
</Field>
1542+
</Provider>
1543+
);
1544+
1545+
const input = TestUtils.findRenderedDOMComponentWithTag(field, 'input');
1546+
1547+
input.value = 'testing';
1548+
1549+
TestUtils.Simulate.change(input);
1550+
1551+
assert.equal(input.value, 'testing!!!',
1552+
'should format the view value');
1553+
1554+
assert.equal(store.getState().test.foo, 'testing',
1555+
'should not alter the model');
1556+
});
1557+
});
15271558
});

0 commit comments

Comments
 (0)