Skip to content

Commit 1046a4d

Browse files
pivotal-james-zchengarthurdenner
authored andcommitted
feat: allow selection through datepicker only (#134)
* Add support for date picker only mode Signed-off-by: Jinjia Lin <lin_jinjia@edb.gov.sg> * Add datePickerOnly to ownProps in readme. Update tests according to pr comments Signed-off-by: James Cheng <zcheng@pivotal.io>
1 parent 7071f30 commit 1046a4d

File tree

6 files changed

+68
-1
lines changed

6 files changed

+68
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ More examples [here](https://react-semantic-ui-datepickers.now.sh).
7878
| onChange | function | no | () => {} | Callback fired when the value changes |
7979
| pointing | string | no | 'left' | Location to render the component around input. Available options: 'left', 'right', 'top left', 'top right' |
8080
| type | string | no | basic | Type of input to render. Available options: 'basic' and 'range' |
81+
| datePickerOnly | boolean | no | false | Allows the date to be selected only via the date picker, and disables the text input |
8182

8283
### Form.Input Props
8384

src/__tests__/datepicker.test.tsx

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ const setup = (props?: Partial<SemanticDatepickerProps>) => {
2020
options.rerender(
2121
<DatePicker onChange={jest.fn()} {...props} {...newProps} />
2222
),
23+
datePickerInput: options.getByTestId('datepicker-input')
24+
.firstChild as HTMLInputElement,
2325
};
2426
};
2527

@@ -28,6 +30,54 @@ describe('Basic datepicker', () => {
2830
expect(setup()).toBeTruthy();
2931
});
3032

33+
describe('not read only or date picker only', () => {
34+
it('accepts input', async () => {
35+
const { datePickerInput } = setup();
36+
fireEvent.input(datePickerInput, { target: { value: '23' } });
37+
38+
expect(datePickerInput.value).toBe('23');
39+
});
40+
41+
it('opens date picker', async () => {
42+
const { getByTestId, openDatePicker } = setup();
43+
openDatePicker();
44+
45+
expect(getByTestId('datepicker-today-button')).toBeDefined();
46+
});
47+
});
48+
49+
describe('is read only', () => {
50+
it('does not accept input', async () => {
51+
const { datePickerInput } = setup({ readOnly: true });
52+
53+
expect(datePickerInput.readOnly).toBeTruthy();
54+
});
55+
56+
it('does not open date picker', async () => {
57+
const { queryByTestId, openDatePicker } = setup({ readOnly: true });
58+
openDatePicker();
59+
60+
expect(queryByTestId('datepicker-today-button')).toBeNull();
61+
});
62+
});
63+
64+
describe('is date picker only', () => {
65+
it('does not accept input', async () => {
66+
const { getByTestId } = setup({ readOnly: true });
67+
const datePickerInput = getByTestId('datepicker-input')
68+
.firstChild as HTMLInputElement;
69+
70+
expect(datePickerInput.readOnly).toBeTruthy();
71+
});
72+
73+
it('opens date picker', async () => {
74+
const { getByTestId, openDatePicker } = setup();
75+
openDatePicker();
76+
77+
expect(getByTestId('datepicker-today-button')).toBeDefined();
78+
});
79+
});
80+
3181
it('updates the locale if the prop changes', async () => {
3282
const { getByTestId, openDatePicker, rerender } = setup();
3383

src/components/input.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const CustomInput = ({
1414
...rest
1515
}: InputProps) => (
1616
<Form.Input
17+
data-testid="datepicker-input"
1718
{...rest}
1819
icon={
1920
<Icon

src/index.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ class SemanticDatepicker extends React.Component<
8383
placeholder: null,
8484
pointing: 'left',
8585
readOnly: false,
86+
datePickerOnly: false,
8687
required: false,
8788
showOutsideDays: false,
8889
type: 'basic',
@@ -369,7 +370,13 @@ class SemanticDatepicker extends React.Component<
369370
selectedDateFormatted,
370371
typedValue,
371372
} = this.state;
372-
const { clearable, pointing, filterDate, readOnly } = this.props;
373+
const {
374+
clearable,
375+
pointing,
376+
filterDate,
377+
readOnly,
378+
datePickerOnly,
379+
} = this.props;
373380

374381
return (
375382
<div className="field" style={style} ref={this.el}>
@@ -382,6 +389,7 @@ class SemanticDatepicker extends React.Component<
382389
onClick={readOnly ? null : this.showCalendar}
383390
onKeyDown={this.handleKeyDown}
384391
value={typedValue || selectedDateFormatted}
392+
readOnly={readOnly || datePickerOnly}
385393
/>
386394
{isVisible && (
387395
<this.Component

src/types/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ export type SemanticDatepickerProps = PickedDayzedProps &
6969
) => void;
7070
pointing: 'left' | 'right' | 'top left' | 'top right';
7171
type: 'basic' | 'range';
72+
datePickerOnly: boolean;
7273
value: DayzedProps['selected'];
7374
};
7475

stories/basic.stories.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ export const withReadOnly = () => (
2828
</Content>
2929
);
3030

31+
export const withDatePickerOnly = () => (
32+
<Content>
33+
<SemanticDatepicker onChange={action('selected date')} datePickerOnly />
34+
</Content>
35+
);
36+
3137
export const withoutClearOnSameDateClick = () => (
3238
<Content>
3339
<SemanticDatepicker

0 commit comments

Comments
 (0)