Skip to content

Commit

Permalink
bugfix: Remove duplicated apply button in DateInput & handle onChange…
Browse files Browse the repository at this point in the history
… on date selection directly

BUGFIX: Remove duplicated apply button in DateInput & handle onChange on date selection directly
  • Loading branch information
Christian Pansch committed Mar 14, 2024
1 parent 5245d97 commit fd57e26
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 61 deletions.
1 change: 0 additions & 1 deletion packages/neos-ui-editors/src/Editors/DateTime/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ class DateTime extends PureComponent {
timeOnly={!hasDateFormat(options.format)}
placeholder={i18nRegistry.translate($get('placeholder', options) || 'Neos.Neos:Main:content.inspector.editors.dateTimeEditor.noDateSet')}
todayLabel={i18nRegistry.translate('content.inspector.editors.dateTimeEditor.today', 'Today', {}, 'Neos.Neos', 'Main')}
applyLabel={i18nRegistry.translate('content.inspector.editors.dateTimeEditor.apply', 'Apply', {}, 'Neos.Neos', 'Main')}
locale={interfaceLanguage}
disabled={options.disabled}
timeConstraints={timeConstraints}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,22 +88,6 @@ exports[`<DateInput/> should render correctly. 1`] = `
timeFormat={true}
utc={false}
/>
<ThemedButton
_refHandler={[Function]}
className="applyBtnClassName"
composeTheme="deeply"
disabled={false}
hoverStyle="brand"
isActive={false}
isFocused={false}
mapThemrProps={[Function]}
onClick={[Function]}
size="regular"
style="brand"
type="button"
>
applyLabel
</ThemedButton>
</UnmountClosed>
</div>
`;
Expand Down Expand Up @@ -196,22 +180,6 @@ exports[`<DateInput/> should set "utc" on DatePickerComponent if "dateOnly" prop
timeFormat={false}
utc={true}
/>
<ThemedButton
_refHandler={[Function]}
className="applyBtnClassName"
composeTheme="deeply"
disabled={false}
hoverStyle="brand"
isActive={false}
isFocused={false}
mapThemrProps={[Function]}
onClick={[Function]}
size="regular"
style="brand"
type="button"
>
applyLabel
</ThemedButton>
</UnmountClosed>
</div>
`;
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@ import toJson from 'enzyme-to-json';
import moment from 'moment';
import DatePicker from 'react-datetime';

import Button from '../Button';
import {DateInput} from './dateInput';

describe('<DateInput/>', () => {
const props: DateInput['props'] = {
onChange: jest.fn(),
locale: 'en-US',
applyLabel: 'applyLabel',
todayLabel: 'todayLabel',
theme: {
'wrapper': 'wrapperClassName',
Expand Down Expand Up @@ -62,15 +60,13 @@ describe('<DateInput/>', () => {
expect(onChange.mock.calls[0][0]).toBe(null);
});

it('should call the "onChange" prop when triggering the change event on the "DatePicker" Component and clicking apply.', () => {
it('should call the "onChange" prop when triggering the change event on the "DatePicker".', () => {
const onChange = jest.fn();
const wrapper = shallow(<DateInput {...props} value={new Date()} onChange={onChange}/>);
const picker = wrapper.find(DatePicker);
const applyButton = wrapper.find(Button);
const newVal = moment();

picker.simulate('change', newVal);
applyButton.simulate('click');

expect(onChange.mock.calls.length).toBe(1);
expect(onChange.mock.calls[0][0].toTimeString()).toBe(newVal.toDate().toTimeString());
Expand Down
27 changes: 4 additions & 23 deletions packages/react-ui-components/src/DateInput/dateInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import Collapse from 'react-collapse';
import DatePicker, {TimeConstraints} from 'react-datetime';

import {PickDefaultProps} from '../utils-typescript';
import Button from '../Button';
import Icon from '../Icon';

// WHY: Because momentJs locales are not bundled automatically, we have to explicitly add them.
Expand Down Expand Up @@ -39,11 +38,6 @@ export interface DateInputProps {
*/
readonly todayLabel: string;

/**
* The label which will be displayed within the `Apply` btn.
*/
readonly applyLabel: string;

/**
* The moment format string to use to format the passed value.
*/
Expand Down Expand Up @@ -137,7 +131,6 @@ export class DateInput extends PureComponent<DateInputProps, DateInputState> {
className,
id,
todayLabel,
applyLabel,
labelFormat,
dateOnly,
timeOnly,
Expand Down Expand Up @@ -223,15 +216,9 @@ export class DateInput extends PureComponent<DateInputProps, DateInputState> {
locale={locale}
timeFormat={!dateOnly}
onChange={this.handleChange}

timeConstraints={this.props.timeConstraints}
/>
<Button
onClick={this.handleApply}
className={theme!.applyBtn}
style="brand"
>
{applyLabel}
</Button>
</Collapse>
</div>
);
Expand All @@ -257,19 +244,13 @@ export class DateInput extends PureComponent<DateInputProps, DateInputState> {
}
}

private readonly handleApply = () => {
this.setState({
isOpen: false
}, () => {
this.props.onChange(this.state.transientDate);
});
}

private readonly handleChange = (value: Moment | string) => {
const momentVal: Moment = isMoment(value) ? value : moment(value);
const selectedDate = momentVal.toDate()
this.setState({
transientDate: momentVal.toDate()
transientDate: selectedDate
});
this.props.onChange(selectedDate);
}

private readonly handleSelectTodayBtnClick = () => {
Expand Down

0 comments on commit fd57e26

Please sign in to comment.