Skip to content

Commit

Permalink
DatePicker: support literal
Browse files Browse the repository at this point in the history
  • Loading branch information
island205 committed May 21, 2019
1 parent c27788f commit fc6db12
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 1 deletion.
1 change: 1 addition & 0 deletions examples/docs/en-US/date-picker.md
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,7 @@ Pay attention to capitalization
| `A` | AM/PM | only for `format`, uppercased | AM |
| `a` | am/pm | only for `format`, lowercased | am |
| `timestamp` | JS timestamp | only for `value-format`; binding value will be a `number` | 1483326245000 |
| `[MM]` | No escape characters | To escape characters, wrap them in square brackets (e.g. [A] [MM]) | MM |

:::demo
```html
Expand Down
1 change: 1 addition & 0 deletions examples/docs/es/date-picker.md
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,7 @@ Preste atención a la capitalización
| `A` | AM/PM | solamente para `format`, mayusculas | AM |
| `a` | am/pm | solamente para `format`, minúsculas | am |
| `timestamp` | JS timestamp | solamente para `value-format`; valor vinculado debe ser un `number` | 1483326245000 |
| `[MM]` | No escape characters | To escape characters, wrap them in square brackets (e.g. [A] [MM]) | MM |

:::demo
```html
Expand Down
1 change: 1 addition & 0 deletions examples/docs/fr-FR/date-picker.md
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@ Attention à la capitalisation !
| `A` | AM/PM | uniquement pour `format`, majuscules | AM |
| `a` | am/pm | uniquement pour `format`, minuscules | am |
| `timestamp` | timestamp JS | uniquement pour `value-format`; la variable stockée sera un `number` | 1483326245000 |
| `[MM]` | No escape characters | To escape characters, wrap them in square brackets (e.g. [A] [MM]) | MM |

:::demo
```html
Expand Down
1 change: 1 addition & 0 deletions examples/docs/zh-CN/date-picker.md
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@
| `A` | AM/PM |`format` 可用,大写 | AM |
| `a` | am/pm |`format` 可用,小写 | am |
| `timestamp` | JS时间戳 |`value-format` 可用;组件绑定值为`number`类型 | 1483326245000 |
| `[MM]` | 不需要格式化字符 | 使用方括号标识不需要格式化的字符 (如 [A] [MM]) | MM |

:::demo
```html
Expand Down
15 changes: 14 additions & 1 deletion src/utils/date.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
var threeDigits = /\d{3}/;
var fourDigits = /\d{4}/;
var word = /[0-9]*['a-z\u00A0-\u05FF\u0700-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+|[\u0600-\u06FF\/]+(\s*?[\u0600-\u06FF]+){1,2}/i;
var literal = /\[([^]*?)\]/gm;
var noop = function () {
};

Expand Down Expand Up @@ -261,9 +262,21 @@

mask = fecha.masks[mask] || mask || fecha.masks['default'];

return mask.replace(token, function ($0) {
var literals = [];

// Make literals inactive by replacing them with ??
mask = mask.replace(literal, function($0, $1) {
literals.push($1);
return '??';
});
// Apply formatting rules
mask = mask.replace(token, function ($0) {
return $0 in formatFlags ? formatFlags[$0](dateObj, i18n) : $0.slice(1, $0.length - 1);
});
// Inline literal values back into the formatted value
return mask.replace(/\?\?/g, function() {
return literals.shift();
});
};

/**
Expand Down
59 changes: 59 additions & 0 deletions test/unit/specs/date-picker.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,37 @@ describe('DatePicker', () => {
}, DELAY);
});

it('with literal string', done => {
vm = createVue({
template: `
<el-date-picker
ref="compo"
v-model="value"
type="date"
value-format="dd/MM yyyy [Element]" />`,
data() {
return {
value: ''
};
}
}, true);

vm.$refs.compo.$el.querySelector('input').focus();

setTimeout(_ => {
vm.$refs.compo.picker.$el.querySelector('.el-date-table td.available').click();
setTimeout(_ => {
const today = new Date();
const yyyy = today.getFullYear();
const MM = ('0' + (today.getMonth() + 1)).slice(-2);
const dd = '01'; // first available one should be first day of month
const expectValue = `${dd}/${MM} ${yyyy} Element`;
expect(vm.value).to.equal(expectValue);
done();
}, DELAY);
}, DELAY);
});

it('accepts', done => {
vm = createVue({
template: `
Expand Down Expand Up @@ -549,6 +580,34 @@ describe('DatePicker', () => {
}, DELAY);
});

it('translates format to value-format with literal string', done => {
vm = createVue({
template: `
<el-date-picker
ref="compo"
v-model="value"
type="date"
format="[Element] yyyy-MM-dd"
value-format="dd/MM yyyy [UI]" />`,
data() {
return {
value: ''
};
}
}, true);
const input = vm.$refs.compo.$el.querySelector('input');
input.focus();
setTimeout(_ => {
input.value = 'Element 2000-10-01';
triggerEvent(input, 'input');
keyDown(input, ENTER);
setTimeout(_ => {
expect(vm.value).to.equal('01/10 2000 UI');
done();
}, DELAY);
}, DELAY);
});

it('works for daterange', done => {
vm = createVue({
template: `
Expand Down

0 comments on commit fc6db12

Please sign in to comment.