Skip to content

Commit

Permalink
Custom toolbar component (mui#1182)
Browse files Browse the repository at this point in the history
* Add new ToolbarComponent prop for custom toolbars

* Update props docs

* Run docgen on precommit hook

* Run docgen on precommit hook

* Run git add after precommit command

* Automatically format json before prettier
  • Loading branch information
dmtrKovalenko committed Jul 15, 2019
1 parent 646db90 commit a9067fd
Show file tree
Hide file tree
Showing 11 changed files with 1,112 additions and 376 deletions.
1,436 changes: 1,075 additions & 361 deletions docs/prop-types.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/scripts/docgen.js
Expand Up @@ -85,4 +85,4 @@ components.forEach(filePart => {
});
});

fs.writeFileSync(path.resolve(__dirname, '..', 'prop-types.json'), JSON.stringify(doc));
fs.writeFileSync(path.resolve(__dirname, '..', 'prop-types.json'), JSON.stringify(doc, null, 2));
6 changes: 3 additions & 3 deletions lib/src/DatePicker/DatePicker.tsx
Expand Up @@ -38,7 +38,7 @@ export interface BaseDatePickerProps extends OutterCalendarProps {
*/
disableFuture?: boolean;
/**
* To animate scrolling to current year (with scrollIntoView)
* To animate scrolling to current year (using scrollIntoView)
* @default false
*/
animateYearScrolling?: boolean;
Expand Down Expand Up @@ -76,12 +76,12 @@ function useOptions(props: DatePickerViewsProps) {

export const DatePicker = makePurePicker<DatePickerViewsProps>({
useOptions,
ToolbarComponent: DatePickerToolbar,
DefaultToolbarComponent: DatePickerToolbar,
});

export const KeyboardDatePicker = makeKeyboardPicker<DatePickerViewsProps>({
useOptions,
ToolbarComponent: DatePickerToolbar,
DefaultToolbarComponent: DatePickerToolbar,
});

DatePicker.defaultProps = defaultProps;
Expand Down
4 changes: 2 additions & 2 deletions lib/src/DateTimePicker/DateTimePicker.tsx
Expand Up @@ -54,12 +54,12 @@ function useOptions(props: DateTimePickerProps | KeyboardDateTimePickerProps) {

export const DateTimePicker = makePurePicker<DateTimePickerProps>({
useOptions,
ToolbarComponent: DateTimePickerToolbar,
DefaultToolbarComponent: DateTimePickerToolbar,
});

export const KeyboardDateTimePicker = makeKeyboardPicker<KeyboardDateTimePickerProps>({
useOptions,
ToolbarComponent: DateTimePickerToolbar,
DefaultToolbarComponent: DateTimePickerToolbar,
});

DateTimePicker.defaultProps = defaultProps;
Expand Down
5 changes: 3 additions & 2 deletions lib/src/Picker/WrappedKeyboardPicker.tsx
Expand Up @@ -15,14 +15,14 @@ export type WrappedKeyboardPickerProps = DateValidationProps &

export interface MakePickerOptions {
useOptions: (props: any) => StateHookOptions;
ToolbarComponent: React.ComponentType<ToolbarComponentProps>;
DefaultToolbarComponent: React.ComponentType<ToolbarComponentProps>;
}

// Mostly duplicate of ./WrappedPurePicker.tsx to enable tree-shaking of keyboard logic
// TODO investigate how to reduce duplications
export function makeKeyboardPicker<T extends any>({
useOptions,
ToolbarComponent,
DefaultToolbarComponent,
}: MakePickerOptions): React.FC<WrappedKeyboardPickerProps & T> {
function WrappedKeyboardPicker(props: WrappedKeyboardPickerProps & T) {
const {
Expand Down Expand Up @@ -67,6 +67,7 @@ export function makeKeyboardPicker<T extends any>({
variant,
disableToolbar,
loadingIndicator,
ToolbarComponent = DefaultToolbarComponent,
...other
} = props;

Expand Down
3 changes: 2 additions & 1 deletion lib/src/Picker/WrappedPurePicker.tsx
Expand Up @@ -13,7 +13,7 @@ export type WrappedPurePickerProps = DateValidationProps &

export function makePurePicker<T extends any>({
useOptions,
ToolbarComponent,
DefaultToolbarComponent,
}: MakePickerOptions): React.FC<WrappedPurePickerProps & T> {
function WrappedPurePicker(props: WrappedPurePickerProps & T) {
const {
Expand Down Expand Up @@ -58,6 +58,7 @@ export function makePurePicker<T extends any>({
orientation,
disableToolbar,
loadingIndicator,
ToolbarComponent = DefaultToolbarComponent,
...other
} = props;

Expand Down
4 changes: 2 additions & 2 deletions lib/src/TimePicker/TimePicker.tsx
Expand Up @@ -51,12 +51,12 @@ function useOptions(props: TimePickerProps | KeyboardTimePickerProps) {

export const TimePicker = makePurePicker<TimePickerViewsProps>({
useOptions,
ToolbarComponent: TimePickerToolbar,
DefaultToolbarComponent: TimePickerToolbar,
});

export const KeyboardTimePicker = makeKeyboardPicker<TimePickerViewsProps>({
useOptions,
ToolbarComponent: TimePickerToolbar,
DefaultToolbarComponent: TimePickerToolbar,
});

TimePicker.defaultProps = defaultProps;
Expand Down
13 changes: 13 additions & 0 deletions lib/src/__tests__/e2e/DatePicker.test.tsx
Expand Up @@ -218,3 +218,16 @@ describe('e2e - DatePicker month change async', () => {
).toEqual(0);
});
});

test('Custom toolbar component', () => {
const component = mount(
<DatePicker
open
value={new Date()}
onChange={jest.fn()}
ToolbarComponent={() => <div id="custom-toolbar" />}
/>
);

expect(component.find('#custom-toolbar').length).toBe(1);
});
3 changes: 3 additions & 0 deletions lib/src/typings/BasePicker.tsx
@@ -1,6 +1,7 @@
import { MaterialUiPickersDate } from './date';
import { WrapperVariant } from '../wrappers/Wrapper';
import { ParsableDate } from '../constants/prop-types';
import { ToolbarComponentProps } from '../Picker/Picker';

export interface BasePickerProps {
/** Picker value */
Expand Down Expand Up @@ -50,6 +51,8 @@ export interface BasePickerProps {
* @default "portrait"
*/
orientation?: 'portrait' | 'landscape';
/* Component that will replace default toolbar renderer */
ToolbarComponent?: React.ComponentType<ToolbarComponentProps>;
variant?: WrapperVariant;
mergePreviousDateOnChange?: boolean;
forwardedRef?: any;
Expand Down
8 changes: 4 additions & 4 deletions lib/src/wrappers/ModalWrapper.tsx
Expand Up @@ -9,22 +9,22 @@ import { DialogProps as MuiDialogProps } from '@material-ui/core/Dialog';
export interface ModalWrapperProps<T = {}> extends WrapperProps<T> {
/**
* "OK" label message
* @default 'OK'
* @default "OK"
*/
okLabel?: React.ReactNode;
/**
* "CANCEL" label message
* @default 'CANCEL'
* @default "CANCEL"
*/
cancelLabel?: React.ReactNode;
/**
* "CLEAR" label message
* @default 'CLEAR'
* @default "CLEAR"
*/
clearLabel?: React.ReactNode;
/**
* "TODAY" label message
* @default 'TODAY'
* @default "TODAY"
*/
todayLabel?: React.ReactNode;
/**
Expand Down
4 changes: 4 additions & 0 deletions package.json
Expand Up @@ -43,6 +43,10 @@
}
},
"lint-staged": {
"lib/**/*.{ts,tsx}": [
"yarn docgen",
"git add"
],
"*.{js,jsx,ts,tsx,json,css,md}": [
"prettier --write",
"git add"
Expand Down

0 comments on commit a9067fd

Please sign in to comment.