Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
ui-component-library / src / inputs / Incrementer / renderProps.tsx
Size: Mime:
import React from 'react'
import { isFunction } from 'exotic'
import {
  IncrementerProps,
  DefaultIncrementerState,
} from './typings'
import {
  StyledInput,
  InputBoxWrapper,
  StyledButton,
  StyledPlusIcon,
  StyledMinusIcon,
  InnerWrapper,
  IncrementerWrapper,
} from './styled'

function defaultOnIncrement(props: IncrementerProps, state: DefaultIncrementerState) {
  return state.incrementCount(props)
}

function defaultOnDecrement(props: IncrementerProps, state: DefaultIncrementerState) {
  return state.decrementCount(props)
}

function defaultHandleChange(props: IncrementerProps, state: DefaultIncrementerState, event: Event) {
  return state.handleChange(props, event)
}

function defaultHandleBlur(props: IncrementerProps, state: DefaultIncrementerState, event: Event) {
  return state.handleBlur(props, event)
}

function defaultRenderBox(props: IncrementerProps, state: DefaultIncrementerState) {
  const { onChange } = props

  const handleOnChangeQuantity = () => {
    if (isFunction(onChange)) {
      props.onChange(state.count)
    }
  }
  const increment = event => {
    defaultOnIncrement(props, state)
    handleOnChangeQuantity()
  }
  const decrement = event => {
    defaultOnDecrement(props, state)
    handleOnChangeQuantity()
  }
  const handleChange = event => {
    defaultHandleChange(props, state, event)
    handleOnChangeQuantity()
  }

  const handleBlur = event => defaultHandleBlur(props, state, event)

  return (
    <InnerWrapper>
      <StyledButton onClick={decrement} isDisabled={!state.shouldDecrement} >
        <StyledMinusIcon />
      </StyledButton>
      <InputBoxWrapper><StyledInput type="text" value={state.count} onChange={handleChange} onBlur={handleBlur} /></InputBoxWrapper>
      <StyledButton onClick={increment} isDisabled={!state.shouldIncrement}>
        <StyledPlusIcon />
      </StyledButton>
    </InnerWrapper>
  )
}

function defaultRenderWrapper(props: IncrementerProps) {
  const { children, nowrap, className } = props

  if (nowrap) {
    return <React.Fragment>{children}</React.Fragment>
  } else {
    return <IncrementerWrapper className={className}>{children}</IncrementerWrapper>
  }
}

export { defaultRenderBox, defaultRenderWrapper }