Skip to content

Commit

Permalink
chore: local dev, name attrs, clear icon fixes, refactors. (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
morganney authored Oct 21, 2023
1 parent a04b920 commit af2869a
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 8 deletions.
3 changes: 3 additions & 0 deletions packages/components/src/autoSuggest/mod.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ interface AutoSuggestProps<T> {
placeholder?: string
labelledBy?: string
id?: string
name?: string
}
interface Label {
label: string
Expand Down Expand Up @@ -186,6 +187,7 @@ const getChangeEvt = (value?: string | null): ChangeEvent<HTMLInputElement> => {
}
const AutoSuggest = <U,>({
id,
name,
items,
value,
onBlur,
Expand Down Expand Up @@ -433,6 +435,7 @@ const AutoSuggest = <U,>({
}
})}
id={id}
name={name}
color={color}
size={size}
fontSize={sizing[size].fontSize}
Expand Down
34 changes: 29 additions & 5 deletions packages/components/src/input/mod.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { forwardRef, useCallback, useState, useRef } from 'react'
import { forwardRef, useCallback, useState, useRef, useEffect } from 'react'
import styled from 'styled-components'

import { Close as CloseIcon } from '../icons/close/mod.js'
Expand All @@ -9,6 +9,7 @@ import type { Size } from '../types.js'

interface InputProps {
id?: string
name?: string
size?: Size
labelledBy?: string
list?: string
Expand Down Expand Up @@ -77,6 +78,7 @@ const StyledInput = styled.input<{
const Input = forwardRef<HTMLInputElement, InputProps>(function Input(
{
id,
name,
list,
labelledBy,
value,
Expand All @@ -97,10 +99,10 @@ const Input = forwardRef<HTMLInputElement, InputProps>(function Input(
) {
const wrapper = useRef<HTMLSpanElement>(null)
const isClearable = typeof onClear === 'function'
const [hasFocus, setHasFocus] = useState(false)
const [showClearIcon, setShowClearIcon] = useState(false)
const handleOnFocus = useCallback(
(evt: FocusEvent) => {
setHasFocus(true)
setShowClearIcon(true)

if (typeof onFocus === 'function') {
onFocus(evt)
Expand All @@ -110,7 +112,7 @@ const Input = forwardRef<HTMLInputElement, InputProps>(function Input(
)
const handleOnBlur = useCallback((evt: FocusEvent) => {
if (!evt.currentTarget.contains(evt.relatedTarget)) {
setHasFocus(false)
setShowClearIcon(false)
}
}, [])
const handleOnClear = useCallback(() => {
Expand All @@ -121,10 +123,32 @@ const Input = forwardRef<HTMLInputElement, InputProps>(function Input(
}
}, [onClear])

useEffect(() => {
/**
* When the AutoSuggest changes values, either from
* user interaction or dynamic scripting, specifically
* when `onSelectedItemChanged` is invoked, the blur
* event can not fire and the state to hide the clear icon
* does not update correctly.
*
* This corrects for that by resetting the state whenever
* the input state says to show the clear icon while the
* <input /> is not the activeElement.
*/
if (
showClearIcon &&
wrapper.current?.querySelector('input') !== document.activeElement
) {
setShowClearIcon(false)
}
}, [value, showClearIcon])

return (
<Wrapper ref={wrapper} onBlur={handleOnBlur}>
<StyledInput
id={id}
name={name ?? id}
title={value?.toString() || undefined}
ref={ref}
list={list}
type={type}
Expand All @@ -142,7 +166,7 @@ const Input = forwardRef<HTMLInputElement, InputProps>(function Input(
borderColor={borderColor}
aria-labelledby={labelledBy}
/>
{value && !isDisabled && isClearable && hasFocus && (
{value && !isDisabled && isClearable && showClearIcon && (
<Close onClick={handleOnClear} tabIndex={0} size="small" />
)}
</Wrapper>
Expand Down
2 changes: 1 addition & 1 deletion packages/components/src/toast/story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export default {
type: 'info',
variant: 'standard',
message: 'A toast message',
position: 'bottom left',
position: 'top center',
timeout: 6_000
},
argTypes: {
Expand Down
1 change: 1 addition & 0 deletions packages/ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"cycles": "madge --circular --warning --exclude node_modules --ts-config ./tsconfig.json --extensions ts,tsx src",
"prettier": "prettier -w .",
"dev": "vite",
"dev:local": "RESTBUS_HOST=http://localhost:3000 vite",
"dev:node": "tsx devServer.ts",
"lint": "eslint . src --ext .ts,.tsx --max-warnings 0",
"lint:fix": "npm run lint -- --fix",
Expand Down
1 change: 1 addition & 0 deletions packages/ui/src/components/selectors/agencies.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const Agencies: FC<Props> = ({ agencies, selected, isDisabled, onSelect }) => {
onClear
caseInsensitive
inputBoundByItems
name="agencies"
size="small"
color="black"
value={selected ?? undefined}
Expand Down
1 change: 1 addition & 0 deletions packages/ui/src/components/selectors/directions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const Directions: FC<Props> = ({
inputBoundByItems
size="small"
color="black"
name="directions"
itemToString={itemToString}
value={selected ?? undefined}
isDisabled={isDisabled}
Expand Down
1 change: 1 addition & 0 deletions packages/ui/src/components/selectors/routes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const Routes: FC<Props> = ({
inputBoundByItems
size="small"
color="black"
name="routes"
value={selected ?? undefined}
isDisabled={isDisabled}
placeholder={`Routes ... ${routes ? `(${routes.length})` : ''}`}
Expand Down
1 change: 1 addition & 0 deletions packages/ui/src/components/selectors/stops.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const Stops: FC<Props> = ({
inputBoundByItems
size="small"
color="black"
name="stops"
value={selected ?? undefined}
isDisabled={isDisabled}
placeholder={`Stops ... ${stops.length ? `(${stops.length})` : ''}`}
Expand Down
9 changes: 8 additions & 1 deletion packages/ui/src/components/settings/vehicle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,12 @@ const VehicleSettings: FC = () => {
direction="horizontal-rev"
justifyContent="flex-end"
fontWeight="normal">
<input type="checkbox" checked={vehicle.visible} onChange={onChangeVisible} />
<input
type="checkbox"
name="visible"
checked={vehicle.visible}
onChange={onChangeVisible}
/>
</FormItem>
<FormItem
label="Color predicted"
Expand All @@ -81,6 +86,7 @@ const VehicleSettings: FC = () => {
fontWeight="normal">
<input
type="checkbox"
name="colorPredicted"
disabled={!vehicle.visible}
checked={vehicle.markPredictedVehicles}
onChange={onTogglePredictedVehicles}
Expand All @@ -94,6 +100,7 @@ const VehicleSettings: FC = () => {
fontWeight="normal">
<input
type="checkbox"
name="hideOther"
disabled={!vehicle.visible}
checked={vehicle.hideOtherDirections}
onChange={onToggleHideOtherDirections}
Expand Down
11 changes: 10 additions & 1 deletion packages/ui/vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { env } from 'node:process'

import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'

Expand All @@ -13,7 +15,14 @@ export default defineConfig(() => {
})
],
server: {
host: true
host: true,
proxy: !env.RESTBUS_HOST
? undefined
: {
'/restbus': {
target: env.RESTBUS_HOST
}
}
}
}
})

0 comments on commit af2869a

Please sign in to comment.