// Copyright (c) Meta Platforms, Inc. and affiliates. 'use client'; import {useId, useState} from 'react'; import * as stylex from '@stylexjs/stylex'; import {Card} from '@astryxdesign/core/Card'; import { Field, inputStatusBorderStyles, inputWrapperStyles, } from '@astryxdesign/core/Field'; import type {FieldStatusPlacement, InputStatus} from '@astryxdesign/core/Field'; import {useInputStatusIcon} from '@astryxdesign/core/hooks'; import {VStack} from '@astryxdesign/core/Layout'; import { SegmentedControl, SegmentedControlItem, } from '@astryxdesign/core/SegmentedControl'; import {Text} from '@astryxdesign/core/Text'; import {colorVars, spacingVars} from '@astryxdesign/core/theme/tokens.stylex'; const VALID_CODE = 'SAVE20'; const styles = stylex.create({ field: { paddingInline: spacingVars['--spacing-3'], }, input: { flexGrow: 1, minWidth: 0, borderWidth: 0, backgroundColor: 'transparent', color: colorVars['--color-text-primary'], font: 'inherit', outline: 'none', }, }); function CouponField({statusVariant}: {statusVariant: FieldStatusPlacement}) { const [code, setCode] = useState('SAVE10'); const inputId = useId(); const messageId = useId(); const status: InputStatus | undefined = code === '' ? undefined : code.toUpperCase() === VALID_CODE ? {type: 'success', message: 'Code applied: 20% off.'} : {type: 'error', message: 'This code is not valid.'}; // The hook decides what the field shows for this status and variant, and // returns the id of its tooltip only when that tooltip is in the DOM. const {statusIcon, describedBy} = useInputStatusIcon({ status, statusVariant, }); // `attached` and `detached` carry the text in a message box under the field; // `tooltip` carries it in the hook's tooltip instead. const hasMessageBox = statusVariant !== 'tooltip' && !!status && !!status.message; const ariaDescribedBy = [hasMessageBox ? messageId : null, describedBy].filter(Boolean).join(' ') || undefined; return (
setCode(e.target.value)} {...stylex.props(styles.input)} /> {statusIcon}
); } export default function UseInputStatusIconHookUsage() { const [statusVariant, setStatusVariant] = useState('attached'); return ( setStatusVariant(next as FieldStatusPlacement)} size="sm"> Type {VALID_CODE} to see the success status. ); }