Skip to content

Commit 6bd7b40

Browse files
Merge pull request #38 from vtex/feat/add-input-type-date
Add date to input type
2 parents 611ba1e + b14e956 commit 6bd7b40

File tree

6 files changed

+165
-308
lines changed

6 files changed

+165
-308
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
## [0.2.0] - 2021-08-03
11+
1012
## [0.2.0-beta.13] - 2020-03-26
1113

1214
### Fixed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-hook-form-jsonschema",
3-
"version": "0.2.0-beta.13",
3+
"version": "0.2.0",
44
"description": "Wrapper arround react-hook-form to create forms from a JSON schema.",
55
"main": "output/index.cjs.js",
66
"module": "output/index.esm.js",
@@ -13,6 +13,7 @@
1313
"devDependencies": {
1414
"@rollup/plugin-commonjs": "^11.0.1",
1515
"@rollup/plugin-node-resolve": "^7.0.0",
16+
"@testing-library/react-hooks": "^7.0.1",
1617
"@types/react": "^16.8.22",
1718
"@types/react-intl": "^2.3.18",
1819
"@vtex/test-tools": "^0.3.2",

src/hooks/__mocks__/mockTextSchema.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,31 @@ const mockTextSchema = {
2626
name: 'test-showError',
2727
minLength: 10,
2828
},
29+
stringDateTest: {
30+
type: 'string',
31+
title: 'test-useInput-format-date',
32+
format: 'date',
33+
},
34+
stringDateTimeTest: {
35+
type: 'string',
36+
title: 'test-useInput-format-date-time',
37+
format: 'date-time',
38+
},
39+
stringEmailTest: {
40+
type: 'string',
41+
title: 'test-useInput-format-email',
42+
format: 'email',
43+
},
44+
stringHostnameTest: {
45+
type: 'string',
46+
title: 'test-useInput-format-hostname',
47+
format: 'hostname',
48+
},
49+
stringUriTest: {
50+
type: 'string',
51+
title: 'test-useInput-format-uri',
52+
format: 'uri',
53+
},
2954
},
3055
}
3156

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import React, { FC } from 'react'
2+
import { renderHook } from '@testing-library/react-hooks'
3+
4+
import { FormContext } from '../../components'
5+
import { useInput } from '../useInput'
6+
import mockTextSchema from '../__mocks__/mockTextSchema'
7+
8+
const Wrapper: FC = ({ children }) => {
9+
return <FormContext schema={mockTextSchema}>{children}</FormContext>
10+
}
11+
12+
test('useInput type date', () => {
13+
const { result } = renderHook(() => useInput('#/properties/stringDateTest'), {
14+
wrapper: Wrapper,
15+
})
16+
17+
const { type } = result.current.getInputProps()
18+
expect(type).toBe('date')
19+
})
20+
21+
test('useInput type date-time', () => {
22+
const { result } = renderHook(
23+
() => useInput('#/properties/stringDateTimeTest'),
24+
{
25+
wrapper: Wrapper,
26+
}
27+
)
28+
29+
const { type } = result.current.getInputProps()
30+
expect(type).toBe('datetime-local')
31+
})
32+
33+
test('useInput type email', () => {
34+
const { result } = renderHook(
35+
() => useInput('#/properties/stringEmailTest'),
36+
{
37+
wrapper: Wrapper,
38+
}
39+
)
40+
41+
const { type } = result.current.getInputProps()
42+
expect(type).toBe('email')
43+
})
44+
45+
test('useInput type hostname', () => {
46+
const { result } = renderHook(
47+
() => useInput('#/properties/stringHostnameTest'),
48+
{
49+
wrapper: Wrapper,
50+
}
51+
)
52+
53+
const { type } = result.current.getInputProps()
54+
expect(type).toBe('url')
55+
})
56+
57+
test('useInput type uri', () => {
58+
const { result } = renderHook(() => useInput('#/properties/stringUriTest'), {
59+
wrapper: Wrapper,
60+
})
61+
62+
const { type } = result.current.getInputProps()
63+
expect(type).toBe('url')
64+
})

src/hooks/useInput.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ export const getInputCustomFields = (
1414
let inputType = 'text'
1515
if (currentObject.type === 'string') {
1616
switch (currentObject.format) {
17+
case 'date':
18+
inputType = 'date'
19+
break
1720
case 'date-time':
1821
inputType = 'datetime-local'
1922
break

0 commit comments

Comments
 (0)