-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Federico Zivolo
committed
Jan 2, 2019
1 parent
d7bc0bb
commit 353f254
Showing
9 changed files
with
210 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
The `InputNumber` component provides the same API of the HTML `<input type="number" />`, | ||
with the addition of some useful features. | ||
|
||
Default: | ||
|
||
```js | ||
<InputNumber min={-5} defaultValue={15} step={2} /> | ||
``` | ||
|
||
With optional unit (specified with the `unit` property): | ||
|
||
```js | ||
<InputNumber unit="pt" defaultValue={15} /> | ||
``` | ||
|
||
Disabled: | ||
|
||
```js | ||
<InputNumber defaultValue={15} disabled /> | ||
``` | ||
|
||
Read Only: | ||
|
||
```js | ||
<InputNumber defaultValue={15} readOnly /> | ||
``` |
7 changes: 7 additions & 0 deletions
7
packages/react-forms/src/InputNumber/__snapshots__/index.test.js.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`renders the expected markup 1`] = ` | ||
<ContextConsumer> | ||
<Component /> | ||
</ContextConsumer> | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// @flow | ||
import * as React from 'react'; | ||
import styled from '@emotion/styled/macro'; | ||
import { withFallback as wf } from '@quid/theme'; | ||
import { Icon } from '@quid/react-core'; | ||
import InputText from '../InputText'; | ||
|
||
type Props = { | ||
step?: number, | ||
disabled?: boolean, | ||
readOnly?: boolean, | ||
unit?: string, | ||
}; | ||
|
||
const Addon = styled.div` | ||
display: flex; | ||
flex-direction: column; | ||
align-items: center; | ||
align-self: stretch; | ||
`; | ||
|
||
const Unit = styled.div` | ||
margin-left: -0.7em; | ||
margin-right: 0.7em; | ||
`; | ||
|
||
const Caret = styled.button` | ||
all: unset; | ||
display: flex; | ||
align-items: center; | ||
&:focus-visible { | ||
box-shadow: 0 0 0 0.5px ${wf(props => props.theme.background)}, | ||
0 0 2px 2px ${wf(props => props.theme.selected)}; | ||
} | ||
&:not(:disabled):active { | ||
color: ${wf(props => props.theme.selected)}; | ||
} | ||
`; | ||
|
||
const InputNumber = styled( | ||
({ step = 1, disabled = false, readOnly = false, unit, ...props }: Props) => { | ||
const input = React.createRef(); | ||
return ( | ||
<InputText | ||
ref={input} | ||
type="number" | ||
step={step} | ||
disabled={disabled} | ||
readOnly={readOnly} | ||
renderAddon={() => ( | ||
<> | ||
<Unit>{unit}</Unit> | ||
<Addon> | ||
<Caret | ||
disabled={disabled || readOnly} | ||
onClick={() => input.current && input.current.stepUp(step)} | ||
> | ||
<Icon name="caret_up" /> | ||
</Caret> | ||
<Caret | ||
disabled={disabled || readOnly} | ||
onClick={() => input.current && input.current.stepDown(step)} | ||
> | ||
<Icon name="caret_down" /> | ||
</Caret> | ||
</Addon> | ||
</> | ||
)} | ||
{...props} | ||
/> | ||
); | ||
} | ||
)` | ||
text-align: right; | ||
`; | ||
|
||
// @component | ||
export default InputNumber; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// @flow | ||
import React from 'react'; | ||
import { shallow, mount } from 'enzyme'; | ||
import InputNumber from './'; | ||
|
||
it('renders the expected markup', () => { | ||
const wrapper = shallow(<InputNumber />); | ||
expect(wrapper).toMatchSnapshot(); | ||
}); | ||
|
||
it('increments on click on up arrow', () => { | ||
const stepUp = jest.fn(); | ||
const wrapper = mount(<InputNumber defaultValue={0} />); | ||
wrapper.find('input').getDOMNode().stepUp = stepUp; | ||
wrapper | ||
.find('button') | ||
.at(0) | ||
.simulate('click'); | ||
|
||
expect(stepUp).toBeCalled(); | ||
expect(stepUp.mock.calls.length).toBe(1); | ||
}); | ||
|
||
it('decrements on click on down arrow', () => { | ||
const stepDown = jest.fn(); | ||
const wrapper = mount(<InputNumber defaultValue={0} />); | ||
wrapper.find('input').getDOMNode().stepDown = stepDown; | ||
wrapper | ||
.find('button') | ||
.at(1) | ||
.simulate('click'); | ||
|
||
expect(stepDown).toBeCalled(); | ||
expect(stepDown.mock.calls.length).toBe(1); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
// @flow | ||
export { Button } from '@quid/react-core'; | ||
export { default as InputText } from './InputText'; | ||
export { default as InputNumber } from './InputNumber'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters