addons/components/TextField.tsxblame
View source
b69ab311/**
b69ab312 * Copyright (c) Meta Platforms, Inc. and affiliates.
b69ab313 *
b69ab314 * This source code is licensed under the MIT license found in the
b69ab315 * LICENSE file in the root directory of this source tree.
b69ab316 */
b69ab317
b69ab318import type {ForwardedRef, ReactNode} from 'react';
b69ab319import type {ReactProps} from './utils';
b69ab3110
b69ab3111import * as stylex from '@stylexjs/stylex';
b69ab3112import {forwardRef, useId} from 'react';
b69ab3113import {Column} from './Flex';
b69ab3114
b69ab3115export const textFieldStyles = stylex.create({
b69ab3116 root: {
b69ab3117 gap: 0,
b69ab3118 },
b69ab3119 label: {
b69ab3120 marginBlock: '1px',
b69ab3121 },
b69ab3122 input: {
b69ab3123 boxSizing: 'border-box',
b69ab3124 height: '26px',
b69ab3125 padding: '0 9px',
b69ab3126 marginBlock: 0,
b69ab3127 minWidth: '100px',
b69ab3128 width: '100%',
b69ab3129 background: 'var(--input-background)',
b69ab3130 color: 'var(--input-foreground)',
b69ab3131 border: '1px solid var(--dropdown-border)',
b69ab3132 outline: {
b69ab3133 default: 'none',
b69ab3134 ':focus-visible': '1px solid var(--focus-border)',
b69ab3135 },
b69ab3136 outlineOffset: '-1px',
b69ab3137 },
b69ab3138});
b69ab3139
b69ab3140export const TextField = forwardRef(
b69ab3141 (
b69ab3142 {
b69ab3143 children,
b69ab3144 xstyle,
b69ab3145 containerXstyle,
b69ab3146 value,
b69ab3147 width,
b69ab3148 ...rest
b69ab3149 }: {
b69ab3150 children?: ReactNode;
b69ab3151 xstyle?: stylex.StyleXStyles;
b69ab3152 containerXstyle?: stylex.StyleXStyles;
b69ab3153 value?: string;
b69ab3154 width?: string;
b69ab3155 placeholder?: string;
b69ab3156 readOnly?: boolean;
b69ab3157 } & ReactProps<HTMLInputElement>,
b69ab3158 ref: ForwardedRef<HTMLInputElement>,
b69ab3159 ) => {
b69ab3160 const id = useId();
b69ab3161 return (
b69ab3162 <Column xstyle={[textFieldStyles.root, containerXstyle ?? null]} style={{width}} alignStart>
b69ab3163 {children && (
b69ab3164 <label htmlFor={id} {...stylex.props(textFieldStyles.label)}>
b69ab3165 {children}
b69ab3166 </label>
b69ab3167 )}
b69ab3168 <input
b69ab3169 {...stylex.props(textFieldStyles.input, xstyle)}
b69ab3170 type="text"
b69ab3171 id={id}
b69ab3172 value={value}
b69ab3173 {...rest}
b69ab3174 ref={ref}
b69ab3175 />
b69ab3176 </Column>
b69ab3177 );
b69ab3178 },
b69ab3179);