@antscorp/antsomi-ui 1.6.7 → 1.6.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,18 @@
1
+ import React from 'react';
2
+ interface RateContinuousProps {
3
+ count?: number;
4
+ className?: string;
5
+ defaultValue?: number;
6
+ /**
7
+ * @description number of star
8
+ */
9
+ value?: number;
10
+ type?: 'full' | 'half' | 'continuous';
11
+ /**
12
+ * @param value current number of stars are selected
13
+ * @description number of stars = fillWidth / STAR_WIDTH
14
+ */
15
+ onChange?: (value: number) => void;
16
+ }
17
+ export declare const RateV2: React.FC<RateContinuousProps>;
18
+ export {};
@@ -0,0 +1,73 @@
1
+ import React, { useEffect, useState } from 'react';
2
+ import StarImage from '@antscorp/antsomi-ui/es/assets/images/stars_slider.png';
3
+ const STAR_WIDTH = 30;
4
+ const parseNumDecimals = (input, decimals) => +Number.parseFloat(input === null || input === void 0 ? void 0 : input.toString()).toFixed(decimals);
5
+ const roundHalf = (num) => {
6
+ const multiplier = 1 / 0.5;
7
+ return Math.round(num * multiplier) / multiplier;
8
+ };
9
+ /**
10
+ *
11
+ * @param width width measure from the left edge of star container
12
+ * @param type type of star - 'full' | 'half' | 'continuous'
13
+ * @default type 'full'
14
+ * @returns
15
+ */
16
+ const getWidth = (width, type = 'full') => {
17
+ const numberOfStar = width / STAR_WIDTH;
18
+ return type === 'full'
19
+ ? Math.ceil(numberOfStar) * STAR_WIDTH
20
+ : type === 'half'
21
+ ? roundHalf(numberOfStar) * STAR_WIDTH
22
+ : width;
23
+ };
24
+ export const RateV2 = ({ count = 5, className, defaultValue = 0, value, type = 'full', onChange, }) => {
25
+ const fullWidth = count * STAR_WIDTH;
26
+ // The width is calculated when move mouse => change the UI, not change the real value
27
+ const [fillWidth, setFillWidth] = useState(() => defaultValue ? getWidth(defaultValue * STAR_WIDTH, type) : 0);
28
+ // The real current value that will be return to parent component by onChange function
29
+ // currWidth = value * STAR_WIDTH
30
+ const [currWidth, setCurrWidth] = useState(0);
31
+ const handleMouseMove = e => {
32
+ const rect = e.currentTarget.getBoundingClientRect();
33
+ const x = e.clientX - rect.left;
34
+ const hoverWidth = Math.round(x);
35
+ setFillWidth(getWidth(hoverWidth, type));
36
+ };
37
+ const handleClick = () => {
38
+ // If have no value => auto set current value; if have value => must control value with onChange props
39
+ if (!value)
40
+ setCurrWidth(fillWidth);
41
+ if (onChange)
42
+ onChange(parseNumDecimals(fillWidth / STAR_WIDTH, 2));
43
+ };
44
+ useEffect(() => {
45
+ if (typeof value === 'number') {
46
+ const width = getWidth(value * STAR_WIDTH, type);
47
+ setCurrWidth(width);
48
+ setFillWidth(width);
49
+ }
50
+ }, [value]);
51
+ return (React.createElement("div", { className: `antsomi-rate-continuous${className ? ` ${className}` : ''}`, style: {
52
+ position: 'relative',
53
+ width: `${fullWidth}px`,
54
+ backgroundPosition: '-27px',
55
+ height: '28px',
56
+ }, onMouseMove: handleMouseMove, onMouseLeave: () => setFillWidth(currWidth || 0), onClick: handleClick },
57
+ React.createElement("div", { className: "custom-rate custom-rate--fill", style: {
58
+ position: 'absolute',
59
+ background: `url(${StarImage}) 0px 0px`,
60
+ width: `${fillWidth}px`,
61
+ height: '26px',
62
+ zIndex: 1,
63
+ } }),
64
+ React.createElement("div", { className: "custom-rate custom-rate--empty", style: {
65
+ position: 'absolute',
66
+ top: '0px',
67
+ left: '0px',
68
+ backgroundImage: 'none',
69
+ background: `transparent url(${StarImage}) 0px -26px`,
70
+ width: `${fullWidth}px`,
71
+ height: '26px',
72
+ } })));
73
+ };
@@ -0,0 +1 @@
1
+ export { RateV2 } from './RateV2';
@@ -0,0 +1 @@
1
+ export { RateV2 } from './RateV2';
@@ -1,5 +1,6 @@
1
1
  import React from 'react';
