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

[Backport 1.x] Add compressed styling to OuiDatePicker and OuiDatePickerRange #1383

Merged
merged 1 commit into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion src-docs/src/views/date_picker/date_picker.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export default () => {
};

return (
<OuiFormRow label="Select a date">
<OuiFormRow label="Select a date" fullWidth>
<OuiDatePicker selected={startDate} onChange={handleChange} />
</OuiFormRow>
);
Expand Down
3 changes: 3 additions & 0 deletions src-docs/src/views/date_picker/date_picker_example.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import {
OuiDatePickerRange,
} from '../../../../src/components';

import { datePickerConfig } from './playground';

import DatePicker from './date_picker';
const datePickerSource = require('!!raw-loader!./date_picker');
const datePickerHtml = renderToHtml(DatePicker);
Expand Down Expand Up @@ -231,6 +233,7 @@ export const DatePickerExample = {
snippet: datePickerSnippet,
demo: <DatePicker />,
props: { OuiDatePicker },
playground: datePickerConfig,
},
{
title: 'Date picker states',
Expand Down
73 changes: 73 additions & 0 deletions src-docs/src/views/date_picker/playground.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*
* Modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/

import { PropTypes } from 'react-view';
import { OuiDatePicker } from '../../../../src/components/';
import {
propUtilityForPlayground,
iconValidator,
} from '../../services/playground';
import moment from 'moment';

export const datePickerConfig = () => {
const docgenInfo = Array.isArray(OuiDatePicker.__docgenInfo)
? OuiDatePicker.__docgenInfo[0]
: OuiDatePicker.__docgenInfo;
const propsToUse = propUtilityForPlayground(docgenInfo.props);

propsToUse.dateFormat = {
...propsToUse.dateFormat,
type: PropTypes.String,
};

propsToUse.selected = {
...propsToUse.selected,
type: PropTypes.Object,
value: 'moment()',
stateful: true,
};

propsToUse.onChange = {
...propsToUse.onChange,
type: PropTypes.Function,
value: 'selected => setSelected(selected)',
propHook: {
what: 'selected',
into: 'selected',
},
};

propsToUse.iconType = iconValidator(propsToUse.iconType);

propsToUse.autoComplete = {
...propsToUse.autoComplete,
hidden: true,
};

return {
config: {
componentName: 'OuiDatePicker',
props: propsToUse,
scope: {
OuiDatePicker,
moment,
},
imports: {
'@opensearch-project/oui': {
named: ['OuiDatePicker'],
},
moment: {
default: 'moment',
},
},
},
};
};
51 changes: 27 additions & 24 deletions src-docs/src/views/date_picker/range.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,35 +14,38 @@ import React, { useState } from 'react';
import moment from 'moment';

import { OuiDatePicker, OuiDatePickerRange } from '../../../../src/components';
import { DisplayToggles } from '../form_controls/display_toggles';

export default () => {
const [startDate, setStartDate] = useState(moment());
const [endDate, setEndDate] = useState(moment().add(11, 'd'));

return (
<OuiDatePickerRange
startDateControl={
<OuiDatePicker
selected={startDate}
onChange={setStartDate}
startDate={startDate}
endDate={endDate}
isInvalid={startDate > endDate}
aria-label="Start date"
showTimeSelect
/>
}
endDateControl={
<OuiDatePicker
selected={endDate}
onChange={setEndDate}
startDate={startDate}
endDate={endDate}
isInvalid={startDate > endDate}
aria-label="End date"
showTimeSelect
/>
}
/>
<DisplayToggles canDisabled={false} canLoading={false} canInvalid={false}>
<OuiDatePickerRange
startDateControl={
<OuiDatePicker
selected={startDate}
onChange={setStartDate}
startDate={startDate}
endDate={endDate}
isInvalid={startDate > endDate}
aria-label="Start date"
showTimeSelect
/>
}
endDateControl={
<OuiDatePicker
selected={endDate}
onChange={setEndDate}
startDate={startDate}
endDate={endDate}
isInvalid={startDate > endDate}
aria-label="End date"
showTimeSelect
/>
}
/>
</DisplayToggles>
);
};
2 changes: 1 addition & 1 deletion src-docs/src/views/date_picker/states.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export default () => {
return (
/* DisplayToggles wrapper for Docs only */
<div>
<DisplayToggles canCompressed={false}>
<DisplayToggles>
<OuiDatePicker
showTimeSelect
selected={startDate}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -805,9 +805,11 @@ exports[`OuiDatePicker localization inherits locale from context 1`] = `

exports[`OuiDatePicker popoverPlacement top-end is rendered 1`] = `
<OuiDatePicker
accessibleMode={true}
adjustDateOnChange={true}
aria-label="aria-label"
className="testClass1 testClass2"
compressed={false}
data-test-subj="test subject string"
dateFormat="MM/DD/YYYY"
fullWidth={false}
Expand All @@ -817,8 +819,11 @@ exports[`OuiDatePicker popoverPlacement top-end is rendered 1`] = `
shadow={true}
shouldCloseOnSelect={true}
showIcon={true}
showMonthDropdown={true}
showTimeSelect={false}
showYearDropdown={true}
timeFormat="hh:mm A"
yearDropdownItemNumber={7}
>
<span>
<span
Expand Down
5 changes: 5 additions & 0 deletions src/components/date_picker/_date_picker_range.scss
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@

.ouiDatePickerRange {
@include ouiFormControlSize(auto, $includeAlternates: true);

.ouiDatePicker.ouiFieldText--compressed {
height: $ouiFormControlLayoutGroupInputCompressedHeight;
}

// Match just the regular drop shadow of inputs
@include ouiFormControlDefaultShadow();
display: flex;
Expand Down
37 changes: 36 additions & 1 deletion src/components/date_picker/date_picker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,27 @@ interface OuiExtendedDatePickerProps extends ReactDatePickerProps {
* Sets the placement of the popover. It accepts: `"bottom"`, `"bottom-end"`, `"bottom-start"`, `"left"`, `"left-end"`, `"left-start"`, `"right"`, `"right-end"`, `"right-start"`, `"top"`, `"top-end"`, `"top-start"`
*/
popoverPlacement?: ReactDatePickerProps['popperPlacement'];

/**
* Reduces the size to a typical (compressed) input
*/
compressed?: boolean;
}

type _OuiDatePickerProps = CommonProps & OuiExtendedDatePickerProps;
type _OuiDatePickerProps = CommonProps &
Omit<
OuiExtendedDatePickerProps,
| 'monthsShown'
| 'showWeekNumbers'
| 'fixedHeight'
| 'dropdownMode'
| 'useShortMonthInDropdown'
| 'todayButton'
| 'timeCaption'
| 'disabledKeyboardNavigation'
| 'isClearable'
| 'withPortal'
>;

export type OuiDatePickerProps = ApplyClassComponentDefaults<
typeof OuiDatePicker
Expand All @@ -127,10 +145,15 @@ export class OuiDatePicker extends Component<_OuiDatePickerProps> {
isLoading: false,
shadow: true,
shouldCloseOnSelect: true,
showMonthDropdown: true,
showYearDropdown: true,
yearDropdownItemNumber: 7,
accessibleMode: true,
showIcon: true,
showTimeSelect: false,
timeFormat: ouiDatePickerDefaultTimeFormat,
popoverPlacement: 'bottom-start',
compressed: false,
};

render() {
Expand Down Expand Up @@ -170,6 +193,7 @@ export class OuiDatePicker extends Component<_OuiDatePickerProps> {
showTimeSelectOnly,
timeFormat,
utcOffset,
compressed,
...rest
} = this.props;

Expand All @@ -186,6 +210,7 @@ export class OuiDatePicker extends Component<_OuiDatePickerProps> {
'ouiFieldText-isLoading': isLoading,
'ouiFieldText--withIcon': !inline && showIcon,
'ouiFieldText-isInvalid': isInvalid,
'ouiFieldText--compressed': compressed,
},
className
);
Expand Down Expand Up @@ -216,24 +241,34 @@ export class OuiDatePicker extends Component<_OuiDatePickerProps> {
};

if (
// @ts-ignore for guard against omitted prop
// We don't want to show multiple months next to each other
this.props.monthsShown ||
// @ts-ignore for guard against omitted prop
// There is no need to show week numbers
this.props.showWeekNumbers ||
// @ts-ignore for guard against omitted prop
// Our css adapts to height, no need to fix it
this.props.fixedHeight ||
// @ts-ignore for guard against omitted prop
// We force the month / year selection UI. No need to configure it
this.props.dropdownMode ||
// @ts-ignore for guard against omitted prop
// Short month is unnecessary. Our UI has plenty of room for full months
this.props.useShortMonthInDropdown ||
// @ts-ignore for guard against omitted prop
// The today button is not needed. This should always be external to the calendar
this.props.todayButton ||
// @ts-ignore for guard against omitted prop
// We hide the time caption, so there is no need to overwrite its text
this.props.timeCaption ||
// @ts-ignore for guard against omitted prop
// We always want keyboard accessibility on
this.props.disabledKeyboardNavigation ||
// @ts-ignore for guard against omitted prop
// This is easy enough to do. It can conflict with isLoading state
this.props.isClearable ||
// @ts-ignore for guard against omitted prop
// There is no reason to launch the datepicker in its own modal. Can always build these ourselves
this.props.withPortal
) {
Expand Down
2 changes: 2 additions & 0 deletions src/components/date_picker/date_picker_range.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ export const OuiDatePickerRange: FunctionComponent<OuiDatePickerRangeProps> = ({
readOnly: readOnly,
iconType: typeof iconType === 'boolean' ? undefined : iconType,
showIcon: !!iconType,
compressed,
}
);

Expand All @@ -119,6 +120,7 @@ export const OuiDatePickerRange: FunctionComponent<OuiDatePickerRangeProps> = ({
fullWidth: fullWidth,
readOnly: readOnly,
popoverPlacement: 'bottom-end',
compressed,
}
);
}
Expand Down
Loading