Skip to content

Commit 2bdaffd

Browse files
lucasvnborgesarthurdenner
authored andcommitted
feat(datepicker): Adding new prop filterDate (#7)
* feat(datepicker): Adding new prop `filterDate` * docs(readme): adding the filterDate prop in the readme documentation * chore(contributors): adding myself to the contributors table * chore(calendar): removing console.log * fix(calendars): Condição para filtrar apenas os valores selecionáveis. * test(stories): Test to check if the dateFilter property interferes with the value of the selectables
1 parent 6909d23 commit 2bdaffd

File tree

5 files changed

+58
-11
lines changed

5 files changed

+58
-11
lines changed

.all-contributorsrc

+11
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,17 @@
3333
"doc",
3434
"example"
3535
]
36+
},
37+
{
38+
"login": "lucasnsborges",
39+
"name": "Lucas Borges",
40+
"avatar_url": "https://avatars2.githubusercontent.com/u/12260334?v=4",
41+
"profile": "https://github.com/lucasnsborges",
42+
"contributions": [
43+
"code",
44+
"doc",
45+
"example"
46+
]
3647
}
3748
]
3849
}

README.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Datepickers built with [Semantic UI for React][semantic-ui-react] and [Dayzed][d
44

55
[![version][version-badge]][package]
66
[![MIT License][license-badge]][license]
7-
[![All Contributors](https://img.shields.io/badge/all_contributors-2-orange.svg?style=flat-square)](#contributors)
7+
[![All Contributors](https://img.shields.io/badge/all_contributors-3-orange.svg?style=flat-square)](#contributors)
88

99
## Overview
1010

@@ -78,6 +78,7 @@ More examples [here](https://react-semantic-ui-datepickers.now.sh).
7878
| locale | object | no | [en-US](https://github.com/arthurdenner/react-semantic-ui-datepickers/blob/master/src/locales/en-US.js) | Object with the labels to be used on the library PS: Feel free to submit PR's with more locales! |
7979
| onDateChange | function | yes | | Callback fired when the value changes |
8080
| type | string | no | basic | Type of input to render. Available options: 'basic' and 'range' |
81+
| filterDate | function | no | () => true | Function that receives each date and returns a boolean to indicate whether it is enabled or not |
8182
|selected | Date, arrayOf(Date) | no | | Default date selected |
8283
| pointing | string | no | 'left' | Location to render the component around input. Available options: 'left', 'right', 'top left', 'top right' |
8384

@@ -133,8 +134,8 @@ Thanks goes to these wonderful people ([emoji key](https://github.com/kentcdodds
133134

134135
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
135136
<!-- prettier-ignore -->
136-
| [<img src="https://avatars0.githubusercontent.com/u/13774309?v=4" width="100px;"/><br /><sub><b>Arthur Denner</b></sub>](https://github.com/arthurdenner)<br />[💻](https://github.com/arthurdenner/react-semantic-ui-datepickers/commits?author=arthurdenner "Code") [🎨](#design-arthurdenner "Design") [📖](https://github.com/arthurdenner/react-semantic-ui-datepickers/commits?author=arthurdenner "Documentation") [💡](#example-arthurdenner "Examples") [🤔](#ideas-arthurdenner "Ideas, Planning, & Feedback") | [<img src="https://avatars2.githubusercontent.com/u/10627086?v=4" width="100px;"/><br /><sub><b>Emerson Laurentino</b></sub>](https://twitter.com/elaurent_)<br />[💻](https://github.com/arthurdenner/react-semantic-ui-datepickers/commits?author=emersonlaurentino "Code") [🤔](#ideas-emersonlaurentino "Ideas, Planning, & Feedback") [📖](https://github.com/arthurdenner/react-semantic-ui-datepickers/commits?author=emersonlaurentino "Documentation") [💡](#example-emersonlaurentino "Examples") |
137-
| :---: | :---: |
137+
| [<img src="https://avatars0.githubusercontent.com/u/13774309?v=4" width="100px;"/><br /><sub><b>Arthur Denner</b></sub>](https://github.com/arthurdenner)<br />[💻](https://github.com/arthurdenner/react-semantic-ui-datepickers/commits?author=arthurdenner "Code") [🎨](#design-arthurdenner "Design") [📖](https://github.com/arthurdenner/react-semantic-ui-datepickers/commits?author=arthurdenner "Documentation") [💡](#example-arthurdenner "Examples") [🤔](#ideas-arthurdenner "Ideas, Planning, & Feedback") | [<img src="https://avatars2.githubusercontent.com/u/10627086?v=4" width="100px;"/><br /><sub><b>Emerson Laurentino</b></sub>](https://twitter.com/elaurent_)<br />[💻](https://github.com/arthurdenner/react-semantic-ui-datepickers/commits?author=emersonlaurentino "Code") [🤔](#ideas-emersonlaurentino "Ideas, Planning, & Feedback") [📖](https://github.com/arthurdenner/react-semantic-ui-datepickers/commits?author=emersonlaurentino "Documentation") [💡](#example-emersonlaurentino "Examples") | [<img src="https://avatars2.githubusercontent.com/u/12260334?v=4" width="100px;"/><br /><sub><b>Lucas Borges</b></sub>](https://github.com/lucasnsborges)<br />[💻](https://github.com/arthurdenner/react-semantic-ui-datepickers/commits?author=lucasnsborges "Code") [📖](https://github.com/arthurdenner/react-semantic-ui-datepickers/commits?author=lucasnsborges "Documentation") [💡](#example-lucasnsborges "Examples") |
138+
| :---: | :---: | :---: |
138139

139140
<!-- ALL-CONTRIBUTORS-LIST:END -->
140141

src/components/calendar/calendar.js

+11-4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const pointings = {
2222

2323
const Calendar = ({
2424
calendars,
25+
filterDate,
2526
getBackProps,
2627
getDateProps,
2728
getForwardProps,
@@ -98,16 +99,22 @@ const Calendar = ({
9899
week.map((dateObj, weekIdx) => {
99100
const key = `${calendar.year}-${calendar.month}-${weekIdx}`;
100101

101-
return dateObj ? (
102+
if (!dateObj) {
103+
return <CalendarCell key={key} />;
104+
}
105+
106+
const selectable =
107+
dateObj.selectable && filterDate(dateObj.date);
108+
109+
return (
102110
<CalendarCell
103111
key={key}
104112
{...dateObj}
105-
{...getDateProps({ dateObj })}
113+
{...getDateProps({ dateObj: { ...dateObj, selectable } })}
114+
selectable={selectable}
106115
>
107116
{dateObj.date.getDate()}
108117
</CalendarCell>
109-
) : (
110-
<CalendarCell key={key} />
111118
);
112119
})
113120
)}

src/components/datepicker.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -58,18 +58,20 @@ class SemanticDatepicker extends React.Component {
5858
]),
5959
type: PropTypes.oneOf(['basic', 'range']),
6060
pointing: PropTypes.oneOf(['left', 'right', 'top left', 'top right']),
61+
filterDate: PropTypes.func,
6162
};
6263

6364
static defaultProps = {
64-
pointing: 'left',
6565
clearable: true,
66-
keepOpenOnClear: false,
67-
keepOpenOnSelect: false,
6866
date: undefined,
67+
filterDate: () => true,
6968
firstDayOfWeek: 0,
7069
format: 'YYYY-MM-DD',
70+
keepOpenOnClear: false,
71+
keepOpenOnSelect: false,
7172
locale: localeEn,
7273
placeholder: null,
74+
pointing: 'left',
7375
selected: null,
7476
type: 'basic',
7577
};
@@ -232,7 +234,7 @@ class SemanticDatepicker extends React.Component {
232234

233235
render() {
234236
const { isVisible, selectedDate, selectedDateFormatted } = this.state;
235-
const { clearable, locale, pointing } = this.props;
237+
const { clearable, locale, pointing, filterDate } = this.props;
236238

237239
return (
238240
<div
@@ -262,6 +264,7 @@ class SemanticDatepicker extends React.Component {
262264
{...this.dayzedProps}
263265
{...props}
264266
{...locale}
267+
filterDate={filterDate}
265268
pointing={pointing}
266269
weekdays={this.weekdays}
267270
/>

stories/index.js

+25
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ const Content = ({ children, style }) => (
1717
</div>
1818
);
1919

20+
const isWeekday = date => {
21+
const day = date.getDay();
22+
23+
return day !== 0 && day !== 6;
24+
};
25+
2026
storiesOf('Examples', module)
2127
.add('Basic', () => (
2228
<Content>
@@ -173,4 +179,23 @@ storiesOf('Examples', module)
173179
<Content>
174180
<SemanticDatepicker pointing="bottom right" onDateChange={console.log} />
175181
</Content>
182+
))
183+
.add('Basic with filterDate', () => (
184+
<Content>
185+
<SemanticDatepicker
186+
filterDate={isWeekday}
187+
onDateChange={console.log}
188+
showOutsideDays
189+
/>
190+
</Content>
191+
))
192+
.add('Basic with filterDate setting a maxDate', () => (
193+
<Content>
194+
<SemanticDatepicker
195+
filterDate={isWeekday}
196+
maxDate={new Date('2019-01-01')}
197+
onDateChange={console.log}
198+
showOutsideDays
199+
/>
200+
</Content>
176201
));

0 commit comments

Comments
 (0)