3
3
import { jsx } from '@emotion/react'
4
4
import type { CSSProperties } from 'react'
5
5
import React from 'react'
6
+ import type { CSSNumberUnit } from '../../components/inspector/common/css-utils'
6
7
import {
7
8
cssKeyword ,
8
9
gridDimensionsAreEqual ,
10
+ isFR ,
9
11
isGridCSSNumber ,
10
12
isValidGridDimensionKeyword ,
11
13
parseCSSNumber ,
@@ -28,6 +30,9 @@ import { Icons, SmallerIcons } from '../icons'
28
30
import { NO_OP } from '../../core/shared/utils'
29
31
import { unless } from '../../utils/react-conditionals'
30
32
import { useColorTheme , UtopiaTheme } from '../styles/theme'
33
+ import type { Optic } from '../../core/shared/optics/optics'
34
+ import { fromField , fromTypeGuard , notNull } from '../../core/shared/optics/optic-creators'
35
+ import { exists , modify } from '../../core/shared/optics/optic-utilities'
31
36
32
37
interface GridExpressionInputProps {
33
38
testId : string
@@ -43,6 +48,8 @@ interface GridExpressionInputProps {
43
48
44
49
const DropdownWidth = 25
45
50
51
+ const ArrowKeyFractionalIncrement = 0.1
52
+
46
53
export const GridExpressionInput = React . memo (
47
54
( {
48
55
testId,
@@ -66,32 +73,56 @@ export const GridExpressionInput = React.memo(
66
73
67
74
const onKeyDown = React . useCallback (
68
75
( e : React . KeyboardEvent ) => {
69
- if ( e . key !== 'Enter' ) {
70
- return
71
- }
72
- if ( isValidGridDimensionKeyword ( printValue ) ) {
73
- return onUpdateNumberOrKeyword ( cssKeyword ( printValue ) )
74
- }
76
+ switch ( e . key ) {
77
+ case 'Enter' :
78
+ if ( isValidGridDimensionKeyword ( printValue ) ) {
79
+ return onUpdateNumberOrKeyword ( cssKeyword ( printValue ) )
80
+ }
75
81
76
- const defaultUnit = isGridCSSNumber ( value ) ? value . value . unit : 'px'
77
- const maybeNumber = parseCSSNumber ( printValue , 'AnyValid' , defaultUnit )
78
- if ( isRight ( maybeNumber ) ) {
79
- return onUpdateNumberOrKeyword ( maybeNumber . value )
80
- }
82
+ const defaultUnit = isGridCSSNumber ( value ) ? value . value . unit : 'px'
83
+ const maybeNumber = parseCSSNumber ( printValue , 'AnyValid' , defaultUnit )
84
+ if ( isRight ( maybeNumber ) ) {
85
+ return onUpdateNumberOrKeyword ( maybeNumber . value )
86
+ }
81
87
82
- const maybeMinmax = parseGridCSSMinmaxOrRepeat ( printValue )
83
- if ( maybeMinmax != null ) {
84
- return onUpdateDimension ( {
85
- ...maybeMinmax ,
86
- lineName : value . lineName ,
87
- } as GridDimension )
88
- }
88
+ const maybeMinmax = parseGridCSSMinmaxOrRepeat ( printValue )
89
+ if ( maybeMinmax != null ) {
90
+ return onUpdateDimension ( {
91
+ ...maybeMinmax ,
92
+ lineName : value . lineName ,
93
+ } as GridDimension )
94
+ }
89
95
90
- if ( printValue === '' ) {
91
- return onUpdateNumberOrKeyword ( cssKeyword ( 'auto' ) )
92
- }
96
+ if ( printValue === '' ) {
97
+ return onUpdateNumberOrKeyword ( cssKeyword ( 'auto' ) )
98
+ }
93
99
94
- setPrintValue ( stringifyGridDimension ( value ) )
100
+ setPrintValue ( stringifyGridDimension ( value ) )
101
+ break
102
+ case 'ArrowUp' :
103
+ case 'ArrowDown' :
104
+ e . preventDefault ( )
105
+ const gridNumberValueOptic : Optic < GridDimension , CSSNumber > = fromTypeGuard (
106
+ isGridCSSNumber ,
107
+ ) . compose ( fromField ( 'value' ) )
108
+ const valueUnitOptic : Optic < GridDimension , 'fr' > = gridNumberValueOptic
109
+ . compose ( fromField ( 'unit' ) )
110
+ . compose ( notNull ( ) )
111
+ . compose ( fromTypeGuard ( isFR ) )
112
+ const gridNumberNumberOptic : Optic < GridDimension , number > =
113
+ gridNumberValueOptic . compose ( fromField ( 'value' ) )
114
+ if ( exists ( valueUnitOptic , value ) ) {
115
+ function updateFractional ( fractionalValue : number ) : number {
116
+ return (
117
+ fractionalValue +
118
+ ( e . key === 'ArrowUp' ? ArrowKeyFractionalIncrement : - ArrowKeyFractionalIncrement )
119
+ )
120
+ }
121
+ const updatedDimension = modify ( gridNumberNumberOptic , updateFractional , value )
122
+ onUpdateDimension ( updatedDimension )
123
+ }
124
+ break
125
+ }
95
126
} ,
96
127
[ printValue , onUpdateNumberOrKeyword , onUpdateDimension , value ] ,
97
128
)
0 commit comments