Skip to content

Commit

Permalink
[CLD-237]Date picker component (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
NghiaPham authored Sep 11, 2019
1 parent 6d556c9 commit 9d9d9a0
Show file tree
Hide file tree
Showing 11 changed files with 184 additions and 7 deletions.
1 change: 1 addition & 0 deletions __mocks__/styleMock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = {}
5 changes: 4 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,8 @@ module.exports = {
},
snapshotSerializers: [
"enzyme-to-json/serializer"
]
],
"moduleNameMapper": {
"\\.(css|less)$": "<rootDir>/__mocks__/styleMock.js"
}
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
"dayjs": "^1.8.16",
"formik": "^1.5.8",
"jsonwebtoken": "^8.5.1",
"react-datepicker": "^2.9.6",
"react-google-map": "^3.1.1",
"react-google-maps-loader": "^4.2.5",
"react-icons": "^3.7.0"
Expand Down
5 changes: 4 additions & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ export default [
const content = ['./dist/elements.esm.js']
const options = {
output: 'dist/index.css',
minify: true
minify: true,
whitelist: [
'*react-datepicker*'
]
// Uncomment this line if you want to see the CSS purified from the package
// rejected: true
}
Expand Down
3 changes: 0 additions & 3 deletions src/components/Checkbox/__tests__/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@ describe('Checkbox', () => {
/>
)
expect(wrapper.find('label')).toHaveLength(1)
// TODO: will resolve this later. Not sure why this doesn't work yet
// wrapper.find('input[type="checkbox"]').simulate('change', { target: { checked: true } })
// expect(wrapper.find('input[type="checkbox"]').prop('checked')).toBeTruthy()
})

