Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2019
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // THEMING IS HARD WITH THIS ONE.
  2. // Сначала - темы.
  3. import produce from 'immer'
  4. import ThemedStyleSheet from 'react-with-styles/lib/ThemedStyleSheet';
  5. import aphroditeInterface from 'react-with-styles-interface-aphrodite';
  6. import DefaultTheme from 'react-dates/lib/theme/DefaultTheme';
  7. // https://github.com/airbnb/react-dates/blob/master/src/theme/DefaultTheme.js
  8.  
  9. ThemedStyleSheet.registerInterface(aphroditeInterface);
  10. const DesignSystemTheme = produce(DefaultTheme, draft => {
  11.   const { input, pickerInput } = draft.reactDates.border
  12.   input.border = '1px solid transparent';
  13.   input.borderTop = '1px solid transparent'
  14.   input.borderBottom ='1px solid transparent'
  15.   input.borderLeft = '1px solid transparent'
  16.   input.borderRight= '1px solid transparent';
  17.   input.borderTopFocused = '1px solid #0091ea';
  18.   input.borderLeftFocused= '1px solid #0091ea';
  19.   input.borderBottomFocused= '1px solid #0091ea';
  20.   input.borderRightFocused= '1px solid #0091ea';
  21.   input.borderRadius = 4;
  22.   pickerInput.borderRadius = 4
  23.   // Цвета:
  24.   const { color } = draft.reactDates
  25.   const lightBlue = '#0091ea'
  26.   const blue = '#1e88e5'
  27.   const darkBlue = '#1976d2'
  28.   color.core.primary = darkBlue;
  29.   color.core.primaryShade_1 = blue;
  30.   color.core.primaryShade_2 = lightBlue;
  31.   color.core.primaryShade_3 = lightBlue;
  32.   color.core.primaryShade_4 = lightBlue;
  33.   color.background = '#f5f5f5';
  34.   color.backgroundFocused = '#ffffff';
  35.   color.border = 'transparent';
  36.   color.outside.backgroundColor_hover = '#ff0000';
  37.   //tbd
  38.   const { sizing } = draft.reactDates
  39.   sizing.inputWidth = '100%';
  40.   sizing.inputWidth = '100%';
  41.   const { spacing }= draft.reactDates
  42.   spacing.displayTextPaddingTop = 7;
  43.   spacing.displayTextPaddingBottom = 7;
  44.   spacing.displayTextPaddingLeft = 16;
  45.   spacing.displayTextPaddingRight = 8;
  46.   const { font } = draft.reactDates
  47.   font.input.size = 14;
  48.   font.input.size_small = 14;
  49. })
  50. ThemedStyleSheet.registerTheme(DesignSystemTheme);
  51.  
  52. // Проставим локаль.
  53. import 'moment/locale/ru'
  54. moment.locale('ru')
  55. // Ta-da!
  56.  
  57. // Теперь у нас отст<s>тойный</s>айленный датапикер.
  58. import React, { Component } from 'react'
  59. import moment from 'moment'
  60. import styled from 'styled-components'
  61. import { DateRangePicker, SingleDatePicker, DayPickerRangeController } from 'react-dates'
  62. import Box from '../primitives/Box'
  63.  
  64. const Wrapper = styled(Box)`
  65.   input:hover:not(:focus) {
  66.     border: 1px solid #3a3a3a !important;
  67.     background: #ffffff !important;
  68.   }
  69. `
  70.  
  71. /** Используется для получение данных типа "Дата" от пользователя.*/
  72. class Datepicker extends Component {
  73.   constructor(props) {
  74.     super(props)
  75.     if (!this.props.id) {
  76.       throw new Error('id prop обязателен для компонента.')
  77.     }
  78.     this.state = {
  79.       value: props.value === undefined ? props.value : props.defaultValue || null,
  80.     }
  81.   }
  82.  
  83.   static getDerivedStateFromProps(nextProps) {
  84.     if ('value' in nextProps) {
  85.       return {
  86.         value: nextProps.value,
  87.       }
  88.     }
  89.     return null
  90.   }
  91.  
  92.   handleChange = (newDate, b, c) => {
  93.     this.setState({
  94.       value: newDate,
  95.     })
  96.     this.props.onChange && this.props.onChange(newDate)
  97.   }
  98.  
  99.   render() {
  100.     return (
  101.       <Wrapper>
  102.         <SingleDatePicker
  103.           className="dsDatepicker"
  104.           {...this.props}
  105.           // required props
  106.           date={this.state.value} // momentPropTypes.momentObj or null
  107.           onDateChange={this.handleChange} // PropTypes.func.isRequired
  108.           focused={this.state.focused} // PropTypes.bool
  109.           onFocusChange={({ focused }) => this.setState({ focused })} // PropTypes.func.isRequired
  110.           // optional
  111.           hideKeyboardShortcutsPanel
  112.           regular
  113.           numberOfMonths={1}
  114.           isOutsideRange={() => false}
  115.         />
  116.       </Wrapper>
  117.     )
  118.   }
  119. }
  120.  
  121. /** @component */
  122. export default Datepicker
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement