@instructure/ui-date-input 9.1.0 → 9.1.1-pr-snapshot-1720090208194

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,94 @@
1
+ ---
2
+ describes: DateInput
3
+ ---
4
+
5
+ This component is an updated version of [`DateInput`](/#DateInput) that's easier to configure for developers and use for end users. It has better accessibility and a year picker option. We recommend using this one instead and in our next major release (v10) it will replace `DateInput`.
6
+
7
+ ### Minimal config
8
+
9
+ - ```js
10
+ class Example extends React.Component {
11
+ state = { value: '' }
12
+
13
+ render() {
14
+ return (
15
+ <DateInput2
16
+ renderLabel="Choose a date"
17
+ value={this.state.value}
18
+ width="20rem"
19
+ onChange={(e, value) => this.setState({ value })}
20
+ invalidDateErrorMessage="Invalid date"
21
+ />
22
+ )
23
+ }
24
+ }
25
+
26
+ render(<Example />)
27
+ ```
28
+
29
+ - ```js
30
+ const Example = () => {
31
+ const [value, setValue] = useState('')
32
+ return (
33
+ <DateInput2
34
+ renderLabel="Choose a date"
35
+ value={value}
36
+ width="20rem"
37
+ onChange={(e, value) => setValue(value)}
38
+ invalidDateErrorMessage="Invalid date"
39
+ />
40
+ )
41
+ }
42
+
43
+ render(<Example />)
44
+ ```
45
+
46
+ ### With year picker
47
+
48
+ - ```js
49
+ class Example extends React.Component {
50
+ state = { value: '' }
51
+
52
+ render() {
53
+ return (
54
+ <DateInput2
55
+ renderLabel="Choose a date"
56
+ width="20rem"
57
+ value={this.state.value}
58
+ onChange={(e, value) => this.setState({ value })}
59
+ invalidDateErrorMessage="Invalid date"
60
+ withYearPicker={{
61
+ screenReaderLabel: 'Year picker',
62
+ startYear: 1999,
63
+ endYear: 2024
64
+ }}
65
+ />
66
+ )
67
+ }
68
+ }
69
+
70
+ render(<Example />)
71
+ ```
72
+
73
+ - ```js
74
+ const Example = () => {
75
+ const [value, setValue] = useState('')
76
+
77
+ return (
78
+ <DateInput2
79
+ renderLabel="Choose a date"
80
+ width="20rem"
81
+ value={value}
82
+ onChange={(e, value) => setValue(value)}
83
+ invalidDateErrorMessage="Invalid date"
84
+ withYearPicker={{
85
+ screenReaderLabel: 'Year picker',
86
+ startYear: 1999,
87
+ endYear: 2024
88
+ }}
89
+ />
90
+ )
91
+ }
92
+
93
+ render(<Example />)
94
+ ```
@@ -0,0 +1,202 @@
1
+ /*
2
+ * The MIT License (MIT)
3
+ *
4
+ * Copyright (c) 2015 - present Instructure, Inc.
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ * of this software and associated documentation files (the "Software"), to deal
8
+ * in the Software without restriction, including without limitation the rights
9
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ * copies of the Software, and to permit persons to whom the Software is
11
+ * furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included in all
14
+ * copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ * SOFTWARE.
23
+ */
24
+
25
+ /** @jsx jsx */
26
+ import { useState, useEffect, useContext } from 'react'
27
+ import type { SyntheticEvent } from 'react'
28
+ import moment from 'moment-timezone'
29
+ import type { Moment } from '@instructure/ui-i18n'
30
+ import { Calendar } from '@instructure/ui-calendar'
31
+ import { IconButton } from '@instructure/ui-buttons'
32
+ import { IconCalendarMonthLine } from '@instructure/ui-icons'
33
+ import { Popover } from '@instructure/ui-popover'
34
+ import { TextInput } from '@instructure/ui-text-input'
35
+
36
+ import { DateTime, ApplyLocaleContext, Locale } from '@instructure/ui-i18n'
37
+ import { jsx } from '@instructure/emotion'
38
+
39
+ import { propTypes } from './props'
40
+ import type { DateInput2Props } from './props'
41
+ import type { FormMessage } from '@instructure/ui-form-field'
42
+
43
+ /**
44
+ ---
45
+ category: components
46
+ ---
47
+ **/
48
+ const DateInput2 = ({
49
+ renderLabel,
50
+ isRequired = false,
51
+ interaction = 'enabled',
52
+ value = '',
53
+ size = 'medium',
54
+ isInline = false,
55
+ messages = [],
56
+ width,
57
+ onChange,
58
+ onBlur,
59
+ withYearPicker,
60
+ invalidDateErrorMessage,
61
+ locale,
62
+ timezone,
63
+ placeholder
64
+ }: DateInput2Props) => {
65
+ const [selectedDate, setSelectedDate] = useState<string>('')
66
+ const [inputMessages, setInputMessages] = useState<FormMessage[]>(messages)
67
+ const [showPopover, setShowPopover] = useState<boolean>(false)
68
+ const localeContext = useContext(ApplyLocaleContext)
69
+
70
+ useEffect(() => {
71
+ validateInput(true)
72
+ }, [value])
73
+
74
+ const handleInputChange = (e: SyntheticEvent, value: string) => {
75
+ onChange?.(e, value)
76
+ }
77
+
78
+ const handleDateSelected = (
79
+ dateString: string,
80
+ momentDate: Moment,
81
+ e: SyntheticEvent
82
+ ) => {
83
+ setSelectedDate(dateString)
84
+ handleInputChange(
85
+ e,
86
+ `${momentDate.format('MMMM')} ${momentDate.format(
87
+ 'D'
88
+ )}, ${momentDate.format('YYYY')}`
89
+ )
90
+ setShowPopover(false)
91
+ }
92
+
93
+ const validateInput = (onlyRemoveError = false) => {
94
+ if (
95
+ moment
96
+ .tz(
97
+ value ? value : '',
98
+ [
99
+ DateTime.momentISOFormat,
100
+ 'llll',
101
+ 'LLLL',
102
+ 'lll',
103
+ 'LLL',
104
+ 'll',
105
+ 'LL',
106
+ 'l',
107
+ 'L'
108
+ ],
109
+ getLocale(),
110
+ true,
111
+ getTimezone()
112
+ )
113
+ .isValid() ||
114
+ value === ''
115
+ ) {
116
+ setSelectedDate(value || '')
117
+ setInputMessages(messages)
118
+ return
119
+ }
120
+ if (typeof invalidDateErrorMessage !== 'function' && !onlyRemoveError) {
121
+ setInputMessages([
122
+ {
123
+ type: 'error',
124
+ text: invalidDateErrorMessage
125
+ }
126
+ ])
127
+ }
128
+ }
129
+
130
+ const getLocale = () => {
131
+ if (locale) {
132
+ return locale
133
+ } else if (localeContext.locale) {
134
+ return localeContext.locale
135
+ }
136
+ return Locale.browserLocale()
137
+ }
138
+
139
+ const getTimezone = () => {
140
+ if (timezone) {
141
+ return timezone
142
+ } else if (localeContext.timezone) {
143
+ return localeContext.timezone
144
+ }
145
+ return DateTime.browserTimeZone()
146
+ }
147
+
148
+ const handleBlur = (e: SyntheticEvent) => {
149
+ onBlur?.(e)
150
+ validateInput(false)
151
+ }
152
+
153
+ return (
154
+ <TextInput
155
+ renderLabel={renderLabel}
156
+ onChange={handleInputChange}
157
+ onBlur={handleBlur}
158
+ isRequired={isRequired}
159
+ value={value}
160
+ placeholder={placeholder}
161
+ width={width}
162
+ size={size}
163
+ display={isInline ? 'inline-block' : 'block'}
164
+ messages={inputMessages}
165
+ interaction={interaction}
166
+ renderAfterInput={
167
+ <Popover
168
+ renderTrigger={
169
+ <IconButton
170
+ withBackground={false}
171
+ withBorder={false}
172
+ screenReaderLabel="Calendar"
173
+ shape="circle"
174
+ interaction={interaction}
175
+ >
176
+ <IconCalendarMonthLine />
177
+ </IconButton>
178
+ }
179
+ isShowingContent={showPopover}
180
+ onShowContent={() => setShowPopover(true)}
181
+ onHideContent={() => setShowPopover(false)}
182
+ on="click"
183
+ shouldContainFocus
184
+ shouldReturnFocus
185
+ shouldCloseOnDocumentClick
186
+ >
187
+ <Calendar
188
+ withYearPicker={withYearPicker}
189
+ onDateSelected={handleDateSelected}
190
+ selectedDate={selectedDate}
191
+ visibleMonth={selectedDate}
192
+ />
193
+ </Popover>
194
+ }
195
+ />
196
+ )
197
+ }
198
+
199
+ DateInput2.propTypes = propTypes
200
+
201
+ export default DateInput2
202
+ export { DateInput2 }
@@ -0,0 +1,176 @@
1
+ /*
2
+ * The MIT License (MIT)
3
+ *
4
+ * Copyright (c) 2015 - present Instructure, Inc.
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ * of this software and associated documentation files (the "Software"), to deal
8
+ * in the Software without restriction, including without limitation the rights
9
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ * copies of the Software, and to permit persons to whom the Software is
11
+ * furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included in all
14
+ * copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ * SOFTWARE.
23
+ */
24
+
25
+ import PropTypes from 'prop-types'
26
+ import type { SyntheticEvent } from 'react'
27
+
28
+ import { controllable } from '@instructure/ui-prop-types'
29
+ import { FormPropTypes } from '@instructure/ui-form-field'
30
+ import type { FormMessage } from '@instructure/ui-form-field'
31
+ import type { Renderable, PropValidators } from '@instructure/shared-types'
32
+
33
+ type DateInput2Props = {
34
+ /**
35
+ * Specifies the input label.
36
+ */
37
+ renderLabel: Renderable
38
+ /**
39
+ * Specifies the input value.
40
+ */
41
+ value?: string // TODO: controllable(PropTypes.string)
42
+ /**
43
+ * Specifies the input size.
44
+ */
45
+ size?: 'small' | 'medium' | 'large'
46
+ /**
47
+ * Html placeholder text to display when the input has no value. This should
48
+ * be hint text, not a label replacement.
49
+ */
50
+ placeholder?: string
51
+ /**
52
+ * Callback fired when the input changes.
53
+ */
54
+ onChange?: (event: React.SyntheticEvent, value: string) => void
55
+ /**
56
+ * Callback executed when the input fires a blur event.
57
+ */
58
+ onBlur?: (event: React.SyntheticEvent) => void
59
+ /**
60
+ * Specifies if interaction with the input is enabled, disabled, or readonly.
61
+ * When "disabled", the input changes visibly to indicate that it cannot
62
+ * receive user interactions. When "readonly" the input still cannot receive
63
+ * user interactions but it keeps the same styles as if it were enabled.
64
+ */
65
+ interaction?: 'enabled' | 'disabled' | 'readonly'
66
+ /**
67
+ * Specifies if the input is required.
68
+ */
69
+ isRequired?: boolean
70
+ /**
71
+ * Controls whether the input is rendered inline with other elements or if it
72
+ * is rendered as a block level element.
73
+ */
74
+ isInline?: boolean
75
+ /**
76
+ * Specifies the width of the input.
77
+ */
78
+ width?: string
79
+ /**
80
+ * Displays messages and validation for the input. It should be an object
81
+ * with the following shape:
82
+ * `{
83
+ * text: PropTypes.node,
84
+ * type: PropTypes.oneOf(['error', 'hint', 'success', 'screenreader-only'])
85
+ * }`
86
+ */
87
+ messages?: FormMessage[]
88
+ /**
89
+ * Callback fired requesting the calendar be shown.
90
+ */
91
+ onRequestShowCalendar?: (event: SyntheticEvent) => void
92
+ /**
93
+ * Callback fired requesting the calendar be hidden.
94
+ */
95
+ onRequestHideCalendar?: (event: SyntheticEvent) => void
96
+ /**
97
+ * The message shown to the user when the data is invalid.
98
+ * If a string, shown to the user anytime the input is invalid.
99
+ *
100
+ * If a function, receives a single parameter:
101
+ * - *rawDateValue*: the string entered as a date by the user.
102
+ **/
103
+ invalidDateErrorMessage?: string | ((rawDateValue: string) => FormMessage)
104
+ /**
105
+ * A standard language identifier.
106
+ *
107
+ * See [Moment.js](https://momentjs.com/timezone/docs/#/using-timezones/parsing-in-zone/) for
108
+ * more details.
109
+ *
110
+ * This property can also be set via a context property and if both are set
111
+ * then the component property takes precedence over the context property.
112
+ *
113
+ * The web browser's locale will be used if no value is set via a component
114
+ * property or a context property.
115
+ **/
116
+ locale?: string
117
+ /**
118
+ * A timezone identifier in the format: *Area/Location*
119
+ *
120
+ * See [List of tz database time zones](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones) for the list
121
+ * of possible options.
122
+ *
123
+ * This property can also be set via a context property and if both are set
124
+ * then the component property takes precedence over the context property.
125
+ *
126
+ * The web browser's timezone will be used if no value is set via a component
127
+ * property or a context property.
128
+ **/
129
+ timezone?: string
130
+
131
+ /**
132
+ * If set, years can be picked from a dropdown.
133
+ * It accepts an object.
134
+ * screenReaderLabel: string // e.g.: i18n("pick a year")
135
+ *
136
+ * onRequestYearChange?:(e: React.MouseEvent,requestedYear: number): void // if set, on year change, only this will be called and no internal change will take place
137
+ *
138
+ * startYear: number // e.g.: 2001, sets the start year of the selectable list
139
+ *
140
+ * endYear: number // e.g.: 2030, sets the end year of the selectable list
141
+ */
142
+ withYearPicker?: {
143
+ screenReaderLabel: string
144
+ onRequestYearChange?: (e: SyntheticEvent, requestedYear: number) => void
145
+ startYear: number
146
+ endYear: number
147
+ }
148
+ }
149
+
150
+ type PropKeys = keyof DateInput2Props
151
+
152
+ const propTypes: PropValidators<PropKeys> = {
153
+ renderLabel: PropTypes.oneOfType([PropTypes.node, PropTypes.func]).isRequired,
154
+ value: controllable(PropTypes.string),
155
+ size: PropTypes.oneOf(['small', 'medium', 'large']),
156
+ placeholder: PropTypes.string,
157
+ onChange: PropTypes.func,
158
+ onBlur: PropTypes.func,
159
+ interaction: PropTypes.oneOf(['enabled', 'disabled', 'readonly']),
160
+ isRequired: PropTypes.bool,
161
+ isInline: PropTypes.bool,
162
+ width: PropTypes.string,
163
+ messages: PropTypes.arrayOf(FormPropTypes.message),
164
+ onRequestShowCalendar: PropTypes.func,
165
+ onRequestHideCalendar: PropTypes.func,
166
+ invalidDateErrorMessage: PropTypes.oneOfType([
167
+ PropTypes.func,
168
+ PropTypes.string
169
+ ]),
170
+ locale: PropTypes.string,
171
+ timezone: PropTypes.string,
172
+ withYearPicker: PropTypes.object
173
+ }
174
+
175
+ export type { DateInput2Props }
176
+ export { propTypes }
package/src/index.ts CHANGED
@@ -23,4 +23,5 @@
23
23
  */
24
24
 
25
25
  export { DateInput } from './DateInput'
26
+ export { DateInput2 } from './DateInput2'
26
27
  export type { DateInputProps } from './DateInput/props'
@@ -10,6 +10,9 @@
10
10
  {
11
11
  "path": "../ui-babel-preset/tsconfig.build.json"
12
12
  },
13
+ {
14
+ "path": "../ui-buttons/tsconfig.build.json"
15
+ },
13
16
  {
14
17
  "path": "../ui-test-utils/tsconfig.build.json"
15
18
  },