afterEach(() => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Checkbox should match a snapshot 1`] = `
<FormikConnect(FieldInner)
name="abc"
render={[Function]}
/>
`;
36 changes: 36 additions & 0 deletions src/components/DatePicker/__tests__/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import * as React from 'react'
import { shallow, mount } from 'enzyme'
import { DatePicker, DatePickerProps } from '../index'
import { Formik, Form } from 'formik'
import toJson from 'enzyme-to-json'

const props: DatePickerProps = {
name: 'abc',
labelText: 'abc',
id: '123'
}

describe('Checkbox', () => {
it('should match a snapshot', () => {
expect(toJson(shallow(<DatePicker {...props} />))).toMatchSnapshot()
})

it('should work when integrating with Formik', () => {
const wrapper = mount(
<Formik
initialValues={{ test: false }}
onSubmit={jest.fn()}
render={() => (
<Form>
<DatePicker {...props} />
</Form>
)}
/>
)
expect(wrapper.find('label')).toHaveLength(1)
})

afterEach(() => {
jest.resetAllMocks()
})
})
59 changes: 59 additions & 0 deletions src/components/DatePicker/date-picker.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import React, { useRef, useEffect } from 'react'

import { storiesOf } from '@storybook/react'
import { action } from '@storybook/addon-actions'
import { DatePicker } from '.'
import { Form, Formik } from 'formik'

storiesOf('form/DatePicker', module)
.add('Date Picker', () => (
<section className="section">
<Formik
initialValues={{ demo: new Date() }}
onSubmit={values => {
action('Form Values' + values)
}}
render={() => (
<Form>
<div className="column is-half-desktop">
<DatePicker name="demo" labelText="demo" id="demo" />
</div>
</Form>
)}
/>
</section>
))
.add('Date picker error', () => {
const Parent = () => {
const ref = useRef(null)
useEffect(() => {
console.log((ref.current as any).setTouched({ demo: true }))
}, [])

return (
<section className="section">
<Formik
ref={ref}
validate={() => {
return {
demo: 'error'
}
}}
initialValues={{ demo: new Date() }}
onSubmit={values => {
action('Form Values' + values)
}}
render={() => (
<Form>
<div className="column is-half-desktop">
<DatePicker name="demo" labelText="Demo" id="test" />
</div>
</Form>
)}
/>
</section>
)
}

return <Parent />
})
47 changes: 47 additions & 0 deletions src/components/DatePicker/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import * as React from 'react'
import { Field } from 'formik'
import ReactDatePicker from 'react-datepicker'

export interface DatePickerProps {
name: string
id: string
labelText: string
}

export const DatePicker = ({ name, id, labelText }: DatePickerProps) => {
return (
<Field
name={name}
render={({ field, form: { touched, errors, setFieldValue } }) => {
const hasError = touched[field.name] && errors[field.name]

const className = hasError ? 'input is-danger' : 'input is-primary'
return (
<div className="field pb-2">
<div className="control">
<label className="label" htmlFor={id}>
{labelText}
</label>
<ReactDatePicker
className={className}
name={name}
id={id}
labelText={labelText}
{...field}
selected={field.value}
onChange={value => {
setFieldValue(name, value)
}}
/>
{hasError && (
<div className="has-text-danger" data-test="input-error">
{errors[field.name]}
</div>
)}
</div>
</div>
)
}}
/>
)
}
1 change: 1 addition & 0 deletions src/styles/index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ html {
@import './components/toast.scss';
@import './components/menu.scss';
@import './components/appointment-tile.scss';
@import './components/react-datepicker.scss';
25 changes: 23 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3380,7 +3380,7 @@ class-utils@^0.3.5:
isobject "^3.0.0"
static-extend "^0.1.1"

classnames@^2.2.5:
classnames@^2.2.5, classnames@^2.2.6:
version "2.2.6"
resolved "https://registry.yarnpkg.com/classnames/-/classnames-2.2.6.tgz#43935bffdd291f326dad0a205309b38d00f650ce"
integrity sha512-JR/iSQOSt+LQIWwrwEzJ9uk0xfN3mTVYMwt1Ir5mUcSN6pU+V4zQFFaJsclJbPuAUQH+yfWef6tm7l1quW3C8Q==
Expand Down Expand Up @@ -4016,6 +4016,11 @@ data-urls@^1.0.0:
whatwg-mimetype "^2.2.0"
whatwg-url "^7.0.0"

date-fns@^2.0.1:
version "2.1.0"
resolved "https://registry.yarnpkg.com/date-fns/-/date-fns-2.1.0.tgz#0d7e806c3cefe14a943532dbf968995ccfd46bd9"
integrity sha512-eKeLk3sLCnxB/0PN4t1+zqDtSs4jb4mXRSTZ2okmx/myfWyDqeO4r5nnmA5LClJiCwpuTMeK2v5UQPuE4uMaxA==

date-now@^0.1.4:
version "0.1.4"
resolved "https://registry.yarnpkg.com/date-now/-/date-now-0.1.4.tgz#eaf439fd4d4848ad74e5cc7dbef200672b9e345b"
Expand Down Expand Up @@ -9472,6 +9477,17 @@ react-clientside-effect@^1.2.0:
dependencies:
"@babel/runtime" "^7.0.0"

react-datepicker@^2.9.6:
version "2.9.6"
resolved "https://registry.yarnpkg.com/react-datepicker/-/react-datepicker-2.9.6.tgz#26190c9f71692149d0d163398aa19e08626444b1"
integrity sha512-PLiVhyAr567gWuLMZwIH9WpTIZOZVLhEFyuUzSx3kmQdiikjrYpdNlxsfbbgaxRnee5y08KJZequaqRsNySXmw==
dependencies:
classnames "^2.2.6"
date-fns "^2.0.1"
prop-types "^15.7.2"
react-onclickoutside "^6.9.0"
react-popper "^1.3.4"

react-dev-utils@^9.0.0:
version "9.0.3"
resolved "https://registry.yarnpkg.com/react-dev-utils/-/react-dev-utils-9.0.3.tgz#7607455587abb84599451460eb37cef0b684131a"
Expand Down Expand Up @@ -9639,6 +9655,11 @@ react-lifecycles-compat@^3.0.2, react-lifecycles-compat@^3.0.4:
resolved "https://registry.yarnpkg.com/react-lifecycles-compat/-/react-lifecycles-compat-3.0.4.tgz#4f1a273afdfc8f3488a8c516bfda78f872352362"
integrity sha512-fBASbA6LnOU9dOU2eW7aQ8xmYBSXUIWr+UmF9b1efZBazGNO+rcXT/icdKnYm2pTwcRylVUYwW7H1PHfLekVzA==

react-onclickoutside@^6.9.0:
version "6.9.0"
resolved "https://registry.yarnpkg.com/react-onclickoutside/-/react-onclickoutside-6.9.0.tgz#a54bc317ae8cf6131a5d78acea55a11067f37a1f"
integrity sha512-8ltIY3bC7oGhj2nPAvWOGi+xGFybPNhJM0V1H8hY/whNcXgmDeaeoCMPPd8VatrpTsUWjb/vGzrmu6SrXVty3A==

react-popper-tooltip@^2.8.3:
version "2.8.3"
resolved "https://registry.yarnpkg.com/react-popper-tooltip/-/react-popper-tooltip-2.8.3.tgz#1c63e7473a96362bd93be6c94fa404470a265197"
Expand All @@ -9647,7 +9668,7 @@ react-popper-tooltip@^2.8.3:
"@babel/runtime" "^7.4.5"
react-popper "^1.3.3"

react-popper@^1.3.3:
react-popper@^1.3.3, react-popper@^1.3.4:
version "1.3.4"
resolved "https://registry.yarnpkg.com/react-popper/-/react-popper-1.3.4.tgz#f0cd3b0d30378e1f663b0d79bcc8614221652ced"
integrity sha512-9AcQB29V+WrBKk6X7p0eojd1f25/oJajVdMZkywIoAV6Ag7hzE1Mhyeup2Q1QnvFRtGQFQvtqfhlEoDAPfKAVA==
Expand Down

0 comments on commit 9d9d9a0

Please sign in to comment.