From fbf001431f6c35b3a011cbf71aa208a2692d787d Mon Sep 17 00:00:00 2001 From: Dave MacFarlane Date: Fri, 24 Feb 2023 09:39:17 -0500 Subject: [PATCH] [Form.js] Add DateTimeElement wrapper This adds a DateTimeElement wrapper to our form classes for use cases where a datetime needs to be selected. It is a wrapper around the datetime-local input type. --- jsx/Form.js | 100 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 99 insertions(+), 1 deletion(-) diff --git a/jsx/Form.js b/jsx/Form.js index c00beb805cc..3d1fdf3f794 100644 --- a/jsx/Form.js +++ b/jsx/Form.js @@ -1,5 +1,5 @@ /* exported FormElement, FieldsetElement, SelectElement, TagsElement, SearchableDropdown, TextareaElement, -TextboxElement, PasswordElement, DateElement, NumericElement, FileElement, StaticElement, HeaderElement, LinkElement, +TextboxElement, PasswordElement, DateElement, DateTimeElement, NumericElement, FileElement, StaticElement, HeaderElement, LinkElement, CheckboxElement, ButtonElement, LorisElement */ @@ -1784,6 +1784,102 @@ TimeElement.defaultProps = { }, }; +/** + * DateTime Component + * React wrapper for a element. + */ +class DateTimeElement extends Component { + /** + * @constructor + * @param {object} props - React Component properties + */ + constructor(props) { + super(props); + + this.handleChange = this.handleChange.bind(this); + } + + /** + * Handle change + * + * @param {object} e - Event + */ + handleChange(e) { + this.props.onUserInput(this.props.name, e.target.value); + } + + /** + * Renders the React component. + * + * @return {JSX} - React markup for the component + */ + render() { + let disabled = this.props.disabled ? 'disabled' : null; + let required = this.props.required ? 'required' : null; + let requiredHTML = null; + let label; + let classSz; + + // Add required asterix + if (required) { + requiredHTML = *; + } + if (this.props.label) { + label = ; + classSz = 'col-sm-9'; + } else { + classSz = 'col-sm-12'; + } + + return ( +
+ {label} +
+ +
+
+ ); + } +} + +DateTimeElement.propTypes = { + name: PropTypes.string.isRequired, + label: PropTypes.string, + value: PropTypes.string, + id: PropTypes.string, + disabled: PropTypes.bool, + required: PropTypes.bool, + onUserInput: PropTypes.func, +}; + +DateTimeElement.defaultProps = { + name: '', + label: '', + value: '', + id: '', + disabled: false, + required: false, + onUserInput: function() { + console.warn('onUserInput() callback is not set'); + }, +}; + /** * Numeric Component * React wrapper for a element. @@ -2828,6 +2924,7 @@ window.EmailElement = EmailElement; window.PasswordElement = PasswordElement; window.DateElement = DateElement; window.TimeElement = TimeElement; +window.DateTimeElement = DateTimeElement; window.NumericElement = NumericElement; window.FileElement = FileElement; window.StaticElement = StaticElement; @@ -2851,6 +2948,7 @@ export default { PasswordElement, DateElement, TimeElement, + DateTimeElement, NumericElement, FileElement, StaticElement,