-
Notifications
You must be signed in to change notification settings - Fork 42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add datepicker as a fragment component #262
base: main
Are you sure you want to change the base?
Changes from 27 commits
646baf5
6cb5cff
0219acf
63ca1b4
404c8a5
e6a3cf9
668ba8f
4157f0e
5ed4b0b
a381f4e
a3125bd
201484a
65a863c
a4b5655
c8fad9d
3bf5c40
933e7ef
7c3b89e
251e7bb
a9eb7f7
dbb87df
a9dd65e
693d844
8ebec46
2f862e0
38142f7
ffe362d
81ed09e
076953b
b37fd9f
6b82184
7e92ac5
acf664f
ecef193
4d65d81
bc3c30b
9d18919
9312697
211e641
2b7fd06
749688e
cf36bd7
c2178fa
8a15d63
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import React from 'react'; | ||
import { | ||
DatePicker, | ||
DatePickerInput | ||
} from '@carbon/react'; | ||
import { commonSlots, slotsDisabled } from '../common-slots'; | ||
import { SendSignal } from '../types'; | ||
|
||
export interface DatePickerState { | ||
type: string; | ||
placeholder: string; | ||
disabled?: boolean; | ||
id: string; | ||
invalid?: boolean; | ||
invalidText?: string; | ||
rangeInvalidText?: string; | ||
light?: boolean; | ||
size?: string; | ||
datePickerType?: string; | ||
dateFormat?: string; | ||
value?: string; | ||
rangeStartLabel?: string; | ||
rangeEndLabel?: string; | ||
} | ||
|
||
export const type = 'date-picker'; | ||
|
||
export const slots = { | ||
...commonSlots, | ||
...slotsDisabled, | ||
dateFormat: 'string', | ||
datePickerType: 'string', | ||
placeholder: 'string', | ||
size: 'string', | ||
invalidText: 'string', | ||
rangeStartLabel: 'string', | ||
rangeEndLabel: 'string', | ||
rangeInvalidText: 'string', | ||
value: 'string', | ||
invalid: 'boolean', | ||
setInvalid: (state: DatePickerState) => ({ | ||
jz5426 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
...state, | ||
invalid: true | ||
}), | ||
setValid: (state: DatePickerState) => ({ | ||
...state, | ||
invalid: false | ||
}), | ||
toggleValid: (state: DatePickerState) => ({ | ||
...state, | ||
invalid: !state.invalid | ||
}), | ||
light: 'boolean', | ||
setLight: (state: DatePickerState) => ({ | ||
...state, | ||
light: true | ||
}), | ||
setDark: (state: DatePickerState) => ({ | ||
...state, | ||
light: false | ||
}), | ||
toggleLight: (state: DatePickerState) => ({ | ||
...state, | ||
light: !state.light | ||
}) | ||
}; | ||
|
||
export const signals = ['valueChange', 'click']; | ||
|
||
export const UIDatePicker = ({ state, sendSignal }: { | ||
state: DatePickerState; | ||
setState: (state: any) => void; | ||
setGlobalState: (state: any) => void; | ||
sendSignal: SendSignal; | ||
}) => { | ||
if (state.type !== 'date-picker') { | ||
// eslint-disable-next-line react/jsx-no-useless-fragment | ||
return <></>; | ||
} | ||
return <DatePicker | ||
id={state.id} | ||
dateFormat={state.dateFormat} | ||
datePickerType={state.datePickerType} | ||
value={state.value} | ||
light={state.light} | ||
onClick={() => sendSignal(state.id, 'click')} | ||
onChange={(event: any) => { | ||
sendSignal(state.id, 'valueChange', [event[0].toISOString()], { ...state, value: event[0].toISOString() }); | ||
}}> | ||
<DatePickerInput | ||
id={`${state.id} + '-start'`} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't need the "-start", do we? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, we need something to differentiate the start date and end date if type is selecting a date range instead of a single date
jz5426 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
placeholder={state.placeholder} | ||
labelText={state.rangeStartLabel} | ||
size={state.size} | ||
disabled={state.disabled} | ||
invalid={state.invalid} | ||
invalidText={state.invalidText}/> | ||
{ | ||
state.datePickerType === 'range' && | ||
<DatePickerInput | ||
id={`${state.id} + '-end'`} | ||
jz5426 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
placeholder={state.placeholder} | ||
labelText={state.rangeEndLabel} | ||
size={state.size} | ||
disabled={state.disabled} | ||
invalid={state.invalid} | ||
invalidText={state.rangeInvalidText} /> | ||
} | ||
</DatePicker>; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
optional