# Migration Plan: `TextInput` from `@hero-design/rn` to `@hero-design/rn-work-uikit`

This document outlines the steps and considerations for migrating the `TextInput` component from the `@hero-design/rn` package to the `@hero-design/rn-work-uikit` package.

## 1. Overview of Changes

While the component's props interface remains largely the same, the `@hero-design/rn-work-uikit` version introduces significant UI and behavioral changes. The new implementation is more modular and introduces a floating label for a more modern user experience.

## 2. Key Changes to Be Aware Of

### 2.1. Prefix Icon Visibility

In the `@hero-design/rn-work-uikit` version, the prefix icon is not visible when the `TextInput` is in its default, idle state (i.e., not focused and with no value). The prefix only appears once the `TextInput` is focused or contains a value. This is different from the `@hero-design/rn` version, where the prefix was always visible.

### 2.2. "Optional" Label Text

The behavior of the `required` prop has changed:

*   **Old Behavior (`@hero-design/rn`):** When `required={true}`, an asterisk (`*`) was appended to the label.
*   **New Behavior (`@hero-design/rn-work-uikit`):** When `required={false}` (or it is not provided), the text `(Optional)` is appended to the label. When `required={true}`, nothing is appended.

This is an inversion of how optionality is communicated to the user.

### 2.3. Layout of Helper and Error Text

The layout for displaying `helpText`, `error` messages, and the `maxLength` character count has been updated.

*   **Old Behavior (`@hero-design/rn`):** These elements were typically displayed below the input.
*   **New Behavior (`@hero-design/rn-work-uikit`):** The `error` or `helpText` is displayed on the left, and the `maxLength` count is on the right, within the same line under the input field. This makes for a more compact component.

## 3. Migration Steps

1.  **Update Imports:**
    Change the import statement from `@hero-design/rn` to `@hero-design/rn-work-uikit`.

    ```diff
    - import { TextInput } from '@hero-design/rn';
    + import { TextInput } from '@hero-design/rn-work-uikit';
    ```

2.  **Review existing `TextInput` implementations:**
    Audit your app for all uses of the `TextInput` component. Pay close attention to forms and screens where the visual layout is critical.

3.  **Perform Visual Regression Testing:**
    Due to the floating label and other layout changes, existing screens may look different. It is crucial to perform visual regression testing on all screens that use `TextInput`.

## 4. Updating Unit and Snapshot Tests

The UI changes will likely break your existing snapshot tests and may affect other unit tests.

### 4.1. Snapshot Tests

Snapshot tests for components using `TextInput` will need to be updated. After you've confirmed the new look is correct, you can update the snapshots.

```bash
# For Jest users
npm test -- -u
# or
yarn test -u
```

### 4.2. Unit Test Adjustments

Here's what to look for in your unit tests:

*   **Label Testing:**
    *   Tests that checked for an asterisk (`*`) for required fields will now fail.
    *   You should now check for the `(Optional)` text when the field is *not* required.

    *Example Test Case (using React Native Testing Library):*

    ```javascript
    // Old test for required field
    it('should display an asterisk for required fields', () => {
      const { getByText } = render(<TextInput label="My Field" required />);
      // This will now fail
      expect(getByText('*')).toBeTruthy();
    });

    // New test for optional field
    it('should display (Optional) for non-required fields', () => {
      const { queryByText } = render(<TextInput label="My Field" required={false} />);
      expect(queryByText(/\(Optional\)/)).toBeTruthy();
    });

    // New test for required field
    it('should not display (Optional) for required fields', () => {
      const { queryByText } = render(<TextInput label="My Field" required={true} />);
      expect(queryByText(/\(Optional\)/)).toBeNull();
    });
    ```

*   **Finding Elements:**
    *   Because of the new floating label animation, the way you query for elements might need to change, especially if you were targeting specific wrapper views for styling.
    *   The internal structure of the component has changed significantly. Tests that rely on `Dive` in Enzyme or deep selectors will be brittle and will likely need to be rewritten. It's better to test based on what the user sees, e.g., using `getByTestId`, `getByText`, etc.

*   **Interaction Tests:**
    *   If you have tests that simulate user input, the floating label will animate. This shouldn't affect most tests, but if you have complex tests that check for element positions, they might need adjustment.