2
2
  import type { SliderRangeProps, SliderSingleProps } from 'antd/es/slider';
3
- export interface SliderProps extends SliderSingleProps {
3
+ export interface SliderProps {
4
+ defaultSliderTrack?: boolean;
4
5
  }
5
- export declare const SliderV2: React.FC<SliderProps | SliderRangeProps>;
6
+ export declare const SliderV2: React.FC<(SliderSingleProps & SliderProps) | (SliderRangeProps & SliderProps)>;
@@ -4,20 +4,18 @@ import React, { useState } from 'react';
4
4
  import { Slider as AntdSlider } from 'antd';
5
5
  // React.FC<SliderProps>
6
6
  export const SliderV2 = props => {
7
- const [value, setValue] = useState(0);
8
- // console.log('sliderRef:: ', sliderRef);
9
- // const min = props.min || 0;
10
- // const max = props.max || 100;
11
- const { min, max, onChange, styles, range } = props;
12
- if (min && max && min < 0 && max > 0 && !range) {
7
+ const { min, max, onChange, styles, range, value, defaultValue, marks, defaultSliderTrack } = props;
8
+ const [valueInternal, setValueInternal] = useState(defaultValue || min || 0);
9
+ if (min && max && min < 0 && max > 0 && !range && !defaultSliderTrack) {
13
10
  const sliderWidth = max - min;
14
- const leftTrackPosition = `${(Math.abs(value > 0 ? min : min - value) / sliderWidth) * 100}%`;
15
- const sliderTrackWidth = `${(Math.abs(value) / sliderWidth) * 100}%`;
16
- return (React.createElement(AntdSlider, Object.assign({ value: value, onChange: value => {
17
- setValue(value);
11
+ const calcValue = value || valueInternal;
12
+ const leftTrackPosition = `${(Math.abs(calcValue > 0 ? min : min - calcValue) / sliderWidth) * 100}%`;
13
+ const sliderTrackWidth = `${(Math.abs(calcValue) / sliderWidth) * 100}%`;
14
+ return (React.createElement(AntdSlider, Object.assign({ value: value || valueInternal, onChange: value => {
15
+ setValueInternal(value);
18
16
  if (onChange)
19
17
  onChange(value);
20
- }, styles: Object.assign({ track: Object.assign({ left: leftTrackPosition, width: sliderTrackWidth }, styles === null || styles === void 0 ? void 0 : styles.track) }, styles), marks: { 0: ' ' } }, props)));
18
+ }, styles: Object.assign({ track: Object.assign({ left: leftTrackPosition, width: sliderTrackWidth }, styles === null || styles === void 0 ? void 0 : styles.track) }, styles), marks: marks || { 0: ' ' } }, props)));
21
19
  }
22
20
  return React.createElement(AntdSlider, Object.assign({}, props));
23
21
  };
@@ -40,6 +40,7 @@ export * from './ReactIframe';
40
40
  export * from './Scrollbars';
41
41
  export * from './Switch';
42
42
  export * from './Button';
43
+ export * from './RateV2';
43
44
  export type { SliderProps } from './Slider';
44
45
  export type { PaginationProps } from './Pagination';
45
46
  export type { InputDynamicProps } from './InputDynamic';
@@ -40,3 +40,4 @@ export * from './ReactIframe';
40
40
  export * from './Scrollbars';
41
41
  export * from './Switch';
42
42
  export * from './Button';
43
+ export * from './RateV2';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@antscorp/antsomi-ui",
3
- "version": "1.6.7",
3
+ "version": "1.6.9",
4
4
  "description": "An enterprise-class UI design language and React UI library.",
5
5
  "sideEffects": [
6
6
  "dist/*",