Skip to content
This repository was archived by the owner on Sep 20, 2024. It is now read-only.

Commit f8273e3

Browse files
committed
refactor(select): select component to use attrs api
1 parent 83a8054 commit f8273e3

File tree

3 files changed

+105
-82
lines changed

3 files changed

+105
-82
lines changed

packages/chakra-ui-core/src/CIconButton/CIconButton.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ const CIconButton = {
6464
click: (e) => {
6565
const emitClick = context.listeners.click
6666
if (emitClick) {
67-
emitClick('click', e)
67+
emitClick(e)
6868
}
6969
}
7070
}

packages/chakra-ui-core/src/CSelect/CSelect.js

Lines changed: 67 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,12 @@
88
* @see Source https://github.com/chakra-ui/chakra-ui-vue/blob/master/packages/chakra-ui-core/src/CSelect/CSelect.js
99
*/
1010

11-
import { baseProps } from '../config'
12-
import styleProps from '../config/props'
1311
import { inputProps } from '../CInput/utils/input.props'
1412

1513
import CBox from '../CBox'
1614
import CIcon from '../CIcon'
1715
import CInput from '../CInput'
16+
import { createStyledAttrsMixin, extractListeners, forwardProps } from '../utils'
1817
import splitProps from './utils/select.utils'
1918

