Skip to content

Commit fc555c9

Browse files
committed
fix: codes
1 parent 38c1dc3 commit fc555c9

File tree

2 files changed

+35
-22
lines changed

2 files changed

+35
-22
lines changed

example/src/stories/Select.stories.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,12 @@ CustomRender.args = {
3232
items: ['kr', 'gb', 'us', 'jp'],
3333
render: (s: string) => (
3434
<>
35-
<img src={`https://flagicons.lipis.dev/flags/4x3/${s}.svg`} alt={s} /> {s}
35+
<img
36+
src={`https://flagicons.lipis.dev/flags/4x3/${s}.svg`}
37+
alt={s}
38+
style={{ height: '1em' }}
39+
/>{' '}
40+
{s}
3641
</>
3742
),
3843
}

src/components/Select.tsx

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import styled from '@emotion/styled'
12
import {
23
autoUpdate,
34
flip,
@@ -26,6 +27,25 @@ import React, {
2627
useState
2728
} from 'react'
2829
import { PC, PP } from '../types/PolymorphicElementProps'
30+
import { cssDisablable } from '../utils/styles'
31+
32+
interface SelectDisplayProps {
33+
fullWidth: boolean
34+
}
35+
36+
const SelectDisplay = styled.div<SelectDisplayProps>`
37+
${cssDisablable}
38+
font-family: inherit;
39+
height: auto;
40+
line-height: normal;
41+
font-size: 1rem;
42+
padding: 0.8em 0.5em;
43+
background: ${({ theme }) => theme.color.background.footer};
44+
color: ${({ theme }) => theme.color.text.primary.main};
45+
border: ${({ theme }) => theme.styles.border()};
46+
border-radius: 8px;
47+
width: ${({ fullWidth }) => (fullWidth ? '100%' : 'auto')};
48+
`
2949

3050
// Adopted from https://codesandbox.io/s/shy-snowflake-kp6479?file=/src/Select.tsx:5939-5954
3151

@@ -70,7 +90,6 @@ export const Select: PC<'button', SelectProps> = React.forwardRef(
7090
const [innerOffset, setInnerOffset] = useState(0)
7191
const [controlledScrolling, setControlledScrolling] = useState(false)
7292
const [touch, setTouch] = useState(false)
73-
const [blockSelection, setBlockSelection] = useState(false)
7493

7594
useEffect(() => {
7695
if (onChange) onChange(items[selectedIndex])
@@ -159,12 +178,9 @@ export const Select: PC<'button', SelectProps> = React.forwardRef(
159178
allowMouseUpRef.current = true
160179
setInnerOffset(0)
161180
setFallback(false)
162-
setBlockSelection(false)
163181
return undefined
164182
}, [open])
165183

166-
// Replacement for `useDismiss` as the arrows are outside of the floating
167-
// element DOM tree.
168184
useLayoutEffect(() => {
169185
const onPointerDown = (e: PointerEvent): void => {
170186
const target = e.target as Node
@@ -186,8 +202,6 @@ export const Select: PC<'button', SelectProps> = React.forwardRef(
186202
return undefined
187203
}, [open, refs])
188204

189-
// Scroll the `activeIndex` item into view only in "controlledScrolling"
190-
// (keyboard nav) mode.
191205
useLayoutEffect(() => {
192206
if (open && controlledScrolling) {
193207
requestAnimationFrame(() => {
@@ -198,7 +212,6 @@ export const Select: PC<'button', SelectProps> = React.forwardRef(
198212
}
199213
}, [open, refs, controlledScrolling, activeIndex])
200214

201-
// Scroll the `selectedIndex` into view upon opening the floating element.
202215
useLayoutEffect(() => {
203216
if (open && fallback) {
204217
requestAnimationFrame(() => {
@@ -209,8 +222,6 @@ export const Select: PC<'button', SelectProps> = React.forwardRef(
209222
}
210223
}, [open, fallback, selectedIndex])
211224

212-
// Unset the height limiting for fallback mode. This gets executed prior to
213-
// the positioning call.
214225
useLayoutEffect(() => {
215226
if (refs.floating.current && fallback) {
216227
refs.floating.current.style.maxHeight = ''
@@ -221,7 +232,10 @@ export const Select: PC<'button', SelectProps> = React.forwardRef(
221232

222233
return (
223234
<React.Fragment>
224-
<button
235+
<SelectDisplay
236+
fullWidth={fullWidth}
237+
role="button"
238+
tabIndex={0}
225239
type="button"
226240
ref={reference}
227241
{...getReferenceProps({
@@ -237,13 +251,12 @@ export const Select: PC<'button', SelectProps> = React.forwardRef(
237251
{...rest}
238252
>
239253
{selected ? render(selected) : null}
240-
</button>
254+
</SelectDisplay>
241255
{open && (
242256
<FloatingOverlay lockScroll={!touch} style={{ zIndex: 1 }}>
243257
<FloatingFocusManager context={context} preventTabbing>
244258
<div
245259
ref={floating}
246-
className="MacSelect"
247260
style={{
248261
position: strategy,
249262
top: y ?? 0,
@@ -263,14 +276,11 @@ export const Select: PC<'button', SelectProps> = React.forwardRef(
263276
>
264277
{items.map((item, i) => {
265278
return (
266-
<button
267-
type="button"
279+
<div
268280
key={typeof item === 'string' ? item : item.value}
269-
// Prevent immediate selection on touch devices when
270-
// pressing the ScrollArrows
271-
disabled={blockSelection}
272-
aria-selected={selectedIndex === i}
273281
role="option"
282+
tabIndex={0}
283+
aria-selected={selectedIndex === i}
274284
style={{
275285
// background:
276286
// activeIndex === i
@@ -309,8 +319,6 @@ export const Select: PC<'button', SelectProps> = React.forwardRef(
309319
setOpen(false)
310320
}
311321

312-
// On touch devices, prevent the element from
313-
// immediately closing `onClick` by deferring it
314322
clearTimeout(selectTimeoutRef.current)
315323
selectTimeoutRef.current = setTimeout(() => {
316324
allowSelectRef.current = true
@@ -319,7 +327,7 @@ export const Select: PC<'button', SelectProps> = React.forwardRef(
319327
})}
320328
>
321329
{render(item)}
322-
</button>
330+
</div>
323331
)
324332
})}
325333
</div>

0 commit comments

Comments
 (0)