Skip to content

Commit

Permalink
feat: calendar input to work with react final form
Browse files Browse the repository at this point in the history
  • Loading branch information
flaminic authored and kabaros committed Sep 24, 2024
1 parent e67a7e8 commit 827c3f8
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Press **Submit** to see the form values logged to the console.
`

export default {
title: 'Input Field (Final Form)',
title: 'Final Form - Input Field ',
component: InputFieldFF,
decorators: [formDecorator],
parameters: { docs: { description: { component: description } } },
Expand Down
12 changes: 11 additions & 1 deletion components/calendar/src/calendar-input/calendar-input.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
useDatePicker,
useResolvedDirection,
validateDateString
} from '@dhis2/multi-calendar-dates'
import { Button } from '@dhis2-ui/button'
import { Card } from '@dhis2-ui/card'
Expand Down Expand Up @@ -71,11 +72,20 @@ export const CalendarInput = ({
})

const handleChange = (e) => {
setOpen(false)
setPartialDate(e.value)
}

const handleBlur = (_, e) => {
parentOnDateSelect?.({ calendarDateString: partialDate })
const validated = validateDateString(partialDate, {
calendar,
format,
minDateString: minDate,
maxDateString: maxDate,
strictValidation,
})
parentOnDateSelect?.({ calendarDateString: partialDate, ...validated })

if (
excludeRef.current &&
!excludeRef.current.contains(e.relatedTarget)
Expand Down
57 changes: 57 additions & 0 deletions components/calendar/src/stories/calendar-input.prod.stories.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React, { useState } from 'react'
import { Field, Form } from 'react-final-form'
import { CalendarInput } from '../calendar-input/calendar-input.js'
import { CalendarStoryWrapper } from './calendar-story-wrapper.js'

Expand Down Expand Up @@ -140,3 +141,59 @@ export function CalendarWithEditiableInput() {
</div>
)
}

export function CalendarWithEditiableInputReactForm() {
const [calendarError, setCalendarError] = useState(undefined)

const errored = () => {
return { calendar: calendarError }
}

return (
<Form
onSubmit={(values, meta) => {
console.log('SUBMITTING', { values, meta })
}}
initialValues={{ calendar: '2022-10-12' }}
validate={errored}
>
{({ handleSubmit }) => {
return (
<form onSubmit={handleSubmit}>
<Field name="calendar">
{(props) => (
<CalendarInput
{...props}
input={props.input}
meta={props.meta}
editable
date={props.input.value}
calendar="gregory"
onDateSelect={(date) => {
if (!date.isValid) {
setCalendarError(date.errorMessage)
} else {
setCalendarError(undefined)
}
props.input.onChange(
date ? date?.calendarDateString : ''
)
}}
timeZone={'UTC'}
/>
)}
</Field>

<button
type="submit"
disabled={false}
onClick={handleSubmit}
>
Submit
</button>
</form>
)
}}
</Form>
)
}
20 changes: 16 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7391,14 +7391,26 @@ class-utils@^0.3.5:
isobject "^3.0.0"
static-extend "^0.1.1"

classnames@^2.2.6, classnames@^2.3.1, classnames@^2.3.2:
classnames@^2.2.6, classnames@^2.3.1:
version "2.3.2"
resolved "https://registry.yarnpkg.com/classnames/-/classnames-2.3.2.tgz#351d813bf0137fcc6a76a16b88208d2560a0d924"
integrity sha512-CSbhY4cFEJRe6/GQzIk5qXZ4Jeg5pcsP7b5peFSDpffpe1cqjASH/n9UTjBwOp6XpMSTwQ8Za2K5V02ueA7Tmw==

classnames@^2.3.2:
version "2.5.1"
resolved "https://registry.npmjs.org/classnames/-/classnames-2.5.1.tgz"
resolved "https://registry.yarnpkg.com/classnames/-/classnames-2.5.1.tgz#ba774c614be0f016da105c858e7159eae8e7687b"
integrity sha512-saHYOzhIQs6wy2sVxTM6bUDsQO4F50V9RQ22qBpEdCW+I+/Wmke2HOl6lS6dTpdxVhb88/I6+Hs+438c3lfUow==

clean-css@^5.2.2, clean-css@^5.3.0:
clean-css@^5.2.2:
version "5.2.4"
resolved "https://registry.yarnpkg.com/clean-css/-/clean-css-5.2.4.tgz#982b058f8581adb2ae062520808fb2429bd487a4"
integrity sha512-nKseG8wCzEuji/4yrgM/5cthL9oTDc5UOQyFMvW/Q53oP6gLH690o1NbuTh6Y18nujr7BxlsFuS7gXLnLzKJGg==
dependencies:
source-map "~0.6.0"

clean-css@^5.3.0:
version "5.3.3"
resolved "https://registry.npmjs.org/clean-css/-/clean-css-5.3.3.tgz"
resolved "https://registry.yarnpkg.com/clean-css/-/clean-css-5.3.3.tgz#b330653cd3bd6b75009cc25c714cae7b93351ccd"
integrity sha512-D5J+kHaVb/wKSFcyyV75uCn8fiY4sV38XJoe4CUyGQ+mOU/fMVYUdH1hJC+CJQ5uY3EnW27SbJYS4X8BiLrAFg==
dependencies:
source-map "~0.6.0"
Expand Down

0 comments on commit 827c3f8

Please sign in to comment.