2019
/**
@@ -27,11 +26,12 @@ import splitProps from './utils/select.utils'
2726
*/
2827
const CSelectIconWrapper = {
2928
name: 'SelectIconWrapper',
30-
props: baseProps,
31-
render (h) {
29+
functional: true,
30+
render (h, { data, slots, ...rest }) {
3231
return h(CBox, {
33-
props: {
34-
...this.$props,
32+
...rest,
33+
attrs: {
34+
...data.attrs,
3535
position: 'absolute',
3636
display: 'inline-flex',
3737
width: '1.5rem',
@@ -42,12 +42,10 @@ const CSelectIconWrapper = {
4242
top: '50%',
4343
pointerEvents: 'none',
4444
zIndex: 2,
45-
transform: 'translateY(-50%)'
46-
},
47-
attrs: {
45+
transform: 'translateY(-50%)',
4846
'data-chakra-component': 'CSelectIconWrapper'
4947
}
50-
}, this.$slots.default)
48+
}, slots().default)
5149
}
5250
}
5351

@@ -61,35 +59,51 @@ const CSelectIconWrapper = {
6159
*/
6260
const CSelectInput = {
6361
name: 'CSelectInput',
62+
functional: true,
6463
props: {
65-
...styleProps,
6664
...inputProps,
6765
placeholder: String,
6866
value: String
6967
},
70-
render (h) {
68+
render (h, { props, data, slots, listeners, ...rest }) {
69+
const nonNativeEvents = {
70+
change: (e) => {
71+
const emitChange = listeners.change
72+
if (emitChange) {
73+
emitChange(e)
74+
}
75+
}
76+
}
77+
78+
const { native, nonNative } = extractListeners({ listeners }, nonNativeEvents)
79+
console.log(forwardProps(props))
7180
return h(CInput, {
81+
...rest,
7282
props: {
73-
...this.$props,
83+
...forwardProps(props),
84+
as: 'select'
85+
},
86+
on: nonNative,
87+
nativeOn: native,
88+
domProps: {
89+
value: props.value
90+
},
91+
attrs: {
7492
as: 'select',
7593
appearance: 'none',
7694
pr: '2rem',
7795
pb: 'px',
78-
lineHeight: 'normal'
79-
},
80-
on: {
81-
change: e => this.$emit('change', e)
82-
},
83-
domProps: {
84-
value: this.value
96+
lineHeight: 'normal',
97+
...data.attrs,
98+
'data-chakra-component': 'CSelectInput'
8599
}
86100
}, [
87-
this.placeholder && h('option', {
101+
props.placeholder && h('option', {
88102
attrs: {
89103
value: ''
90104
}
91-
}, this.placeholder),
92-
this.$slots.default
105+
}, props.placeholder),
106+
slots().default
93107
])
94108
}
95109
}
@@ -103,14 +117,12 @@ const CSelectInput = {
103117
* @see Docs https://vue.chakra-ui.com/select
104118
*/
105119
const CSelect = {
106-
name: 'CSelect',
107-
inject: ['$chakraColorMode'],
120+
mixins: [createStyledAttrsMixin('CSelect', true)],
108121
model: {
109122
prop: 'value',
110123
event: 'change'
111124
},
112125
props: {
113-
...styleProps,
114126
...inputProps,
115127
rootProps: {
116128
type: Object,
@@ -134,9 +146,6 @@ const CSelect = {
134146
}
135147
},
136148
computed: {
137-
colorMode () {
138-
return this.$chakraColorMode()
139-
},
140149
_color () {
141150
return this.colorMode === 'dark' ? 'whiteAlpha.800' : 'inherit'
142151
},
@@ -145,45 +154,56 @@ const CSelect = {
145154
},
146155
_value () {
147156
return this.value
148-
}
149-
},
150-
render (h) {
151-
const { rootProps, icon, iconSize, ...props } = this.$props
152-
const [root, select] = splitProps(props)
153-
return h(CBox, {
154-
props: {
157+
},
158+
allSplitProps () {
159+
return splitProps(this.$data.attrs$)
160+
},
161+
componentStyles () {
162+
const [root] = this.allSplitProps
163+
return {
155164
...root,
156-
...rootProps,
165+
...this.rootProps,
157166
position: 'relative',
158167
width: '100%'
159-
},
168+
}
169+
}
170+
},
171+
render (h) {
172+
const { icon, iconSize } = this.$props
173+
const [, select] = this.allSplitProps
174+
return h('div', {
175+
class: [this.className],
160176
attrs: {
177+
...this.computedAttrs,
161178
'data-chakra-component': 'CSelect'
162179
}
163180
}, [
164181
h(CSelectInput, {
165182
props: {
166-
color: this._color,
167183
placeholder: this.placeholder,
168-
...select
169-
},
170-
on: {
171-
change: e => this.$emit('change', e.target.value)
184+
...forwardProps(this.$props)
172185
},
173-
domProps: {
186+
attrs: {
187+
color: this._color,
188+
...select,
174189
value: this._value
190+
},
191+
on: {
192+
change: (e) => {
193+
this.$emit('change', e.target.value)
194+
}
175195
}
176196
}, this.$slots.default),
177197
h(CSelectIconWrapper, {
178-
props: {
198+
attrs: {
179199
opacity: this._opacity,
180200
color: select.color || this._color
181201
}
182202
}, [
183203
h(CIcon, {
184204
props: {
185-
name: this.icon || 'chevron-down',
186-
size: this.iconSize
205+
name: icon || 'chevron-down',
206+
size: iconSize
187207
},
188208
attrs: {
189209
focusable: false,

packages/chakra-ui-core/src/CSelect/utils/select.utils.js

Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,36 @@
1-
const rootOptions = [
2-
'm',
3-
'mt',
4-
'mr',
5-
'mb',
6-
'ml',
7-
'mx',
8-
'my',
9-
'margin',
10-
'marginTop',
11-
'marginBottom',
12-
'marginLeft',
13-
'marginRight',
14-
'marginY',
15-
'marginX',
16-
'flex',
17-
'flexBasis',
18-
'width',
19-
'minWidth',
20-
'maxWidth',
21-
'maxW',
22-
'minW',
23-
'w',
24-
'zIndex',
25-
'top',
26-
'right',
27-
'bottom',
28-
'left',
29-
'position',
30-
'pos'
31-
]
1+
import { camelize } from '../../utils'
2+
3+
const rootOptions = {
4+
m: true,
5+
mt: true,
6+
mr: true,
7+
mb: true,
8+
ml: true,
9+
mx: true,
10+
my: true,
11+
margin: true,
12+
marginTop: true,
13+
marginBottom: true,
14+
marginLeft: true,
15+
marginRight: true,
16+
marginY: true,
17+
marginX: true,
18+
flex: true,
19+
flexBasis: true,
20+
width: true,
21+
minWidth: true,
22+
maxWidth: true,
23+
maxW: true,
24+
minW: true,
25+
w: true,
26+
zIndex: true,
27+
top: true,
28+
right: true,
29+
bottom: true,
30+
left: true,
31+
position: true,
32+
pos: true
33+
}
3234

3335
/**
3436
* Splits all input[type="select"] props from the root node props
@@ -39,10 +41,11 @@ const splitProps = (props) => {
3941
const rootProps = {}
4042
const selectProps = {}
4143
for (const key in props) {
42-
if (rootOptions.includes(key)) {
43-
rootProps[key] = props[key]
44+
const _key = camelize(key)
45+
if (rootOptions[_key]) {
46+
rootProps[_key] = props[key]
4447
} else {
45-
selectProps[key] = props[key]
48+
selectProps[_key] = props[key]
4649
}
4750
}
4851
return [rootProps, selectProps]

0 commit comments

Comments
 (0)