@antscorp/antsomi-ui 1.3.3-beta.18 → 1.3.3-beta.19

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.
@@ -3,12 +3,16 @@ interface RateContinuousProps {
3
3
  count?: number;
4
4
  className?: string;
5
5
  defaultValue?: number;
6
+ /**
7
+ * @description number of star
8
+ */
6
9
  value?: number;
10
+ type?: 'full' | 'half' | 'continuous';
7
11
  /**
8
12
  * @param value current number of stars are selected
9
13
  * @description number of stars = fillWidth / STAR_WIDTH
10
14
  */
11
15
  onChange?: (value: number) => void;
12
16
  }
13
- export declare const RateContinuous: React.FC<RateContinuousProps>;
17
+ export declare const RateV2: React.FC<RateContinuousProps>;
14
18
  export {};
@@ -1,16 +1,38 @@
1
1
  import React, { useEffect, useState } from 'react';
2
2
  import StarImage from '@antscorp/antsomi-ui/es/assets/images/stars_slider.png';
3
- const parseNumDecimals = (input, decimals) => +Number.parseFloat(input === null || input === void 0 ? void 0 : input.toString()).toFixed(decimals);
4
3
  const STAR_WIDTH = 30;
5
- export const RateContinuous = ({ count = 5, className, defaultValue = 0, value, onChange, }) => {
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, }) => {
6
25
  const fullWidth = count * STAR_WIDTH;
7
- const [fillWidth, setFillWidth] = useState(0);
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
8
29
  // currWidth = value * STAR_WIDTH
9
- const [currWidth, setCurrWidth] = useState(() => value ? value * STAR_WIDTH : defaultValue ? defaultValue * STAR_WIDTH : 0);
30
+ const [currWidth, setCurrWidth] = useState(0);
10
31
  const handleMouseMove = e => {
11
32
  const rect = e.currentTarget.getBoundingClientRect();
12
33
  const x = e.clientX - rect.left;
13
- setFillWidth(Math.round(x));
34
+ const hoverWidth = Math.round(x);
35
+ setFillWidth(getWidth(hoverWidth, type));
14
36
  };
15
37
  const handleClick = () => {
16
38
  // If have no value => auto set current value; if have value => must control value with onChange props
@@ -21,8 +43,9 @@ export const RateContinuous = ({ count = 5, className, defaultValue = 0, value,
21
43
  };
22
44
  useEffect(() => {
23
45
  if (typeof value === 'number') {
24
- setCurrWidth(value * STAR_WIDTH);
25
- setFillWidth(value * STAR_WIDTH);
46
+ const width = getWidth(value * STAR_WIDTH, type);
47
+ setCurrWidth(width);
48
+ setFillWidth(width);
26
49
  }
27
50
  }, [value]);
28
51
  return (React.createElement("div", { className: `antsomi-rate-continuous${className ? ` ${className}` : ''}`, style: { position: 'relative', width: `${fullWidth}px`, backgroundPosition: '-27px' }, onMouseMove: handleMouseMove, onMouseLeave: () => setFillWidth(currWidth || 0), onClick: handleClick },
@@ -41,6 +64,5 @@ export const RateContinuous = ({ count = 5, className, defaultValue = 0, value,
41
64
  background: `transparent url(${StarImage}) 0px -26px`,
42
65
  width: `${fullWidth}px`,
43
66
  height: '26px',
44
- // backgroundPosition: '0px -25px',
45
67
  } })));
46
68
  };
@@ -0,0 +1 @@
1
+ export { RateV2 } from './RateV2';
@@ -0,0 +1 @@
1
+ export { RateV2 } from './RateV2';
@@ -32,5 +32,5 @@ export { ScrollBox } from './ScrollBox';
32
32
  export { Rate } from './Rate';
33
33
  export { SliderV2 } from './SliderV2';
34
34
  export * from './Flex';
35
- export * from './RateContinuous';
35
+ export * from './RateV2';
36
36
  export type { SliderProps } from './Slider';
@@ -32,4 +32,4 @@ export { ScrollBox } from './ScrollBox';
32
32
  export { Rate } from './Rate';
33
33
  export { SliderV2 } from './SliderV2';
34
34
  export * from './Flex';
35
- export * from './RateContinuous';
35
+ export * from './RateV2';
@@ -9,9 +9,10 @@ const defaultProps = {
9
9
  cellStyle: {},
10
10
  cellActiveStyle: {},
11
11
  isPreviewMode: false,
12
+ isCompact: false,
12
13
  };
13
14
  export const ResizeGrid = props => {
14
- const { containerStyle, cellBorderStyle, cellStyle, cellActiveStyle, isPreviewMode, renderCell, grid: initGrid, image, imageSize, imagePosition, } = props;
15
+ const { containerStyle, cellBorderStyle, cellStyle, cellActiveStyle, isPreviewMode, renderCell, grid: initGrid, image, imageSize, imagePosition, isCompact, } = props;
15
16
  const [grid, setGrid] = useState(buildGrid(initGrid || GRID_TEMPLATE.GRID_2_3));
16
17
  const startRef = useRef(null);
17
18
  const startCellRef = useRef(null);
@@ -347,6 +348,6 @@ export const ResizeGrid = props => {
347
348
  // console.log('grid', { gridTemplateColumns, gridTemplateRows, childs });
348
349
  return (React.createElement("div", { className: "grid-container", ref: containerRef, style: Object.assign({ backgroundImage: `url(${image})`, backgroundSize: imageSize, backgroundPosition: imagePosition, gridTemplateColumns: gridTemplateColumns.map(fr => `${fr}%`).join(' '), gridTemplateRows: gridTemplateRows.map(fr => `${fr}%`).join(' ') }, containerStyle) }, childs.map((cell, idx) => (React.createElement(Cell, { key: idx, idx: idx, cell: cell, down: handleDown, downCell: handleDownCell, up: handleUp, setRef: setRef, downSelect: downSelect, upSelect: upSelect, hoverSelect: hoverSelect, unMerge: unMerge, renderCell: renderCell, borderStyle: cellBorderStyle, style: cellStyle, activeStyle: cellActiveStyle, isPreviewMode: isPreviewMode })))));
349
350
  };
350
- return React.createElement(StyledGrid, { className: "grid-wrapper" }, renderGrid());
351
+ return (React.createElement(StyledGrid, { className: "grid-wrapper", isCompact: !!isCompact }, renderGrid()));
351
352
  };
352
353
  ResizeGrid.defaultProps = defaultProps;
@@ -1 +1,3 @@
1
- export declare const StyledGrid: import("styled-components").StyledComponent<"div", any, {}, never>;
1
+ export declare const StyledGrid: import("styled-components").StyledComponent<"div", any, {
2
+ isCompact: boolean;
3
+ }, never>;
@@ -2,7 +2,6 @@
2
2
  import styled from 'styled-components';
3
3
  export const StyledGrid = styled.div `
4
4
  width: 100%;
5
- aspect-ratio: 1.5;
6
5
  box-sizing: border-box;
7
6
 
8
7
  * {
@@ -16,5 +15,6 @@ export const StyledGrid = styled.div `
16
15
  background-repeat: no-repeat;
17
16
  background-position: top left;
18
17
  background-size: 100% 100%;
18
+ aspect-ratio: ${props => (props.isCompact ? '3 / 1' : '3 / 2')};
19
19
  }
