Skip to content
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

Time picker - New states and icons #13851

Merged
merged 8 commits into from
May 31, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8596,6 +8596,12 @@ Map {
"value": Object {
"type": "string",
},
"warning": Object {
"type": "bool",
},
"warningText": Object {
"type": "node",
},
},
"render": [Function],
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,15 @@ Playground.argTypes = {
invalidText: {
control: { type: 'text' },
},
warning: {
control: {
type: 'boolean',
},
defaultValue: false,
},
warningText: {
control: { type: 'text' },
},
labelText: {
control: { type: 'text' },
},
Expand Down
63 changes: 58 additions & 5 deletions packages/react/src/components/TimePicker/TimePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@

import cx from 'classnames';
import PropTypes from 'prop-types';
import React from 'react';
import React, { useEffect } from 'react';
import { usePrefix } from '../../internal/usePrefix';
import deprecate from '../../prop-types/deprecate';
import { ForwardRefReturn, ReactAttr } from '../../types/common';
// @ts-ignore
import { WarningFilled, WarningAltFilled } from '@carbon/icons-react';

type ExcludedAttributes = 'id' | 'value';

Expand Down Expand Up @@ -51,6 +53,16 @@ export interface TimePickerProps
*/
invalidText?: React.ReactNode;

/**
* Specify a warning message
*/
warning?: boolean;

/**
* Provide the text that is displayed when the control is in an warning state
*/
warningText?: React.ReactNode;

/**
* Provide the text that will be read by a screen reader when visiting this
* control
Expand Down Expand Up @@ -135,6 +147,8 @@ const TimePicker: TimePickerComponent = React.forwardRef<
id,
invalidText = 'Invalid time format.',
invalid = false,
warningText = 'Warning message.',
warning = false,
labelText,
light = false,
maxLength = 5,
Expand Down Expand Up @@ -186,6 +200,17 @@ const TimePicker: TimePickerComponent = React.forwardRef<
}
}

useEffect(() => {
const inputField = document.querySelector(
`.${prefix}--time-picker__input-field`
);
if (invalid || warning) {
inputField?.classList.add(`${prefix}--time-picker__input-field-error`);
} else {
inputField?.classList.remove(`${prefix}--time-picker__input-field-error`);
}
}, [invalid, warning, prefix]);

const timePickerInputClasses = cx(
`${prefix}--time-picker__input-field`,
`${prefix}--text-input`,
Expand All @@ -199,6 +224,7 @@ const TimePicker: TimePickerComponent = React.forwardRef<
[`${prefix}--time-picker`]: true,
[`${prefix}--time-picker--light`]: light,
[`${prefix}--time-picker--invalid`]: invalid,
[`${prefix}--time-picker--warning`]: warning,
[`${prefix}--time-picker--readonly`]: readOnly,
[`${prefix}--time-picker--${size}`]: size,
...(className && { [className]: true }),
Expand All @@ -215,9 +241,7 @@ const TimePicker: TimePickerComponent = React.forwardRef<
</label>
) : null;

const error = invalid ? (
<div className={`${prefix}--form-requirement`}>{invalidText}</div>
) : null;
const error = invalid || warning;

function getInternalPickerSelects() {
const readOnlyEventHandlers = {
Expand Down Expand Up @@ -278,10 +302,29 @@ const TimePicker: TimePickerComponent = React.forwardRef<
{...rest}
{...readOnlyProps}
/>
{error ? (
<div className={`${prefix}--time-picker__error__icon`}>
{invalid ? (
<WarningFilled
className={`${prefix}--checkbox__invalid-icon`}
size={16}
/>
) : (
<WarningAltFilled
className={`${prefix}--text-input__invalid-icon--warning`}
size={16}
/>
)}
</div>
) : null}
</div>
{getInternalPickerSelects()}
</div>
{error}
{error && (
<div className={`${prefix}--form-requirement`}>
{invalid ? invalidText : warningText}
</div>
)}
</div>
);
});
Expand Down Expand Up @@ -388,6 +431,16 @@ TimePicker.propTypes = {
* Specify the value of the `<input>`
*/
value: PropTypes.string,

/**
* Specify a warning message
*/
warning: PropTypes.bool,

/**
* Provide the text that is displayed when the control is in an warning state
*/
warningText: PropTypes.node,
};

export default TimePicker;
1 change: 1 addition & 0 deletions packages/styles/scss/components/form/_form.scss
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ $input-label-weight: 400 !default;
.#{$prefix}--date-picker-input__wrapper--warn,
.#{$prefix}--date-picker-input__wrapper--invalid,
.#{$prefix}--time-picker--invalid,
.#{$prefix}--time-picker--warning,
.#{$prefix}--text-input__field-wrapper[data-invalid],
.#{$prefix}--text-input__field-wrapper--warning,
.#{$prefix}--text-input__field-wrapper--warning > .#{$prefix}--text-input,
Expand Down
16 changes: 16 additions & 0 deletions packages/styles/scss/components/time-picker/_time-picker.scss
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,22 @@
}

.#{$prefix}--time-picker__input {
position: relative;
display: flex;
flex-direction: column;
}

.#{$prefix}--time-picker__error__icon {
position: absolute;
top: 50%;
right: 1rem;
display: flex;
height: 100%;
place-items: center;
// top/transform used to center invalid icon in IE11
transform: translateY(-50%);
Comment on lines +47 to +48
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IE11 isn't in our support matrix as of v11

}

.#{$prefix}--time-picker .#{$prefix}--select-input {
width: auto;
min-width: auto;
Expand All @@ -60,6 +72,10 @@
}
}

.#{$prefix}--time-picker__input-field-error {
width: 6.175rem;
}

// V11: Possibly deprecate
.#{$prefix}--time-picker--light .#{$prefix}--select-input {
background-color: $field-02;
Expand Down