Skip to content

Commit b45ec15

Browse files
committed
feat: update the icon that can be used to clear the value of the input
1 parent 7cfd0e6 commit b45ec15

File tree

5 files changed

+31
-11
lines changed

5 files changed

+31
-11
lines changed

examples/stories/example.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export default class Example extends PureComponent<{}, {}> {
5454
/>
5555
</div>
5656
<div className="">
57-
<h1>input value can be cleared:</h1>
57+
<h1>input value can't be cleared:</h1>
5858
<Input
5959
canClear={false}
6060
numericInput={true}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
},
6666
"dependencies": {
6767
"classnames": "^2.2.6",
68+
"or-icons": "^1.2.1",
6869
"or-theme": "^1.2.0"
6970
},
7071
"peerDependencies": {

src/Input.tsx

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import clx from 'classnames'
22
import React, { PureComponent } from 'react'
33

4+
import { SvgBorderedClose } from 'or-icons'
5+
46
interface Props {
57
/**
68
* additional classname
@@ -59,9 +61,14 @@ export class Input extends PureComponent<Props, {}> {
5961
maxLength={maxlength}
6062
onChange={this.handleChange}
6163
/>
62-
{canClear && (
63-
<div className="or-clear-icon" onClick={this.handleClear} />
64-
)}
64+
{canClear &&
65+
this.props.value !== '' && (
66+
<SvgBorderedClose
67+
className="or-clear-icon"
68+
size="18"
69+
onClick={this.handleClear}
70+
/>
71+
)}
6572
</div>
6673
)
6774
}

src/styles.scss

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
.or-input {
44
position: relative;
55
display: inline-block;
6+
border: 1px solid $gray2;
67

78
input{
89
width: 100%;
@@ -11,6 +12,7 @@
1112
height: 38px;
1213
padding: 0 30px 0 5px;
1314
font-size: 14px;
15+
border: none;
1416
box-sizing: border-box;
1517
}
1618

@@ -26,11 +28,11 @@
2628
top: 50%;
2729
right: 10px;
2830
transform: translateY(-50%);
29-
width: 15px;
30-
height: 15px;
31-
background-color: #aaa;
32-
border-radius: 50%;
31+
width: 18px;
32+
height: 18px;
3333
cursor: pointer;
34+
fill: $gray2;
35+
color: $gray2;
3436
}
3537
}
3638

tests/Input.test.tsx

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ describe('src/index', () => {
1111
expect(wrapper.find('.or-input').length).toBe(1)
1212
expect(wrapper.find('.or-input input').prop('placeholder')).toBe('')
1313
expect(wrapper.find('.or-input input').prop('value')).toBe('')
14-
expect(wrapper.find('.or-input .or-clear-icon').length).toBe(1)
14+
expect(wrapper.find('.or-input .or-clear-icon').length).toBe(0)
1515
})
1616

1717
it('should render basicly #placeholder', () => {
@@ -28,8 +28,17 @@ describe('src/index', () => {
2828
wrapper = mount(<RenderInput />)
2929
const input = wrapper.find('.or-input input')
3030
input.simulate('change', { target: { value: 'Changed' } })
31-
3231
expect(wrapper.find('.or-input input').prop('value')).toBe('Changed')
32+
expect(wrapper.find('.or-input .or-clear-icon').length).toBe(2)
33+
})
34+
})
35+
36+
describe('simulate clear input events', () => {
37+
let wrapper
38+
it('onchange event', () => {
39+
wrapper = mount(<RenderInput value="Dan" />)
40+
expect(wrapper.find('.or-input input').prop('value')).toBe('Dan')
41+
expect(wrapper.find('.or-input .or-clear-icon').length).toBe(2)
3342
})
3443
})
3544
})
@@ -38,11 +47,12 @@ interface Props {
3847
numericInput?: boolean
3948
maxlength?: number
4049
placeholder?: string
50+
value?: string
4151
}
4252

4353
class RenderInput extends React.Component<Props, {}> {
4454
state = {
45-
name: ''
55+
name: this.props.value || ''
4656
}
4757

4858
render() {

0 commit comments

Comments
 (0)