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

Added Day Number Color prop to ranges #384

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ inputRanges(`DefinedRange`, `DateRangePicker`) | Array | [default i
startDate: PropTypes.object,
endDate: PropTypes.object,
color: PropTypes.string,
dayNumberColor: PropTypes.string,
key: PropTypes.string,
autoFocus: PropTypes.bool,
disabled: PropTypes.bool,
Expand Down
1 change: 1 addition & 0 deletions src/components/DateRangePicker/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ const [state, setState] = useState({
selection3: {
startDate: addDays(new Date(), 8),
endDate: addDays(new Date(), 10),
dayNumberColor: '#000000',
key: 'selection3',
autoFocus: false
}
Expand Down
41 changes: 31 additions & 10 deletions src/components/DayCell/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,16 +102,13 @@ class DayCell extends Component {
/>
);
};
renderSelectionPlaceholders = () => {
const { styles, ranges, day } = this.props;
if (this.props.displayMode === 'date') {
let isSelected = isSameDay(this.props.day, this.props.date);
return isSelected ? (
<span className={styles.selected} style={{ color: this.props.color }} />
) : null;
getInRanges = () => {
const { ranges, day } = this.props;
if (this.props.displayMode !== 'dateRange') {
return null;
}

const inRanges = ranges.reduce((result, range) => {
return ranges.reduce((result, range) => {
let startDate = range.startDate;
let endDate = range.endDate;
if (startDate && endDate && isBefore(endDate, startDate)) {
Expand All @@ -136,6 +133,16 @@ class DayCell extends Component {
}
return result;
}, []);
};
renderSelectionPlaceholders = inRanges => {
const { styles } = this.props;
if (this.props.displayMode === 'date') {
let isSelected = isSameDay(this.props.day, this.props.date);
return isSelected ? (
<span className={styles.selected} style={{ color: this.props.color }} />
) : null;
}
inRanges = inRanges === undefined ? this.getInRanges() : inRanges;

return inRanges.map((range, i) => (
<span
Expand All @@ -149,7 +156,18 @@ class DayCell extends Component {
/>
));
};
getDayNumberColor = inRanges => {
if (inRanges === null) {
return;
}
inRanges = inRanges === undefined ? this.getInRanges() : inRanges;
if (inRanges.length === 0) {
return;
}
return inRanges[inRanges.length - 1].dayNumberColor;
};
render() {
const ranges = this.getInRanges();
return (
<button
type="button"
Expand All @@ -165,10 +183,12 @@ class DayCell extends Component {
className={this.getClassNames(this.props.styles)}
{...(this.props.disabled || this.props.isPassive ? { tabIndex: -1 } : {})}
style={{ color: this.props.color }}>
{this.renderSelectionPlaceholders()}
{this.renderSelectionPlaceholders(ranges)}
{this.renderPreviewPlaceholder()}
<span className={this.props.styles.dayNumber}>
<span>{format(this.props.day, this.props.dayDisplayFormat)}</span>
<span style={{ color: this.getDayNumberColor(ranges) }}>
{format(this.props.day, this.props.dayDisplayFormat)}
</span>
</span>
</button>
);
Expand All @@ -181,6 +201,7 @@ export const rangeShape = PropTypes.shape({
startDate: PropTypes.object,
endDate: PropTypes.object,
color: PropTypes.string,
dayNumberColor: PropTypes.string,
key: PropTypes.string,
autoFocus: PropTypes.bool,
disabled: PropTypes.bool,
Expand Down