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

Datepicker format handling changes #1504

Merged
merged 3 commits into from
Jan 3, 2023
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
14 changes: 2 additions & 12 deletions packages/toolpad-app/src/runtime/ToolpadApp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -267,22 +267,12 @@ function RenderedNodeContent({ node, childNodeGroups, Component }: RenderedNodeC
const handler = (param: any) => {
const bindingId = `${nodeId}.props.${key}`;

const defaultValues = _.mapValues(argTypes, ({ defaultValue }: any) => defaultValue);
const propsValues = _.mapValues(
(node as appDom.ElementNode).props,
(prop) => prop?.value || undefined,
);
const props = {
...defaultValues,
...propsValues,
};

const value = argType.onChangeHandler ? argType.onChangeHandler(param, props) : param;
const value = argType.onChangeHandler ? argType.onChangeHandler(param) : param;
setControlledBinding(bindingId, { value });
};
return [argType.onChangeProp, handler];
}),
[argTypes, nodeId, setControlledBinding, node],
[argTypes, nodeId, setControlledBinding],
);

const navigateToPage = usePageNavigator();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,11 @@ export default function BindableEditor<V>({
);

const initConstValue = React.useCallback(() => {
let constValue = liveBinding?.value;

if (value?.type === 'const') {
constValue = value.value;
return value.value;
}

return constValue;
return liveBinding?.value;
}, [liveBinding, value]);

const constValue = React.useMemo(initConstValue, [value, initConstValue]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@ const FUTURE_COMPONENTS = new Map<string, FutureComponentSpec>([
['Slider', { url: 'https://github.com/mui/mui-toolpad/issues/746', displayName: 'Slider' }],
['Switch', { url: 'https://github.com/mui/mui-toolpad/issues/745', displayName: 'Switch' }],
['Radio', { url: 'https://github.com/mui/mui-toolpad/issues/744', displayName: 'Radio' }],
[
'DatePicker',
{ url: 'https://github.com/mui/mui-toolpad/issues/743', displayName: 'Date picker' },
],
['Checkbox', { url: 'https://github.com/mui/mui-toolpad/issues/742', displayName: 'Checkbox' }],
]);

Expand Down
27 changes: 12 additions & 15 deletions packages/toolpad-components/src/DatePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,24 @@ import { Dayjs } from 'dayjs';

export interface Props extends DesktopDatePickerProps<string, Dayjs> {
format: string;
separator: string;
fullWidth: boolean;
variant: 'outlined' | 'filled' | 'standard';
size: 'small' | 'medium';
sx: any;
}

const resolveFormat = (format: string, separator: string) => format.replaceAll(' ', separator);

function DatePicker(props: Props) {
const customProps: any = {};

if (props.format) {
// If inputFormat receives undefined prop, datepicker throws error
customProps.inputFormat = props.format;
}

return (
<LocalizationProvider dateAdapter={AdapterDayjs as any}>
<DesktopDatePicker
inputFormat={resolveFormat(props.format, props.separator)}
{...customProps}
{...props}
renderInput={(params) => (
<TextField
Expand All @@ -44,25 +48,18 @@ export default createComponent(DatePicker, {
value: {
typeDef: { type: 'string' },
onChangeProp: 'onChange',
onChangeHandler: (newValue: Dayjs, { format, separator }: Props) => {
return newValue.format(resolveFormat(format, separator));
onChangeHandler: (newValue: Dayjs) => {
// date-only form of ISO8601. See https://tc39.es/ecma262/#sec-date-time-string-format
return newValue.format('YYYY-MM-DD');
bytasv marked this conversation as resolved.
Show resolved Hide resolved
},
defaultValue: '',
defaultValueProp: 'defaultValue',
},
format: {
typeDef: {
type: 'string',
enum: ['DD MM YYYY', 'YYYY MM DD', 'MM DD YYYY'],
},
defaultValue: 'YYYY MM DD',
},
separator: {
typeDef: {
type: 'string',
enum: ['-', '/', ' '],
},
defaultValue: '-',
defaultValue: '',
},
// @ts-ignore no idea why it complains even though it's done exactly same as TextField
defaultValue: {
Expand Down