diff --git a/.eslintrc.js b/.eslintrc.js index 147ba164b..a3673eb6a 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -1,30 +1,26 @@ module.exports = { - extends: [ - "eslint:recommended", - "plugin:react/recommended", - "prettier", + extends: ['eslint:recommended', 'plugin:react/recommended', 'prettier'], + plugins: ['react', 'prettier'], + rules: { + 'prettier/prettier': [ + 'error', + { + singleQuote: true, + trailingComma: 'es5', + bracketSpacing: true, + jsxBracketSameLine: true, + printWidth: 100, + parser: 'babylon' + } ], - plugins: [ - "react", - "prettier", - ], - rules: { - "prettier/prettier": ["error", { - "singleQuote": true, - "trailingComma": "es5", - "bracketSpacing": true, - "jsxBracketSameLine": true, - "printWidth": 100, - "parser": "babylon", - }], - "no-debugger": 0, - "no-console": 0, - }, - parser: "babel-eslint", - env: { - "es6": true, - "node": true, - "browser": true, - "jest": true, - }, -}; \ No newline at end of file + 'no-debugger': 0, + 'no-console': 0 + }, + parser: 'babel-eslint', + env: { + es6: true, + node: true, + browser: true, + jest: true + } +}; diff --git a/.github/workflows/publish_to_npm.yml b/.github/workflows/publish_to_npm.yml index 4ee8e378b..ef071a1fe 100644 --- a/.github/workflows/publish_to_npm.yml +++ b/.github/workflows/publish_to_npm.yml @@ -18,6 +18,15 @@ jobs: - run: yarn build:css - run: yarn build:js - run: yarn styleguidist build - - run: yarn publish + - name: Get version from package.json + id: get_version + run: echo "VERSION=$(node -p "require('./package.json').version")" >> $GITHUB_OUTPUT + - name: Publish to npm + run: | + if [[ "${{ steps.get_version.outputs.VERSION }}" == *"alpha"* ]]; then + yarn publish --tag alpha + else + yarn publish + fi env: NODE_AUTH_TOKEN: ${{ secrets.NPM_AUTH_TOKEN }} \ No newline at end of file diff --git a/.gitignore b/.gitignore index 31ca39e05..48f8c6540 100755 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +.vscode/ node_modules/ .DS_Store *.log diff --git a/.prettierrc b/.prettierrc index 727df7cf8..9829e768e 100755 --- a/.prettierrc +++ b/.prettierrc @@ -1,11 +1,8 @@ { - "printWidth": 110, - "tabWidth": 2, - "useTabs": false, - "semi": true, - "singleQuote": true, - "trailingComma": "none", - "bracketSpacing": true, - "jsxBracketSameLine": false, - "fluid": false - } \ No newline at end of file + "singleQuote": true, + "trailingComma": "es5", + "bracketSpacing": true, + "jsxBracketSameLine": true, + "printWidth": 100, + "parser": "babylon" +} diff --git a/package.json b/package.json index f2bb2047f..c068318db 100644 --- a/package.json +++ b/package.json @@ -1,13 +1,15 @@ { "name": "silq-react-date-range", - "version": "2.4.0", + "version": "2.4.1-alpha.8", "description": "A React component for choosing dates and date ranges.", "main": "dist/index.js", "scripts": { - "start": "yarn build:css & styleguidist server", + "start": "yarn watch:css & yarn watch:js & styleguidist server", "build": "yarn build:css & yarn build:js & ls & styleguidist build", + "watch:css": "postcss 'src/styles.scss' -w -d dist --ext css & postcss 'src/theme/*.scss' -w -d 'dist/theme' --ext css", "build:css": "postcss 'src/styles.scss' -d dist --ext css & postcss 'src/theme/*.scss' -d 'dist/theme' --ext css", "build:js": "babel ./src --out-dir ./dist --ignore test.js", + "watch:js": "babel ./src --out-dir ./dist --ignore test.js --watch", "lint": "eslint 'src/**/*.js'", "test": "jest", "preversion": "yarn clear & yarn build" diff --git a/src/components/Calendar/index.js b/src/components/Calendar/index.js index 7f683af64..bea269fbd 100644 --- a/src/components/Calendar/index.js +++ b/src/components/Calendar/index.js @@ -1,9 +1,9 @@ +// @ts-check import React, { PureComponent } from 'react'; import PropTypes from 'prop-types'; import { rangeShape } from '../DayCell'; import Month from '../Month'; -import DateInput from '../DateInput'; -import { calcFocusDate, generateStyles, getMonthDisplayRange } from '../../utils'; +import { calcFocusDate, CommonProps, generateStyles, getMonthDisplayRange } from '../../utils'; import classnames from 'classnames'; import ReactList from 'react-list'; import { shallowEqualObjects } from 'shallow-equal'; @@ -15,7 +15,6 @@ import { startOfWeek, endOfWeek, isSameDay, - addYears, setYear, setMonth, differenceInCalendarMonths, @@ -25,15 +24,15 @@ import { isSameMonth, differenceInDays, min, - max + max, } from 'date-fns'; import { enUS as defaultLocale } from 'date-fns/locale/en-US'; import coreStyles from '../../styles'; import { ariaLabelsShape } from '../../accessibility'; class Calendar extends PureComponent { - constructor (props, context) { - super(props, context); + constructor(props) { + super(props); this.dateOptions = { locale: props.locale }; if (props.weekStartsOn !== undefined) this.dateOptions.weekStartsOn = props.weekStartsOn; this.styles = generateStyles([coreStyles, props.classNames]); @@ -45,16 +44,16 @@ class Calendar extends PureComponent { drag: { status: false, range: { startDate: null, endDate: null }, - disablePreview: false + disablePreview: false, }, - scrollArea: this.calcScrollArea(props) + scrollArea: this.calcScrollArea(props), }; } - getMonthNames () { + getMonthNames() { return [...Array(12).keys()].map(i => this.props.locale.localize.month(i)); } - calcScrollArea (props) { + calcScrollArea(props) { const { direction, months, scroll } = props; if (!scroll.enabled) return { enabled: false }; @@ -65,7 +64,7 @@ class Calendar extends PureComponent { monthHeight: scroll.monthHeight || 220, longMonthHeight: longMonthHeight || 260, calendarWidth: 'auto', - calendarHeight: (scroll.calendarHeight || longMonthHeight || 240) * months + calendarHeight: (scroll.calendarHeight || longMonthHeight || 240) * months, }; } return { @@ -73,7 +72,7 @@ class Calendar extends PureComponent { monthWidth: scroll.monthWidth || 332, calendarWidth: (scroll.calendarWidth || scroll.monthWidth || 332) * months, monthHeight: longMonthHeight || 300, - calendarHeight: longMonthHeight || 300 + calendarHeight: longMonthHeight || 300, }; } focusToDate = (date, props = this.props, preventUnnecessary = true) => { @@ -100,7 +99,7 @@ class Calendar extends PureComponent { const newProps = props.scroll.enabled ? { ...props, - months: this.list.getVisibleRange().length + months: this.list.getVisibleRange().length, } : props; const newFocus = calcFocusDate(this.state.focusedDate, newProps); @@ -114,32 +113,36 @@ class Calendar extends PureComponent { const preview = { startDate: val, endDate: val, - color: this.props.color + color: this.props.color, }; this.setState({ preview }); }; - componentDidMount () { + componentDidMount() { if (this.props.scroll.enabled) { // prevent react-list's initial render focus problem setTimeout(() => this.focusToDate(this.state.focusedDate)); } } - componentDidUpdate (prevProps) { + componentDidUpdate(prevProps) { const propMapper = { dateRange: 'ranges', - date: 'date' + date: 'date', }; const targetProp = propMapper[this.props.displayMode]; if (this.props[targetProp] !== prevProps[targetProp]) { this.updateShownDate(this.props); } - if (prevProps.locale !== this.props.locale || prevProps.weekStartsOn !== this.props.weekStartsOn) { + if ( + prevProps.locale !== this.props.locale || + prevProps.weekStartsOn !== this.props.weekStartsOn + ) { this.dateOptions = { locale: this.props.locale }; - if (this.props.weekStartsOn !== undefined) this.dateOptions.weekStartsOn = this.props.weekStartsOn; + if (this.props.weekStartsOn !== undefined) + this.dateOptions.weekStartsOn = this.props.weekStartsOn; this.setState({ - monthNames: this.getMonthNames() + monthNames: this.getMonthNames(), }); } @@ -155,24 +158,22 @@ class Calendar extends PureComponent { monthOffset: () => addMonths(focusedDate, value), setMonth: () => setMonth(focusedDate, value), setYear: () => setYear(focusedDate, value), - set: () => value + set: () => value, }; const newDate = min([max([modeMapper[mode](), minDate]), maxDate]); this.focusToDate(newDate, this.props, false); onShownDateChange && onShownDateChange(newDate); }; - handleRangeFocusChange = (rangesIndex, rangeItemIndex) => { - this.props.onRangeFocusChange && this.props.onRangeFocusChange([rangesIndex, rangeItemIndex]); - }; + handleScroll = () => { const { onShownDateChange, minDate } = this.props; const { focusedDate } = this.state; const { isFirstRender } = this; - const visibleMonths = this.list.getVisibleRange(); + const visibleMonths = this.list?.getVisibleRange(); // prevent scroll jump with wrong visible value - if (visibleMonths[0] === undefined) return; + if (!visibleMonths || visibleMonths[0] === undefined) return; const visibleMonth = addMonths(minDate, visibleMonths[0] || 0); const isFocusedToDifferent = !isSameMonth(visibleMonth, focusedDate); if (isFocusedToDifferent && !isFirstRender) { @@ -181,97 +182,124 @@ class Calendar extends PureComponent { } this.isFirstRender = false; }; + renderMonthAndYear = (focusedDate, changeShownDate, props) => { - const { showMonthArrow, minDate, maxDate, showMonthAndYearPickers, ariaLabels } = props; + const { minDate, maxDate, ariaLabels } = props; const upperYearLimit = (maxDate || Calendar.defaultProps.maxDate).getFullYear(); const lowerYearLimit = (minDate || Calendar.defaultProps.minDate).getFullYear(); + const styles = this.styles; + + return ( +