0.1.1 • Published 8 months ago

@acrool/react-picker v0.1.1

Weekly downloads
-
License
MIT
Repository
github
Last release
8 months ago

Acrool React Picker

NPM npm npm

npm downloads npm

Features

  • Quickly create various Pickers such as Datepicker, timepicker, Select dropdown, etc.
  • Plug and unplug using framer-motion
  • Simulate the browser's native drop-down menu behavior (click outside to close, lose focus to close, open the menu when the keyboard is pressed or blank)
  • The agent menu OnChange and value changes can be controlled freely and will be saved when the menu is closed.

Install

yarn add @acrool/react-picker

Usage

add in your index.tsx

import "@acrool/react-picker/dist/index.css";

add in your App.tsx

import {isEmpty} from '@acrool/js-utils/equal';
import clsx from 'clsx';
import React, {ForwardedRef} from 'react';
import styled, {css} from 'styled-components';

import NumberKeyboard from './NumberKeyboard';
import {createPicker, usePicker} from '@acrool/react-picker';




interface IProps extends FCProps {
  value?: number
  options?: number[]
  onChange?: (value: number) => void
  placeholder?: string
}



/**
 * Select Number Keyboard
 */
const SelectNumberKeyboard = ({
    id,
    placeholder = '0',
}: IProps, ref?: ForwardedRef<HTMLButtonElement>) => {
  const Picker = usePicker();

  /**
   * Clean
   */
  const handleClear = (e: React.MouseEvent) => {
    e.stopPropagation();
    Picker.onChange(0);
  };

  const isPlaceholderValue = isEmpty(Picker.value);

  return <SelectNumberKeyboardRoot
          ref={ref}
          type="button"
          onMouseDown={Picker.toggle}
          isFocus={Picker.isInputFocus}
          onFocus={Picker.inputFocus}
    >
      <Text isPlaceholderValue={isPlaceholderValue}>
        {isPlaceholderValue ? placeholder: Picker.value}
      </Text>
  </SelectNumberKeyboardRoot>;
};


const Picker = (props: IProps) => {
  const Picker = usePicker();

  const handleClickPicker = (addNumber: number) => {
    let result = 0;
    const currValue = Picker.value ?? 0;
    if(addNumber >= 0){
      result = currValue + addNumber;
    }
    Picker.onChange(result);
  };

  return <NumberKeyboard
          data={props.options}
          onChange={handleClickPicker}
  />;
};

export default createPicker(
        SelectNumberKeyboard,
        Picker
);


const SelectNumberKeyboardRoot = styled.button<{
  isFocus?: boolean,
}>`
    transition: box-shadow .15s ease-in-out;

    ${props => props.isFocus && css`
        box-shadow: 0 0 0 0.2rem rgb(0 123 255 / 25%);
    `}

`;

If you need to automatically infer types

export default createPicker(
        SelectNumberKeyboard,
        Picker
) as <V extends any>(props: IProps<V>) => JSX.Element;

If you need to not hide trigger onChange

export default createPicker(
        SelectNumberKeyboard,
        Picker,
    {
        isEnableHideSave: false,
        isEnableClickOutSiteHidden: false
    }
) as <V extends any>(props: IProps<V>) => JSX.Element;

There is also a example that you can play with it:

Play react-editext-example

Precautions

  • The main control controls the opening and closing of the menu. You need to use onMousedown instead of onClick.
  • The main control needs to use button to have the Tab focus function, where react-hook-form is very useful
  • The main control needs to use button. If you want to add a clear button in the inner layer, remember that it cannot be button, because button > button is an html structure error, and you need to use onMousedown instead of onClick.

Ref warning

Warning: forwardRef render functions accept exactly two parameters: props and ref. Did you forget to use the ref parameter?

interface IProps {
  value: string, 
  onChange:(value: string) => void
}
const DateTimeField = (props: IProps) => {}

// fix to 
const DateTimeFieldAfter = (props: IProps, ref?: ForwardedRef<HTMLElement>) => {}

License

MIT © Acrool & Imagine

0.1.1

8 months ago

0.1.2-alpha.0

8 months ago

0.1.1-alpha.1

9 months ago

0.0.11-alpha.1

9 months ago

0.1.1-alpha.0

9 months ago

0.1.0

9 months ago

0.0.11-alpha.0

9 months ago

0.0.12-alpha.0

9 months ago

0.0.11

9 months ago

0.1.0-alpha.5

9 months ago

0.1.0-alpha.4

9 months ago

0.1.0-alpha.7

9 months ago

0.1.0-alpha.6

9 months ago

0.1.0-alpha.1

9 months ago

0.1.0-alpha.0

9 months ago

0.1.0-alpha.3

9 months ago

0.1.0-alpha.2

9 months ago

0.1.0-test.2

9 months ago

0.1.0-test.3

9 months ago

0.1.0-test.1

9 months ago

0.0.5-alpha.0

10 months ago

0.0.6-alpha.0

10 months ago

0.0.6-alpha.1

10 months ago

0.0.6-alpha.2

10 months ago

0.0.7-alpha.0

10 months ago

0.0.6-alpha.3

10 months ago

0.0.3-alpha.3

10 months ago

0.0.6-alpha.4

10 months ago

0.0.3-alpha.2

10 months ago

0.0.6-alpha.5

10 months ago

0.0.6-alpha.6

10 months ago

0.0.4-alpha.0

10 months ago

0.0.3-alpha.4

10 months ago

0.0.3-alpha.1

10 months ago

0.0.3-alpha.0

10 months ago

0.0.10

9 months ago

0.0.9-alpha.0

10 months ago

0.0.10-alpha.2

9 months ago

0.0.10-alpha.3

9 months ago

0.0.10-alpha.4

9 months ago

0.0.10-alpha.5

9 months ago

0.0.8-alpha.0

10 months ago

0.0.10-alpha.0

9 months ago

0.0.3

10 months ago

0.0.10-alpha.1

9 months ago

0.0.9

10 months ago

0.0.8

10 months ago

0.0.5

10 months ago

0.0.4

10 months ago

0.0.7

10 months ago

0.0.6

10 months ago

0.0.2

11 months ago

0.0.2-alpha.7

11 months ago

0.0.2-alpha.6

11 months ago

0.0.2-alpha.5

11 months ago

0.0.2-alpha.4

11 months ago

0.0.2-alpha.3

11 months ago

0.0.2-alpha.2

11 months ago

0.0.2-alpha.1

11 months ago

0.0.2-alpha.0

11 months ago

0.0.1

11 months ago