Skip to content

Commit da11089

Browse files
author
cjmafei
committed
inputnumber
1 parent 3259edc commit da11089

3 files changed

Lines changed: 90 additions & 90 deletions

File tree

src/components/input-number/__test__/index.test.js

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable max-classes-per-file */
12
import React, { Component } from 'react';
23
import { render, mount } from 'enzyme';
34
import mountTest from '@tests/shared/mountTest';
@@ -39,43 +40,43 @@ describe('InputNumber', () => {
3940
it('should have default value', () => {
4041
const wrapper = mount(<InputNumber defaultValue={3} />);
4142

42-
expect(wrapper.state('currentValue')).toEqual(3);
43+
expect(wrapper.state('currentValue')).toEqual('3');
4344
});
4445

4546
it('should use value when defaultValue and value are exits', () => {
4647
const wrapper = mount(<InputNumber defaultValue={1} value={5} />);
47-
expect(wrapper.state('currentValue')).toEqual(5);
48+
expect(wrapper.state('currentValue')).toEqual('5');
4849
});
4950

5051
it('should currentValue equal min when value less than min ', () => {
5152
const wrapper = mount(<InputNumber min={10} value={5} />);
5253

53-
expect(wrapper.state('currentValue')).toEqual(10);
54+
expect(wrapper.state('currentValue')).toEqual('10');
5455
expect(wrapper.state('upButtonEnabled')).toBeTruthy();
5556
expect(wrapper.state('downButtonEnabled')).toBeFalsy();
5657

5758
wrapper.find(`.${prefix}-handler-down`).simulate('click');
5859
wrapper.find(`.${prefix}-handler-up`).simulate('click');
5960

60-
expect(wrapper.state('currentValue')).toEqual(11);
61+
expect(wrapper.state('currentValue')).toEqual('11');
6162
});
6263

6364
it('should currentValue equal max when value more than max', () => {
6465
const wrapper = mount(<InputNumber max={10} value={20} />);
6566

66-
expect(wrapper.state('currentValue')).toEqual(10);
67+
expect(wrapper.state('currentValue')).toEqual('10');
6768
expect(wrapper.state('upButtonEnabled')).toBeFalsy();
6869
expect(wrapper.state('downButtonEnabled')).toBeTruthy();
6970

7071
wrapper.find(`.${prefix}-handler-up`).simulate('click');
7172
wrapper.find(`.${prefix}-handler-down`).simulate('click');
7273

73-
expect(wrapper.state('currentValue')).toEqual(9);
74+
expect(wrapper.state('currentValue')).toEqual('9');
7475
});
7576

7677
it('should currentValue equla value when value between min and max', () => {
7778
const wrapper = mount(<InputNumber min={1} max={10} value={5} />);
78-
expect(wrapper.state('currentValue')).toEqual(5);
79+
expect(wrapper.state('currentValue')).toEqual('5');
7980
expect(wrapper.state('upButtonEnabled')).toBeTruthy();
8081
expect(wrapper.state('downButtonEnabled')).toBeTruthy();
8182
});
@@ -85,15 +86,15 @@ describe('InputNumber', () => {
8586
<InputNumber min={1} max={10} defaultValue={5} step={2} />,
8687
);
8788
wrapper.find(`.${prefix}-handler-up`).simulate('click');
88-
expect(wrapper.state('currentValue')).toEqual(7);
89+
expect(wrapper.state('currentValue')).toEqual('7');
8990
});
9091

9192
it('should update currentValue minus step on click down icon', () => {
9293
const wrapper = mount(
9394
<InputNumber min={1} max={10} defaultValue={5} step={10} />,
9495
);
9596
wrapper.find(`.${prefix}-handler-down`).simulate('click');
96-
expect(wrapper.state('currentValue')).toEqual(1);
97+
expect(wrapper.state('currentValue')).toEqual('1');
9798
});
9899

99100
it('should change function not trigger when disabled', () => {
@@ -107,7 +108,7 @@ describe('InputNumber', () => {
107108
it('should set currentValue to zere when defaultValue is blank', () => {
108109
const wrapper = mount(<InputNumber defaultValue={1.34} step={0.001} />);
109110
wrapper.find(`.${prefix}-handler-down`).simulate('click');
110-
expect(wrapper.state('currentValue')).toEqual(1.339);
111+
expect(wrapper.state('currentValue')).toEqual('1.339');
111112
});
112113

113114
it('should update correctly on change no min', () => {
@@ -206,8 +207,8 @@ describe('InputNumber', () => {
206207
expect(onKeyDown).toHaveBeenCalled();
207208
});
208209

209-
it('should render correctly with precison', () => {
210+
it('should render correctly with precision', () => {
210211
const wrapper = mount(<InputNumber defaultValue={1} precision={2} />);
211-
expect(wrapper.state('currentValue')).toEqual(1.0);
212+
expect(wrapper.state('currentValue')).toEqual('1.00');
212213
});
213214
});
Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
export function isInvalid(value) {
2-
return Number.isNaN(Number(value)) || value === null || value.toString().trim() === '';
2+
return Number.isNaN(Number(value)) || value === null || value.toString().trim() === '';
33
}
44

55
function fixedPrecision(value, precision) {
6-
return value.toFixed(Math.abs(parseInt(precision, 10)));
6+
return value.toFixed(Math.abs(parseInt(precision, 10)));
77
}
88

99
/**
@@ -17,21 +17,21 @@ function fixedPrecision(value, precision) {
1717
* @returns
1818
*/
1919
export function getCurrentValue(value, min, max, precision = -1) {
20-
if (isInvalid(value)) {
21-
return '';
22-
}
23-
const val = precision >= 0 ? fixedPrecision(Number(value), precision) : Number(value);
24-
if (val > max) {
25-
return fixedPrecision(max, precision);
26-
}
27-
if (val < min) {
28-
return fixedPrecision(min, precision);
29-
}
30-
return val;
20+
if (isInvalid(value)) {
21+
return '';
22+
}
23+
const val = precision >= 0 ? fixedPrecision(Number(value), precision) : Number(value);
24+
if (val > max) {
25+
return fixedPrecision(max, precision);
26+
}
27+
if (val < min) {
28+
return fixedPrecision(min, precision);
29+
}
30+
return val;
3131
}
3232

3333
function convertEmptyString2Zero(value) {
34-
return String(value).indexOf('.') >= 0 ? String(value).split('.')[1].length : 0;
34+
return String(value).indexOf('.') >= 0 ? String(value).split('.')[1].length : 0;
3535
}
3636

3737
/**
@@ -44,38 +44,38 @@ function convertEmptyString2Zero(value) {
4444
* @returns
4545
*/
4646
export function fixDoubleOperation(n1, n2) {
47-
const l1 = convertEmptyString2Zero(n1);
48-
const l2 = convertEmptyString2Zero(n2);
49-
const displacement = 10 ** Math.max(l1, l2);
50-
const _n1 = Number((n1 * displacement).toFixed());
51-
const _n2 = Number((n2 * displacement).toFixed());
47+
const l1 = convertEmptyString2Zero(n1);
48+
const l2 = convertEmptyString2Zero(n2);
49+
const displacement = 10 ** Math.max(l1, l2);
50+
const _n1 = Number((n1 * displacement).toFixed());
51+
const _n2 = Number((n2 * displacement).toFixed());
5252

53-
return (_n1 + _n2) / displacement;
53+
return (_n1 + _n2) / displacement;
5454
}
5555

5656
export function getCurrentPrecision(value, precision, step) {
57-
// 精度判断
58-
let _precision;
59-
if (precision === undefined || precision === null) {
60-
const valuePrecision = Number.isInteger(value) ? 0 : convertEmptyString2Zero(value);
61-
const stepPrecision = Number.isInteger(step) ? 0 : convertEmptyString2Zero(step);
62-
_precision = valuePrecision >= stepPrecision ? valuePrecision : stepPrecision;
63-
} else {
64-
_precision = parseInt(precision, 10);
65-
}
57+
// 精度判断
58+
let _precision;
59+
if (precision === undefined || precision === null) {
60+
const valuePrecision = Number.isInteger(value) ? 0 : convertEmptyString2Zero(value);
61+
const stepPrecision = Number.isInteger(step) ? 0 : convertEmptyString2Zero(step);
62+
_precision = valuePrecision >= stepPrecision ? valuePrecision : stepPrecision;
63+
} else {
64+
_precision = parseInt(precision, 10);
65+
}
6666

67-
return _precision;
67+
return _precision;
6868
}
6969

7070
export function isInvalidNumber(num) {
71-
// .00 will disappear in toNumber
72-
return num === '' || num === null || (num.indexOf('.') >= 0 && num.charAt(num.length - 1) === '0');
71+
// .00 will disappear in toNumber
72+
return num === '' || num === null || (num.indexOf('.') >= 0 && num.charAt(num.length - 1) === '0');
7373
}
7474

7575
export function isNotCompleteNumber(num) {
76-
// - & x. Is not complete number
77-
return (
78-
// eslint-disable-next-line no-restricted-globals
79-
isNaN(num) || num === '' || num === null || (num && num.toString().indexOf('.') === num.toString().length - 1)
80-
);
76+
// - & x. Is not complete number
77+
return (
78+
// eslint-disable-next-line no-restricted-globals
79+
isNaN(num) || num === '' || num === null || (num && num.toString().indexOf('.') === num.toString().length - 1)
80+
);
8181
}

src/components/layout/__test__/layout.test.js

Lines changed: 40 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,47 +2,46 @@ import React from 'react';
22
import { mount, render } from 'enzyme';
33
import Layout from '../index';
44

5-
const { Header, Content, Footer, Sider } = Layout;
5+
const {
6+
Header, Content, Footer, Sider,
7+
} = Layout;
68

79
describe('Layout', () => {
8-
9-
it('basic layout should render correctly', () => {
10-
const wrapper = render((
11-
<Layout>
12-
<Header>header</Header>
13-
<Content>content</Content>
14-
<Footer>footer</Footer>
15-
</Layout>
16-
));
17-
expect(wrapper).toMatchSnapshot();
18-
});
19-
it('left to right layout should render correctly', () => {
20-
const wrapper = render((
21-
<Layout hasSider>
22-
<Sider>Sider</Sider>
23-
<Layout>
24-
<Header>Header</Header>
25-
<Content>Content</Content>
26-
<Footer>Footer</Footer>
27-
</Layout>
28-
</Layout>
29-
));
30-
expect(wrapper).toMatchSnapshot();
31-
});
32-
it('layout property hasSider', () => {
33-
console.log(document);
34-
const wrapper = mount((
35-
<Layout hasSider>
36-
<Sider>Sider</Sider>
37-
<Layout>
38-
<Header>Header</Header>
39-
<Content>Content</Content>
40-
<Footer>Footer</Footer>
41-
</Layout>
42-
</Layout>
43-
));
44-
const hasSide = wrapper.find('section.cloud-layout-has-sider');
45-
expect(hasSide).toBeTruthy();
46-
})
47-
10+
it('basic layout should render correctly', () => {
11+
const wrapper = render((
12+
<Layout>
13+
<Header>header</Header>
14+
<Content>content</Content>
15+
<Footer>footer</Footer>
16+
</Layout>
17+
));
18+
expect(wrapper).toMatchSnapshot();
19+
});
20+
it('left to right layout should render correctly', () => {
21+
const wrapper = render((
22+
<Layout hasSider>
23+
<Sider>Sider</Sider>
24+
<Layout>
25+
<Header>Header</Header>
26+
<Content>Content</Content>
27+
<Footer>Footer</Footer>
28+
</Layout>
29+
</Layout>
30+
));
31+
expect(wrapper).toMatchSnapshot();
32+
});
33+
it('layout property hasSider', () => {
34+
const wrapper = mount((
35+
<Layout hasSider>
36+
<Sider>Sider</Sider>
37+
<Layout>
38+
<Header>Header</Header>
39+
<Content>Content</Content>
40+
<Footer>Footer</Footer>
41+
</Layout>
42+
</Layout>
43+
));
44+
const hasSide = wrapper.find('section.cloud-layout-has-sider');
45+
expect(hasSide).toBeTruthy();
46+
});
4847
});

0 commit comments

Comments
 (0)