Skip to content

Commit 4f1e562

Browse files
feature:add-calendar-component add Date component, add isBirthDay property and create story
1 parent 28c30aa commit 4f1e562

File tree

9 files changed

+1915
-1581
lines changed

9 files changed

+1915
-1581
lines changed

.storybook/main.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ module.exports = {
55
],
66
"addons": [
77
"@storybook/addon-links",
8-
"@storybook/addon-essentials"
8+
"@storybook/addon-essentials",
9+
'@storybook/preset-create-react-app'
910
]
1011
}

example/package-lock.json

+1,128-1,034
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/src/App.js

+12
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
Checkbox,
88
Radio,
99
Phone,
10+
Date,
1011
FormBuilder
1112
} from 'react-form-builder'
1213

@@ -20,6 +21,7 @@ const App = () => {
2021
})
2122

2223
const onSubmitForm = (data) => {
24+
console.log('button:', data)
2325
alert(
2426
`You have submitted your form correctly Data: ${'\n'} ${JSON.stringify(
2527
data,
@@ -42,6 +44,16 @@ const App = () => {
4244
clearErrors={clearErrors}
4345
placeholder='Phone'
4446
/>
47+
<Date
48+
register={register}
49+
setValue={setValue}
50+
name='Date'
51+
registerConfig={{}}
52+
placeholder=''
53+
dateFormat="dd-MM-yyyy"
54+
isMobile={false}
55+
isBirthDate={false}
56+
/>
4557
<Label>An important title field here *</Label>
4658
<Label>
4759
<Checkbox />

package-lock.json

+662-543
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+6-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@
3434
"react-markdown": "^5.0.3",
3535
"react-hook-form": "^6.14.2",
3636
"react-hook-form-input": "^1.1.10",
37-
"react-phone-number-input": "^3.1.10"
37+
"react-phone-number-input": "^3.1.10",
38+
"react-datepicker": "^3.4.1"
3839
},
3940
"devDependencies": {
4041
"@babel/core": "^7.12.10",
@@ -45,9 +46,11 @@
4546
"@storybook/addon-actions": "^6.1.15",
4647
"@storybook/addon-essentials": "^6.1.15",
4748
"@storybook/addon-links": "^6.1.15",
49+
"@storybook/preset-create-react-app": "^3.1.5",
4850
"@storybook/react": "^6.1.15",
4951
"babel-eslint": "^10.0.3",
5052
"cross-env": "^7.0.2",
53+
"date-fns": "^2.16.1",
5154
"eslint": "^6.8.0",
5255
"eslint-config-prettier": "^6.7.0",
5356
"eslint-config-standard": "^14.1.0",
@@ -63,11 +66,12 @@
6366
"npm-run-all": "^4.1.5",
6467
"prettier": "^2.0.4",
6568
"react": "^16.13.1",
69+
"react-datepicker": "^3.4.1",
6670
"react-dom": "^16.13.1",
6771
"react-hook-form": "^6.14.2",
6872
"react-hook-form-input": "^1.1.10",
69-
"react-phone-number-input": "^3.1.10",
7073
"react-markdown": "^5.0.3",
74+
"react-phone-number-input": "^3.1.10",
7175
"rollup": "^2.36.2",
7276
"rollup-plugin-peer-deps-external": "^2.2.4",
7377
"theme-ui": "^0.3.5"

rollup.config.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ export default {
1616
'react-markdown': 'react-markdown',
1717
'react-hook-form-input': 'react-hook-form-input',
1818
'react-phone-number-input': 'react-phone-number-input',
19-
'react-hook-form': 'react-hook-form'
19+
'react-hook-form': 'react-hook-form',
20+
'react-datepicker': 'react-datepicker'
2021
}
2122
},
2223
plugins: [

src/Fields/Date/index.js

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/** @jsx jsx */
2+
import { jsx } from 'theme-ui'
3+
4+
import React from 'react'
5+
import { RHFInput } from 'react-hook-form-input'
6+
import ReactDatePicker from 'react-datepicker'
7+
import { differenceInYears, subYears } from 'date-fns'
8+
import 'react-datepicker/dist/react-datepicker.css'
9+
10+
const isOver18 = (dob) => {
11+
return differenceInYears(new Date(), dob) > 18
12+
}
13+
14+
const getInitialDate = () => {
15+
return subYears(new Date(), 18)
16+
}
17+
18+
const DatePicker = ({
19+
register,
20+
setValue,
21+
name,
22+
registerConfig,
23+
placeholder,
24+
isMobile,
25+
dateFormat,
26+
isBirthDate,
27+
...props
28+
}) => {
29+
const [date, setDate] = React.useState()
30+
31+
const pickerRef = React.useRef(null)
32+
React.useEffect(() => {
33+
if (isMobile && pickerRef.current !== null) {
34+
pickerRef.current.input.readOnly = true
35+
}
36+
}, [isMobile, pickerRef])
37+
38+
return (
39+
<RHFInput
40+
as={
41+
<ReactDatePicker
42+
ref={pickerRef}
43+
portalId='root-portal'
44+
withPortal={isMobile}
45+
placeholderText={placeholder}
46+
dateFormat={dateFormat || 'dd/MM/yyyy'}
47+
showYearDropdown
48+
dropdownMode={isMobile ? 'select' : 'scroll'}
49+
openToDate={date || isBirthDate ? getInitialDate() : new Date()}
50+
disabledKeyboardNavigation
51+
{...props}
52+
/>
53+
}
54+
rules={{
55+
...registerConfig,
56+
validate: {
57+
u18: isBirthDate ? isOver18 : ''
58+
}
59+
}}
60+
name={name}
61+
register={register}
62+
setValue={setValue}
63+
selected={date}
64+
onChange={setDate}
65+
/>
66+
)
67+
}
68+
69+
export default DatePicker

src/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export { default as Checkbox } from './Fields/Checkbox'
77
export { default as Select } from './Fields/Select'
88
export { default as Radio } from './Fields/Radio'
99
export { default as Phone } from './Fields/Phone'
10+
export { default as Date } from './Fields/Date'
1011

1112
// Theme
1213
export { default as ExampleTheme } from './theme'

src/stories/Date.stories.js

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import React from 'react'
2+
import Date from '../Fields/Date'
3+
import { useForm } from 'react-hook-form'
4+
5+
export default {
6+
title: 'Form/Date',
7+
component: Date
8+
}
9+
10+
const Template = (args) => {
11+
const { register, setValue } = useForm({
12+
mode: 'onChange',
13+
reValidateMode: 'onChange'
14+
})
15+
16+
return (
17+
<Date
18+
register={register}
19+
setValue={setValue}
20+
name='Date'
21+
registerConfig={{}}
22+
{...args}
23+
/>
24+
)
25+
}
26+
27+
export const Primary = Template.bind({})
28+
Primary.args = {
29+
placeholder: '',
30+
dateFormat: 'dd-MM-yyyy',
31+
isMobile: false,
32+
isBirthDate: false
33+
}

0 commit comments

Comments
 (0)