20
20
  `;
@@ -124,4 +124,5 @@ export interface IResizeGridProps {
124
124
  cellBorderStyle?: string;
125
125
  cellActiveStyle?: React.CSSProperties;
126
126
  containerStyle?: React.CSSProperties;
127
+ isCompact?: boolean;
127
128
  }
@@ -0,0 +1 @@
1
+ export declare function useDeepCompareEffect(callback: any, dependencies: any): void;
@@ -0,0 +1,21 @@
1
+ import { useEffect, useRef } from 'react';
2
+ import isEqual from 'react-fast-compare';
3
+ function deepCompareEquals(a, b) {
4
+ // TODO: implement deep comparison here
5
+ // something like lodash
6
+ // return _.isEqual(a, b);
7
+ return isEqual(a, b);
8
+ }
9
+ function useDeepCompareMemoize(value) {
10
+ const ref = useRef();
11
+ // it can be done by using useMemo as well
12
+ // but useRef is rather cleaner and easier
13
+ if (!deepCompareEquals(value, ref.current)) {
14
+ ref.current = value;
15
+ }
16
+ return ref.current;
17
+ }
18
+ export function useDeepCompareEffect(callback, dependencies) {
19
+ // eslint-disable-next-line react-hooks/exhaustive-deps
20
+ useEffect(callback, dependencies.map(useDeepCompareMemoize));
21
+ }
package/es/test.js CHANGED
@@ -4,7 +4,7 @@ import { createRoot } from 'react-dom/client';
4
4
  import '@antscorp/icons/main.css';
5
5
  import { ConfigProvider, EdgeSetting, GradientSetting, IconSelection, PositionSetting, SettingWrapper,
6
6
  // Slider,
7
- SliderWithInputNumber, Space, UploadImage, RateContinuous, } from './components';
7
+ SliderWithInputNumber, Space, UploadImage, RateV2, } from './components';
8
8
  // import { Slider } from 'antd';
9
9
  import { Slider } from '@antscorp/antsomi-ui/es/components/atoms/Slider';
10
10
  import { Rate } from './components/atoms/Rate';
@@ -86,7 +86,7 @@ export const App = () => {
86
86
  // );
87
87
  // ---------------------------- Test Helps End ------------------------------
88
88
  // ------------------------ Media template components test start -------------------------
89
- const [starValue, setStarValue] = useState(1.5);
89
+ const [starValue, setStarValue] = useState(1.3);
90
90
  return (React.createElement(ConfigProvider, null,
91
91
  React.createElement("div", { style: {
92
92
  width: 500,
@@ -99,7 +99,7 @@ export const App = () => {
99
99
  } },
100
100
  React.createElement("br", null),
101
101
  React.createElement("br", null),
102
- React.createElement(RateContinuous, { value: starValue, onChange: value => setStarValue(value) }),
102
+ React.createElement(RateV2, { value: starValue, onChange: value => setStarValue(value) }),
103
103
  React.createElement("br", null),
104
104
  React.createElement("br", null),
105
105
  React.createElement("br", null),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@antscorp/antsomi-ui",
3
- "version": "1.3.3-beta.18",
3
+ "version": "1.3.3-beta.19",
4
4
  "description": "An enterprise-class UI design language and React UI library.",
5
5
  "sideEffects": [
6
6
  "dist/*",
@@ -1 +0,0 @@
1
- export { RateContinuous } from './RateContinuous';
@@ -1 +0,0 @@
1
- export { RateContinuous } from './RateContinuous';