Skip to content
This repository has been archived by the owner on Oct 27, 2022. It is now read-only.

Commit

Permalink
Support onValuePreUpdate in DatePicker
Browse files Browse the repository at this point in the history
  • Loading branch information
alexkuz committed Sep 18, 2016
1 parent c601a3f commit fc74a2d
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 3 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
0.5.2 / 2016-09-18
------------------
* `DatePicker` value can be patched now with `onValuePreUpdate` callback.

0.5.1 / 2016-09-18
------------------
* Fix server rendering, tests added

0.5.0 / 2016-09-18
------------------
* IE11 fixes
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ If `<input>` element wasn't provided, component tries to find node automatically
* **`emptyChar`** *string* - Character used as an empty symbol (`' '` by default)
* **`placeholder`** *string* - If set, it is shown when `unmaskedValue` is empty
* **`onUnmaskedValueChange`** *function(text)* - Fires when value is changed, providing unmasked value
* **`onValuePreUpdate`** *function* - Optional callback to update value before it is parsed by `Mask`

## DatePicker

Expand Down Expand Up @@ -231,6 +232,12 @@ If `<input>` element wasn't provided, component tries to find node automatically
* **`onChange`** *function(date)* - Fires when date is selected, providing [moment.js](http://momentjs.com/) object
* **`getInputElement`** *function* - Optional callback that provides `<input>` DOM element
* **`registerInput`** *function* - Registers `<input>` DOM element
* **`onValuePreUpdate`** *function* - Optional callback to update value before it is parsed by `DatePicker`. In this example, it parses inserted timestamp:
```js
onValuePreUpdate={v => parseInt(v, 10) > 1e8 ?
moment(parseInt(v, 10)).format('ddd DD/MM/YYYY') : v
}
```

## Combobox

Expand Down
2 changes: 2 additions & 0 deletions __tests__/DatePicker.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ describe('DatePicker', () => {
const tree = renderer.create(
<DatePicker
value={now.format('ddd DD/MM/YYYY')}
placeholder={now.format('ddd DD/MM/YYYY')}
>
{props => <input {...props} />}
</DatePicker>
Expand All @@ -21,6 +22,7 @@ describe('DatePicker', () => {
const tree = renderer.create(
<DatePicker
value={now.format('ddd DD/MM/YYYY')}
placeholder={now.format('ddd DD/MM/YYYY')}
>
<input />
</DatePicker>
Expand Down
4 changes: 2 additions & 2 deletions __tests__/__snapshots__/DatePicker.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ exports[`DatePicker renders correctly with element child 1`] = `
onKeyDown={[Function anonymous]}
onMouseEnter={[Function anonymous]}
onMouseLeave={[Function anonymous]}
placeholder="Sun 18/09/2016"
placeholder="Thu 01/01/1970"
style={
Object {
"paddingRight": "15px"
Expand Down Expand Up @@ -72,7 +72,7 @@ exports[`DatePicker renders correctly with function child 1`] = `
onKeyDown={[Function anonymous]}
onMouseEnter={[Function anonymous]}
onMouseLeave={[Function anonymous]}
placeholder="Sun 18/09/2016"
placeholder="Thu 01/01/1970"
style={
Object {
"paddingRight": "15px"
Expand Down
7 changes: 7 additions & 0 deletions demo/src/js/DemoApp.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,9 @@ const ValueInput9 = pure(({ value, onChange }) =>
<DatePicker
value={moment(value || undefined).format('ddd DD/MM/YYYY')}
onChange={onChange}
onValuePreUpdate={v => parseInt(v, 10) > 1e6 ?
moment(parseInt(v, 10)).format('ddd DD/MM/YYYY') : v
}
>
{(inputProps, otherProps, registerInput) =>
<FormControl
Expand All @@ -526,6 +529,10 @@ const code9 = `
<DatePicker
value={moment(value || undefined).format('ddd DD/MM/YYYY')}
onChange={onChange}
// this callback will parse inserted timestamp
onValuePreUpdate={v => parseInt(v, 10) > 1e8 ?
moment(parseInt(v, 10)).format('ddd DD/MM/YYYY') : v
}
>
{(inputProps, { registerInput }) =>
<FormControl
Expand Down
5 changes: 4 additions & 1 deletion src/DatePicker.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export default class DatePicker extends PureComponent {
}

render() {
const { children, placeholder, registerInput, getInputElement } = this.props;
const { children, placeholder, registerInput, getInputElement, onValuePreUpdate } = this.props;

const child = (maskProps, otherProps, registerInput) =>
(typeof children === 'function') ?
Expand Down Expand Up @@ -127,6 +127,9 @@ export default class DatePicker extends PureComponent {
}

handleValuePreUpdate = value => {
if (this.props.onValuePreUpdate) {
value = this.props.onValuePreUpdate(value);
}
const localeData = moment.localeData(this.props.locale);
const days = localeData._weekdaysShort;

Expand Down

0 comments on commit fc74a2d

Please sign in to comment.