Fix line height, add suffix
This commit is contained in:
@@ -1,5 +1,11 @@
|
|||||||
import React from 'react'
|
import React from 'react'
|
||||||
import {View, TextInput, TextInputProps, TextStyle} from 'react-native'
|
import {
|
||||||
|
View,
|
||||||
|
TextInput,
|
||||||
|
TextInputProps,
|
||||||
|
TextStyle,
|
||||||
|
LayoutChangeEvent,
|
||||||
|
} from 'react-native'
|
||||||
|
|
||||||
import {useTheme, atoms, web, tokens} from '#/alf'
|
import {useTheme, atoms, web, tokens} from '#/alf'
|
||||||
import {Text} from '#/view/com/Typography'
|
import {Text} from '#/view/com/Typography'
|
||||||
@@ -9,15 +15,23 @@ type Props = Omit<TextInputProps, 'placeholder'> & {
|
|||||||
placeholder: string
|
placeholder: string
|
||||||
hasError?: boolean
|
hasError?: boolean
|
||||||
icon?: React.FunctionComponent<any>
|
icon?: React.FunctionComponent<any>
|
||||||
|
suffix?: React.FunctionComponent<any>
|
||||||
}
|
}
|
||||||
|
|
||||||
export function InputText({label, hasError, icon: Icon, ...props}: Props) {
|
export function InputText({
|
||||||
|
label,
|
||||||
|
hasError,
|
||||||
|
icon: Icon,
|
||||||
|
suffix: Suffix,
|
||||||
|
...props
|
||||||
|
}: Props) {
|
||||||
const labelId = React.useId()
|
const labelId = React.useId()
|
||||||
const t = useTheme()
|
const t = useTheme()
|
||||||
const [state, setState] = React.useState({
|
const [state, setState] = React.useState({
|
||||||
hovered: false,
|
hovered: false,
|
||||||
focused: false,
|
focused: false,
|
||||||
})
|
})
|
||||||
|
const [suffixPadding, setSuffixPadding] = React.useState(0)
|
||||||
|
|
||||||
const onHoverIn = React.useCallback(() => {
|
const onHoverIn = React.useCallback(() => {
|
||||||
setState(s => ({
|
setState(s => ({
|
||||||
@@ -43,6 +57,12 @@ export function InputText({label, hasError, icon: Icon, ...props}: Props) {
|
|||||||
focused: false,
|
focused: false,
|
||||||
}))
|
}))
|
||||||
}, [setState])
|
}, [setState])
|
||||||
|
const handleSuffixLayout = React.useCallback(
|
||||||
|
(e: LayoutChangeEvent) => {
|
||||||
|
setSuffixPadding(e.nativeEvent.layout.width + 16)
|
||||||
|
},
|
||||||
|
[setSuffixPadding],
|
||||||
|
)
|
||||||
|
|
||||||
const {inputStyles, iconStyles} = React.useMemo(() => {
|
const {inputStyles, iconStyles} = React.useMemo(() => {
|
||||||
const input: TextStyle[] = []
|
const input: TextStyle[] = []
|
||||||
@@ -113,31 +133,52 @@ export function InputText({label, hasError, icon: Icon, ...props}: Props) {
|
|||||||
atoms.text_md,
|
atoms.text_md,
|
||||||
t.atoms.border,
|
t.atoms.border,
|
||||||
t.atoms.text,
|
t.atoms.text,
|
||||||
{borderWidth: 2},
|
|
||||||
web({
|
web({
|
||||||
paddingTop: atoms.pt_md.paddingTop - 1,
|
paddingTop: atoms.pt_md.paddingTop - 1,
|
||||||
}),
|
}),
|
||||||
|
{paddingRight: suffixPadding},
|
||||||
|
{borderWidth: 2, lineHeight: atoms.text_md.lineHeight * 1.1875},
|
||||||
...inputStyles,
|
...inputStyles,
|
||||||
...(Array.isArray(props.style) ? props.style : [props.style]),
|
...(Array.isArray(props.style) ? props.style : [props.style]),
|
||||||
]}
|
]}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
{Icon && (
|
{Icon && (
|
||||||
<Icon
|
<View
|
||||||
style={[
|
style={[
|
||||||
atoms.absolute,
|
atoms.absolute,
|
||||||
atoms.inset_0,
|
atoms.inset_0,
|
||||||
{color: t.atoms.border_contrast_500.borderColor},
|
atoms.align_center,
|
||||||
{
|
atoms.justify_center,
|
||||||
left: atoms.px_md.paddingLeft,
|
atoms.pl_md,
|
||||||
right: 'auto',
|
{right: 'auto'},
|
||||||
width: 20,
|
]}>
|
||||||
marginVertical: 'auto',
|
<Icon
|
||||||
pointerEvents: 'none',
|
style={[
|
||||||
},
|
{color: t.atoms.border_contrast_500.borderColor},
|
||||||
...iconStyles,
|
{
|
||||||
]}
|
width: 20,
|
||||||
/>
|
pointerEvents: 'none',
|
||||||
|
},
|
||||||
|
...iconStyles,
|
||||||
|
]}
|
||||||
|
/>
|
||||||
|
</View>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{Suffix && (
|
||||||
|
<View
|
||||||
|
onLayout={handleSuffixLayout}
|
||||||
|
style={[
|
||||||
|
atoms.absolute,
|
||||||
|
atoms.inset_0,
|
||||||
|
atoms.align_center,
|
||||||
|
atoms.justify_center,
|
||||||
|
atoms.pr_lg,
|
||||||
|
{left: 'auto'},
|
||||||
|
]}>
|
||||||
|
<Suffix />
|
||||||
|
</View>
|
||||||
)}
|
)}
|
||||||
</View>
|
</View>
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -220,6 +220,12 @@ function Forms() {
|
|||||||
<InputText label="Name" placeholder="Type here" />
|
<InputText label="Name" placeholder="Type here" />
|
||||||
<InputText placeholder="Type here" icon={Logo} />
|
<InputText placeholder="Type here" icon={Logo} />
|
||||||
<InputText hasError placeholder="Type here" icon={Logo} />
|
<InputText hasError placeholder="Type here" icon={Logo} />
|
||||||
|
|
||||||
|
<InputText
|
||||||
|
placeholder="Type here"
|
||||||
|
icon={Logo}
|
||||||
|
suffix={() => <Text>.bksy.social</Text>}
|
||||||
|
/>
|
||||||
</View>
|
</View>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user