Skip to content

Commit

Permalink
feat(date-field): customize format (#252)
Browse files Browse the repository at this point in the history
* feat(date-field): customize format

* test(date-field): custom format
  • Loading branch information
redgeoff authored Feb 22, 2021
1 parent 5361e49 commit 2ef4301
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/demo/components/fields.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ const fields = {
minDate: '2018-01-01T23:07:28.157Z',
maxDate: '2022-01-01T23:07:28.157Z',
required: true,
// format: 'yyyy-MM-dd'
},

{
Expand Down
4 changes: 3 additions & 1 deletion src/fields/date-field.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class DateField extends React.PureComponent {
fullWidth,
disabled,
accessEditable,
format,
} = this.props;

const dis = accessEditable === false || disabled;
Expand Down Expand Up @@ -67,7 +68,7 @@ class DateField extends React.PureComponent {
fullWidth={fullWidth}
disabled={dis}
id={component.getUniqueId()}
// format="M/d/YYYY h:m a"
format={format}
/>
</span>
</MuiPickersUtilsProvider>
Expand Down Expand Up @@ -99,4 +100,5 @@ export default attach([
'maxDate',
'fullWidth',
'disabled',
'format',
])(DateField);
20 changes: 18 additions & 2 deletions src/fields/date-field.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { fireEvent } from '@testing-library/react';
import { compileAndRender } from '../test-utils';
import format from 'date-fns/format';

const definition = {
component: 'DateField',
Expand All @@ -8,7 +9,7 @@ const definition = {
help: 'Date help',
};

it('should pick date', async () => {
const shouldPickDate = (definition, expectedValue) => {
const { getByLabelText, getByRole } = compileAndRender(definition);

// Click text field--this will launch the picker
Expand All @@ -24,8 +25,23 @@ it('should pick date', async () => {
fireEvent.click(ok);

// Verify that the input has been updated accordingly
expect(textField).toHaveValue(expectedValue);
};

it('should pick date', async () => {
const month = new Date().toLocaleString('default', { month: 'long' });
expect(textField).toHaveValue(`${month} 17th`);
shouldPickDate(definition, `${month} 17th`);
});

it('should pick date with custom format', async () => {
const yearMonth = format(new Date(), 'yyyy-MM');
shouldPickDate(
{
...definition,
format: 'yyyy-MM-dd',
},
`${yearMonth}-17`
);
});

it('should initialize picker', async () => {
Expand Down

0 comments on commit 2ef4301

Please sign in to comment.