Skip to content

Commit 7c4400e

Browse files
committed
created conditional rendering of controls & tests
1 parent 3bfe3e0 commit 7c4400e

File tree

2 files changed

+109
-17
lines changed

2 files changed

+109
-17
lines changed

frontend/src/components/widgets/NumberInput/NumberInput.test.tsx

Lines changed: 93 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,11 +191,62 @@ describe("NumberInput widget", () => {
191191
expect(wrapper.find(UIInput).props().overrides.Input.props.step).toBe(1)
192192
})
193193

194-
it("should change the state when ArrowUp", () => {
194+
it("should have default Float step when step=undefined", () => {
195+
const props = getFloatProps({ default: 1.0 })
196+
const wrapper = shallow(<NumberInput {...props} />)
197+
const InputWrapper = wrapper.find(UIInput)
198+
199+
// @ts-ignore
200+
expect(InputWrapper.props().overrides.Input.props.step).toBe(0.01)
201+
})
202+
203+
it("should have default Int step when step=undefined", () => {
204+
const props = getIntProps({ default: 10 })
205+
const wrapper = shallow(<NumberInput {...props} />)
206+
const InputWrapper = wrapper.find(UIInput)
207+
208+
// @ts-ignore
209+
expect(InputWrapper.props().overrides.Input.props.step).toBe(1)
210+
})
211+
212+
it("should have default Float step when step=0.0", () => {
213+
const props = getFloatProps({ default: 1.0, step: 0.0 })
214+
const wrapper = shallow(<NumberInput {...props} />)
215+
const InputWrapper = wrapper.find(UIInput)
216+
217+
// @ts-ignore
218+
expect(InputWrapper.props().overrides.Input.props.step).toBe(0.01)
219+
})
220+
221+
it("should have default Int step when step=0", () => {
222+
const props = getIntProps({ default: 10, step: 0 })
223+
const wrapper = shallow(<NumberInput {...props} />)
224+
const InputWrapper = wrapper.find(UIInput)
225+
226+
// @ts-ignore
227+
expect(InputWrapper.props().overrides.Input.props.step).toBe(1)
228+
})
229+
230+
it("should not have step buttons when step=0", () => {
231+
const props = getIntProps({ default: 10, step: 0 })
232+
const wrapper = shallow(<NumberInput {...props} />)
233+
const InputWrapper = wrapper.find(UIInput)
234+
235+
expect(InputWrapper.exists("StyledInputControls")).toBe(false)
236+
})
237+
238+
it("should not have step buttons when step=0.0", () => {
239+
const props = getFloatProps({ step: 0.0 })
240+
const wrapper = shallow(<NumberInput {...props} />)
241+
const InputWrapper = wrapper.find(UIInput)
242+
243+
expect(InputWrapper.exists("StyledInputControls")).toBe(false)
244+
})
245+
246+
it("should let ArrowKeys change Int state with no rendered buttons", () => {
195247
const props = getIntProps({
196-
format: "%d",
197248
default: 10,
198-
step: 1,
249+
step: 0,
199250
})
200251
const wrapper = shallow(<NumberInput {...props} />)
201252
const InputWrapper = wrapper.find(UIInput)
@@ -209,6 +260,45 @@ describe("NumberInput widget", () => {
209260
expect(preventDefault).toHaveBeenCalled()
210261
expect(wrapper.state("value")).toBe(11)
211262
expect(wrapper.state("dirty")).toBe(false)
263+
264+
// @ts-ignore
265+
InputWrapper.props().onKeyDown({
266+
key: "ArrowDown",
267+
preventDefault,
268+
})
269+
270+
expect(preventDefault).toHaveBeenCalled()
271+
expect(wrapper.state("value")).toBe(10)
272+
expect(wrapper.state("dirty")).toBe(false)
273+
})
274+
275+
it("should let ArrowKeys change Float state with no rendered buttons", () => {
276+
const props = getFloatProps({
277+
default: 1.0,
278+
step: 0.0,
279+
})
280+
const wrapper = shallow(<NumberInput {...props} />)
281+
const InputWrapper = wrapper.find(UIInput)
282+
283+
// @ts-ignore
284+
InputWrapper.props().onKeyDown({
285+
key: "ArrowDown",
286+
preventDefault,
287+
})
288+
289+
expect(preventDefault).toHaveBeenCalled()
290+
expect(wrapper.state("value")).toBe(0.99)
291+
expect(wrapper.state("dirty")).toBe(false)
292+
293+
// @ts-ignore
294+
InputWrapper.props().onKeyDown({
295+
key: "ArrowUp",
296+
preventDefault,
297+
})
298+
299+
expect(preventDefault).toHaveBeenCalled()
300+
expect(wrapper.state("value")).toBe(1.0)
301+
expect(wrapper.state("dirty")).toBe(false)
212302
})
213303

214304
it("should change the state when ArrowDown", () => {

frontend/src/components/widgets/NumberInput/NumberInput.tsx

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -281,20 +281,22 @@ class NumberInput extends React.PureComponent<Props, State> {
281281
},
282282
}}
283283
/>
284-
<StyledInputControls>
285-
<StyledInputControl
286-
className="step-down"
287-
onClick={this.modifyValueUsingStep("decrement")}
288-
>
289-
<Icon content={Minus} size="xs" />
290-
</StyledInputControl>
291-
<StyledInputControl
292-
className="step-up"
293-
onClick={this.modifyValueUsingStep("increment")}
294-
>
295-
<Icon content={Plus} size="xs" />
296-
</StyledInputControl>
297-
</StyledInputControls>
284+
{element.step !== 0 && (
285+
<StyledInputControls>
286+
<StyledInputControl
287+
className="step-down"
288+
onClick={this.modifyValueUsingStep("decrement")}
289+
>
290+
<Icon content={Minus} size="xs" />
291+
</StyledInputControl>
292+
<StyledInputControl
293+
className="step-up"
294+
onClick={this.modifyValueUsingStep("increment")}
295+
>
296+
<Icon content={Plus} size="xs" />
297+
</StyledInputControl>
298+
</StyledInputControls>
299+
)}
298300
</StyledInputContainer>
299301
<StyledInstructionsContainer>
300302
<InputInstructions

0 commit comments

Comments
 (0)